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 read from sorted by
Terminal - Commands using read - 279 results
ps -ef | grep user | awk '{print $2}' | while read pid; do echo $pid ; pfiles $pid| grep portnum; done
2010-01-11 12:34:51
User: sharfah
Functions: awk echo grep ps read
0

My old Solaris server does not have lsof, so I have to use pfiles.

echo 'Desktop SPAM!!!' | while read SPAM_OUT; do notify-send "$SPAM_OUT"; done
2009-12-31 15:38:35
User: michelsberg
Functions: echo read
Tags: notify-send
2

Route output to notify-send to show nice messages on the desktop, e.g. title and interpreter of the current radio stream

read enterKey
2009-12-29 00:18:32
User: bbmarek
Functions: read
Tags: bash read
-4

how to finish command or script without any output

getent shadow | while IFS=: read a b c; do grep -q '!' <<< "$b" && echo "$a LOCKED" || echo "$a not locked"; done
2009-12-18 15:24:19
User: sputnick
Functions: echo getent grep read
0
man 5 shadow

I think it's more reliable, because

passwd -S

dont show "locked" but "L" as second field on my Archlinux for a particular user.

( unixhome alternative ).

awk -F":" '{ print $1 }' /etc/passwd | while read UU ; do STATUS=$(passwd -S ${UU} | grep locked 2>/dev/null) ; if [[ ! -z ${STATUS} ]] ; then echo "Account ${UU} is locked." ; fi ; done
A=1;B=100;X=0;C=0;N=$[$RANDOM%$B+1];until [ $X -eq $N ];do read -p "N between $A and $B. Guess? " X;C=$(($C+1));A=$(($X<$N?$X:$A));B=$(($X>$N?$X:$B));done;echo "Took you $C tries, Einstein";
2009-12-16 13:24:23
User: rodolfoap
Functions: read
Tags: Game
13

Felt like I need to win the lottery, and wrote this command so I train and develop my guessing abilities.

while [ 1 ]; do banner 'ze missiles, zey are coming! ' | while IFS="\n" read l; do echo "$l"; sleep 0.01; done; done
2009-12-14 07:40:07
User: craigds
Functions: banner echo read sleep
10

Displays a scrolling banner which loops until you hit Ctrl-C to terminate it.

Make sure you finish your banner message with a space so it will loop nicely.

while read str; do echo "$((++i)) - $str"; done < infile
2009-12-09 15:11:25
User: putnamhill
Functions: echo read
Tags: bash
-1

Hi glaudiston, you can save a few chars by leaving out cat and pipe and still enjoy the added flexibility.

cat infile | while read str; do echo "$((++i)) - $str" ; done;
2009-12-09 14:05:09
User: glaudiston
Functions: cat echo read
-1

Yep, is hard, but is a way more flexible using pipe.

while read n; do host $n; done < list
while IFS=: read u x; do passwd -e "$u"; done < /etc/passwd
2009-11-20 21:49:34
User: cfajohnson
Functions: passwd read
0

This assumes that te original's 'passwd -e' forces a user to change password; it doesn't in the versions I have.

find <path> -name "*.tgz" -or -name "*.tar.gz" | while read file; do echo "$file: "; tar -tzf $file; done
2009-11-10 20:39:04
User: polaco
Functions: echo find read tar
Tags: find tar list
-2

This script will list all the files in the tarballs present on any folder or subfolder of the provided path. The while loop is for echoing the file name of the tarball before listing the files, so the tarball can be identified

find -name '*.avi' | while read i ; do echo $(mplayer -identify -frames 0 -vo null -nosound "$i" 2>&1 | grep ID_LENGTH | cut -d= -f2)" ""$i" ;done | sort -k1 -r -n | sed 's/^\([^\ ]*\)\ \(.*\)$/\2:\1/g'
2009-11-09 17:14:59
User: ZungBang
Functions: cut echo find grep read sed sort
2

handles file names with spaces and colons, fixes sort (numeric!), uses mplayer, same output format as other alternatives

find . -type f | while read f; do chmod -x "$f"; done
read -sn 1 -p 'Press any key to continue...';echo
read -sn 1 -p "Press any key to continue..."
git ls-files | while read i; do git blame $i | sed -e 's/^[^(]*(//' -e 's/^\([^[:digit:]]*\)[[:space:]]\+[[:digit:]].*/\1/'; done | sort | uniq -ic | sort -nr
2009-10-25 09:40:01
User: pipping
Functions: read sed sort uniq
Tags: statistics git
-1

You'll run into trouble if you have files w/ missing newlines at the end. I tried to use

PAGER='sed \$q' git blame

and even

PAGER='sed \$q' git -p blame

to force a newline at the end, but as soon as the output is redirected, git seems to ignore the pager.

seg() { echo -e "$1" | while read LINE; do for b in $(seq 10); do echo $LINE.$b; done; done; }
ord () { seq 1 127 | while read i; do echo `chr $i` $i; done | grep "^$1 " | cut -c '3-' }
2009-10-16 21:54:01
User: infinull
Functions: cut echo grep read seq
0

uses the previous "chr" function and uses it to create the inverse function "ord" by brute force.

It's slow, It's inelegant, but it works.

I thought I needed ord/chr to do a cartesian cipher in shell script a whie ago, but eventualy I realized I could get fancy with tr and do the same thing...

dpkg --get-selections | cut -f1 | while read pkg; do dpkg -L $pkg | xargs -I'{}' bash -c 'if [ ! -d "{}" ]; then echo "{}"; fi' | tr '\n' '\000' | du -c --files0-from - | tail -1 | sed "s/total/$pkg/"; done
2009-10-12 14:57:54
User: pykler
Functions: bash cut du echo read sed tail tr xargs
Tags: Debian wajig
4

Calculates the size on disk for each package installed on the filesystem (or removed but not purged). This is missing the

| sort -rn

which would put the biggest packges on top. That was purposely left out as the command is slightly on the slow side

Also you may need to run this as root as some files can only be checked by du if you can read them ;)

cat file.txt | while read line; do printf "%7.2f -> %7.2f\n" $line; done
beepwhenup () { echo 'Enter host you want to ping:'; read PHOST; if [[ "$PHOST" == "" ]]; then exit; fi; while true; do ping -c1 -W2 $PHOST 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then for j in $(seq 1 4); do beep; done; ping -c1 $PHOST; break; fi; done; }
2009-09-24 18:11:10
Functions: echo host ping read seq
Tags: ping beep
2

After this, just type:

beepwhenup

You need to install "beep" before this would make the beep sound.

Save it in your .profile if you want to use it later

WARNING: this command won't exit until it is successful. You won't be able to CONTROL+C out of it.

tar -cvf /dev/null . | while read i; do ls -l $i; done
2009-09-16 16:59:15
User: lbonanomi
Functions: ls read tar
-8

I find the ouput of ls -lR to be un-satisfying (why is the path data up there?) and find syntax to be awkward. Running 'du -a' means you will have likely to trim-off filesize data before feeding filenames to the next step in the pipe.

find repMainPath -maxdepth 1 -mindepth 1 -type d | while read dir; do echo processing $dir; sudo svnadmin dump --deltas $dir >dumpPath/`basename $dir`; done
2009-09-15 20:14:51
User: Marco
Functions: dump echo find read sudo
Tags: bash svn
2

This command dumps all SVN repositories inside of folder "repMainPath" (not recursively) to the folder "dumpPath", where one dump file will be created for each SVN repository.

find -type f |while read a;do [ "`head -c3 -- "${a}"`" == $'\xef\xbb\xbf' ] && echo "Match: ${a}";done