Tag Info

Hot answers tagged

34

This question is addressed in BashFAQ/032. In your example, you would: { time sleep 1; } 2> /dev/null The reason why time sleep 1 2>/dev/null doesn't behave how you're expecting is because with that syntax, you'll want to time the command sleep 1 2>/dev/null (yes, the command sleep 1 with stderr redirected to /dev/null). The builtin time works ...


26

curl --fail does part of what you want: from man curl: -f, --fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and ...


24

wget -O - http://whatever.com/page.php > /dev/null or, if you want to redirect standard error output also: wget -O - http://whatever.com/page.php > /dev/null 2>&1 or, for codegolf :-) wget -O-


22

zcat foo.sql.gz | mysql -uroot -ppassword foo This will also leave foo.sql.gz as it is.


13

If you just want to display the contents of the curled page, you can do this: STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" URL) if test $STATUSCODE -ne 200; then # error handling fi This writes the page's content to STDERR while writing the HTTP status code to STDOUT, so it can be assigned to the variable STATUSCODE.


12

Use pbcopy: echo foo | pbcopy Also see man pbcopy for info on it and pbpaste.


11

As an alternative, you could pipe the output through tools designed with the primary purpose of maintaining size-capped, automatically rotated, log file sets, such as: Dan Bernstein's multilog Gerrit Pape's svlogd Tools to then process multilog-format log file sets include, amongst others: Russ Allbery's multilog-watch logrange Paul Kremer's ...


10

If you can have it go to one of the standard log streams (syslog, daemon, cron, user, security, mail, etc.) you can use the logger command and pipe to it instead. echo "Hello." | logger -p daemon.info Otherwise, you may be better off piping your logged content to a custom program or script to handle it, or look at setting up the logrotate configuration. ...


10

There are only three ways I know of to determine what a program will output to STDOUT and what to STDERR Read the documentation. Or Experiment with redirection† print STDERR in red †For example: program > program.stdout 2> program.stderr Then look at the two output files to see what the program has written to STDOUT and what it has written to ...


9

Your particular question about time builtin has been answered, but there are some commands that don't write either to stdout or to stderr. A classic example is the Unix command crypt. crypt with no arguments encrypts standard input stdin and writes it to standard output stdout. It prompts the user for a password using getpass(), which by defaults outputs ...


9

java -version &> version.txt The exit code has nothing to do with where it outputs.


9

It's kind of a feature nowadays. ;-) From an ancient bug report: We should think very, very carefully before ever fixing this bug. It's obviously the wrong thing to print version information to stderr, but since we've been doing that since the beginning of time it seems likely that we'll break existing systems built on top of Java if we change it ...


8

PowerShell sure does, the cmdlet is called Tee-Object. You can also use the alias tee if you're more used to the Unix-like approach: PS C:\Documents and Settings\Administrator> help Tee-Object NAME Tee-Object SYNOPSIS Saves command output in a file or variable and displays it in the console. example: C:>get-process | tee -filepath C:\file.txt ...


8

the rotatelogs tool shipped with apache (in the bin dir) (see docs) takes input from stdin and rotates the log after some specific amount of time


7

Just in case anyone still needs to do this, you can do it with gpg -ac -o- infile Setting the output filename to "-" sends the output to stdout.


7

For building pipelines, obviously. The biggest advantage is that you can avoid having to create temporary files: (For this example, assume an old version of tar which does not have -J or -I compression options.) tar -c foo/*.png | xz -9 | uuencode "foo.tar.xz" | mail -s "Sending foo" musa Now imagine if you had to use -f and stuff... you'd have VMS. ...


6

The same -f - option works for tarring as well. tar -cf - something | tar -C somefolder -xvf - GNU tar uses stdio by default: tar -c something | tar -C somefolder -xv rsync is also popular. rsync -av something/ somefolder/


6

Edit: I cannot be certain this is how mysql does it, but it could be using isatty(3) to determine whether STDOUT is a terminal or not, and modifying the output accordingly. Edit 2: The mysql command line tool definitely uses isatty(). You can read the source code. There are some good examples of this (although not in C) over at Stack Overflow: PHP: How ...


6

Two problems you posit: watch only polls as opposed to outputting when there are updates tail -f does not poll. As of version 7.5 of GNU Coreutils, tail -f will use the Linux kernel's inotify interface to receive signals from the kernel indicating that the file has been modified. If you don't use GNU Coreutils, or use a version older than 7.5 for some ...


6

wget -qO /dev/null http://whatever.com/page.php -q to make it quiet -O /dev/null to ignore the page contents


6

Use tee: command | tee -a mylog.txt will append the output of the command to the file and also show it on screen.


5

The windows PowerShell has a tool that can do that, named tee after the unix tool which does the same. Alternatively, there are ports of the unix tee for windows: GNU utilities for Win32 (last update 2003) CoreUtils for Windows (last update 2005)


5

Chances are that the shell that cron uses doesn't support the shorthand redirection operator &>> that Bash supports. You should use the portable form which is supported in the Bourne shell and others: * * * * * /home/user/script >> /home/user/log.txt 2>&1 That says "append the standard output (file descriptor 1) to the file and ...


5

For those on Max OSX there is a bug with zcat so you'll need to use gzcat instead. gzcat foo.sql.gz | mysql -uroot -ppassword foo


5

You can run ctl-z to background it and then kill the PID. $ ctl z (to background it) $ kill -9 {pid} 'of process that just got backgrounded'


5

When you open a file for writing with >, it is truncated before you even read it. That's why it's empty. Alternative with a temporary file (yes, this is quite common): php -r "…" < build/index.php > tmp mv tmp build/index.php Ideally, use mktemp to generate a unique temporary file: tmp="$(mktemp /tmp/file.XXX)" php -r "…" < build/index.php ...


4

if you do not care whether the string match originates from stdout or stderr, then just merge the two streams by redirecting stderr to stdout, then do the grep: $ your_program 2>&1 | grep "pattern" the example works in sh, bash, ksh, zsh. csh should be: $ your_program |& grep "pattern"


4

Usually, gdm/session start-up scripts redirect stderr & stdout to either: ~/.xsession-errors or ~/.cache/gdm/session.log With systemd and recent gdm versions, everything is redirected to systemd journal, so one way to get that output is: journalctl -b _PID=$(pgrep gnome-session)


4

Use find with the exec option, but first create the target folder. mkdir -p /home/somewhere/else find /somewhere -iname "*.xxx" This will list everything that would be moved. Now if you're sure these are the files you want to move, execute the following: find /somewhere -iname "*.xxx" -exec mv '{}' /home/somewhere/else/ \; In the exec line, '{}' will ...



Only top voted, non community-wiki answers of a minimum length are eligible