Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands using tee from sorted by
Terminal - Commands using tee - 90 results
echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-") | sudo tee /sys/bus/usb/drivers/usb/unbind
2014-04-06 12:06:29
User: tweet78
Functions: awk cut df echo grep head sudo tail tee tr
29

You have an external USB drive or key.

Apply this command (using the file path of anything on your device) and it will simulate the unplug of this device.

If you just want the port, just type :

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-")

command_line 2>&1 | tee -a output_file
num_errs=`grep ERROR /var/log/syslog | tee >(cat >&2) | wc -l`
2014-03-12 00:04:24
Functions: cat tee wc
0

Many circumstances call for creating variable of a summary result while still printing the original pipe. Inserting "tee >(cat >&2)" allows the command output to still be printed while permitting the same output to be processed into a variable.

ssh USER@HOST cat REMOTE_FILE.mp4 | tee LOCAL_FILE.mp4 | mplayer -
2013-11-28 11:25:26
User: flatcap
Functions: cat ssh tee
6

Securely stream a file from a remote server (and save it locally).

Useful if you're impatient and want to watch a movie immediately and download it at the same time without using extra bandwidth.

This is an extension of snipertyler's idea.

Note: This command uses an encrypted connection, unlike the original.

nc HOST PORT | tee movie.mp4 | mplayer -
2013-11-28 01:38:29
User: snipertyler
Functions: tee
7

Requires a listening port on HOST

eg. "cat movie.mp4 | nc -l 1356 " (cat movie.mp4 | nc -l PORT)

Useful if you're impatient and want to watch a movie immediately and download it at the same time without using extra bandwidth.

You can't seek (it'll crash and kill the stream) but you can pause it.

:w !sudo tee %
dd if=file | tee >(sha1sum) >(md5sum) >(sha256sum) >/dev/null
2013-11-07 17:43:54
User: dubbaluga
Functions: dd tee
Tags: tee parallel I/O
0

This is to overcome the issue of slow I/O by reading once and forwarding the output to several processes (e. g. 3 in the given command). One could also invoke grep or other programs to work on read data.

ssh remoteUser@remoteHost "tail -f /var/log/scs/remoteLogName" | tee localLogName
:w !sudo tee %
logger -tdnsupdate $(curl -s 'https://dynamicdns.park-your-domain.com/update?host=@&domain=xxx&password=xxx'|tee -a /root/dnsupdate|perl -pe'/Count>(\d+)<\/Err/;$_=$1eq"0"?"Update Sucessful":"Update failed"'&&date>>/root/dnsupdate)
2013-08-11 16:27:39
User: MagisterQuis
Functions: logger perl tee
1

Sets the @ A record for your domain hosted by namecheap to your current internet-facing IP address, logs success or failure with syslog, and logs the data returned to /root/dnsupdate.

Change the XXX's as appropriate.

More info at: http://www.namecheap.com/support/knowledgebase/article.aspx/29/

psgrep() { ps aux | tee >(head -1>&2) | grep -v " grep $@" | grep "$@" -i --color=auto; }
2013-08-02 12:44:32
User: fnl
Functions: grep head ps tee
Tags: grep ps
0

Pipes the header row of ps to STDERR, then greps for the command on the output of ps, removing the grep entry before that.

echo 'Etc/UTC' | tee /etc/timezone; dpkg-reconfigure --frontend noninteractive tzdata
2013-04-22 06:14:55
User: donatello
Functions: echo tee
0

Replace the first part of the command above with the appropriate timezone string. Eg: 'Europe/London' or for UTC - 'Etc/UTC'. The appropriate string can be found from https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

This is useful when your server is installed by a data centre (managed hardware, VPS, etc) and the timezone is not usually set to the one your prefer.

script -qf | tee >(nc -l -p 5000)
wget -O- http://example.com/mytarball.tgz | tee mytarball.tgz | tar xzv
2013-03-06 11:11:28
Functions: tar tee wget
0

Very similar as doing "wget http://example.com/mytarball|tar xzv", this one involves the "tee" command between both, which will simultaneously write the tarball and copy it to stdout. So this command will locally save the tarball and extract it - both at the same time while it downloads.

echo 'foo' | tee >(wc -c) >(grep o) >(grep f)
2013-01-31 09:54:18
User: totti
Functions: echo grep tee wc
Tags: tee output input
5

Output of a command as input to many

tee >(openssl md5 > sum.md5) <somefile | bzip2 > somefile.bz2
cat somefile | tee >(openssl md5 > sum.md5) | bzip2 > somefile.bz2
cat myfile | tee dest1 dest2 > /dev/null 2>&1
find . -printf "touch -m -d \"%a\" '%p'\n" | tee /tmp/retime.sh
2012-11-05 20:32:05
User: dmmst19
Functions: find tee
4

Sometimes when copying files from one place to another, the timestamps get lost. Maybe you forgot to add a flag to preserve timestamps in your copy command. You're sure the files are exactly the same in both locations, but the timestamps of the files in the new home are wrong and you need them to match the source.

Using this command, you will get a shell script (/tmp/retime.sh) than you can move to the new location and just execute - it will change the timestamps on all the files and directories to their previous values. Make sure you're in the right directory when you launch it, otherwise all the touch commands will create new zero-length files with those names. Since find's output includes "." it will also change the timestamp of the current directory.

Ideally rsync would be the way to handle this - since it only sends changes by default, there would be relatively little network traffic resulting. But rsync has to read the entire file contents on both sides to be sure no bytes have changed, potentially causing a huge amount of local disk I/O on each side. This could be a problem if your files are large. My approach avoids all the comparison I/O. I've seen comments that rsync with the "--size-only" and "--times" options should do this also, but it didn't seem to do what I wanted in my test. With my approach you can review/edit the output commands before running them, so you can tell exactly what will happen.

The "tee" command both displays the output on the screen for your review, AND saves it to the file /tmp/retime.sh.

Credit: got this idea from Stone's answer at http://serverfault.com/questions/344731/rsync-copying-over-timestamps-only?rq=1, and combined it into one line.

command 3>&1 1>&2 2>&3 | tee -a file
2012-10-30 15:40:08
User: netkill
Functions: command tee
0

Appends output to the file, some systems require the -a to do this.

command 3>&1 1>&2 2>&3 | tee file
2012-10-30 10:53:21
User: hute37
Functions: command tee
1

taken from

http://www.unix.com/shell-programming-scripting/158311-how-tee-stderr.html

"

What does it mean?

The redirection operator n>&m makes file descriptor n to be a copy of file descriptor m. So, whe are:

- Opening a new file descriptor, 3, that is a copy of file descriptor 1, the standard output;

- Making file descriptor 1 a copy of file descriptor 2, the standard error output;

- Making file descriptor 2 to be a copy of file descriptor 3 (the "backup" of the standard output)

in a short: we swapped the standard output and the standard error output.

"

COMMAND - OPTIONS - MORE OPTIONS | tee >> /file/you/want_to_append_output_to
echo $(find <directory> -name '*.<extension>' -exec du -s {} \; | tee $(tty) | cut -f1 | tr '\n' '+') 0 | bc
2012-09-17 22:46:50
User: ysangkok
Functions: cut du echo find tee tr
-1

Also shows files as they are found. Only works from a tty.

find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.cxx' \) | xargs grep "#include.*\.c.*" 2>&1 | tee source_inside_source_list.txt
echo "[some repository]" | sudo tee -a /etc/apt/sources.list
2012-04-21 17:54:08
Functions: echo sudo tee
-2

For instance, to add mongodb 10gen package

echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list