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.

Universal configuration monitoring and system of record for IT.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands using read from sorted by
Terminal - Commands using read - 295 results
read c; while [ -n "$c" ]; do clear; echo -e "$c = "$(echo "$c" |bc -l)"\n"; read c; done
read -d ""
2011-10-25 18:44:11
User: totti
Functions: read

Handle any bad named file which contains ",',\n,\b,\t,` etc

Store the file name as null character separated list

find . -print0 >name.lst

and retrieve it using

read -r -d ""


find . -print0 >name.lst; cat name.lst| while IFS="" read -r -d "" file; do ls -l "$file"; done
while read line; do echo $line; done <<< "$var"
2011-09-22 16:53:32
User: totti
Functions: echo read

Consider the following simple situation [ reading something using while and read ]

[See script 1 in sample output]


The variable var is assigned with "nullll" at first. Inside the while loop [piped while] it is assigned with "whillleeee". [Onlly 2 assignments stmts]. Outside the loop the last assigned value for "var" [and no variable] inside the while can't be accessed [Due to pipe, var is executed in a sub shell].

In these type of situation variables can be accessed by modifying as follows.

[See script 2 in sample output]


Vary helpful when reading a set of items, say file names, stored on a file [or variable] to an array an use it later.

Is there any other way 2 access variables inside and outside the loop ??

find . -printf "%T@ %p\n" | sed -e 1d | while read ts fn; do ts=${ts%.*}; if [ $ts -ge ${gts:-0} ]; then gts=$ts; echo `date -d @$gts` $fn; fi; done
read -ra words <<< "<sentence>" && echo "${words[@]^}"
param=${param:-$(read -p "Enter parameter: "; echo "$REPLY")}
2011-09-08 20:48:31
User: frans
Functions: echo read

Can be used for command line parameters too.

If you have a more complicated way of entering values (validation, GUI, ...), then write a function i.e. EnterValue() that echoes the value and then you can write:

ping HOSTNAME | while read pong; do echo "$(date): $pong"; done
get_duration () { IFS=.: read -r _ h m s _ < <(ffmpeg -i "$1" 2>&1 | grep Duration);echo $(( h * 3600 + m * 60 + s )); }
SCALE=3; WIDTHL=10; WIDTHR=60; BAR="12345678"; BAR="${BAR//?/==========}"; while read LEFT RIGHT rest ; do RIGHT=$((RIGHT/SCALE)); printf "%${WIDTHL}s: %-${WIDTHR}s\n" "${LEFT:0:$WIDTHL}" "|${BAR:0:$RIGHT}*"; done < dataset.dat
2011-08-22 19:35:21
User: andreasS
Functions: printf read

WIDTHL=10 and WIDTHR=60 are setting the widths of the left and the right column/bar. BAR="12345678" etc. is used to create a 80 char long string of "="s. I didn't know any shorter way.

If you want to pipe results into it, wrap the whole thing in ( ... )

I know that printing bar graphs can be done rather easily by other means. Here, I was looking for a Bash only variant.

while IFS= read -r -u3 -d $'\0' file; do file "$file" | egrep -q 'executable|ELF' && chmod +x "$file"; done 3< <(find . -type f -print0)
2011-08-18 15:37:23
User: keymon
Functions: chmod egrep file find read

If you make a mess (like I did) and you removed all the executable permissions of a directory (or you set executable permissions to everything) this can help.

It supports spaces and other special characters in the file paths, but it will work only in bash, GNU find and GNU egrep.

You can complement it with these two commands:

1. add executable permission to directories:

find . type d -print0 | xargs -0 chmod +x

2. and remove to files:

find . type d -print0 | xargs -0 chmod -x

Or, in the same loop:

while IFS= read -r -u3 -d $'\0' file; do case $(file "$file" | cut -f 2- -d :) in :*executable*|*ELF*|*directory*) chmod +x "$file" ;; *) chmod -x "$file" ;; esac || break done 3< <(find . -print0)

Ideas stolen from Greg's wiki: http://mywiki.wooledge.org/BashFAQ/020

git branch | cut -c3- | grep -v "^master$" | while read line; do git branch -d $line; done | grep 'Deleted branch' | awk '{print $3;}' | while read line; do git push <target_remote> :$line; done
2011-08-13 16:58:34
User: gocoogs
Functions: awk cut grep read

attempts to delete all local branches. git will fail on any branches not fully merged into local master, so don't worry about losing work. git will return the names of any successfully deleted branches. Find those in the output with grep, then push null repositories to the corresponding names to your target remote.


- your local and remote branches are identically named, and there's nothing extra in the remote branch that you still want

- EDIT: you want to keep your local master branch

find . -type l | (while read FN ; do test -e "$FN" || ls -ld "$FN"; done)
read day month year < <(date +'%d %m %y')
2011-07-30 06:06:29
User: frans
Functions: date read
Tags: bash read

No command substitution but subshell redirection

read day month year <<< $(date +'%d %m %y')
read VAR1 VAR2 VAR3 <<< aa bb cc; echo $VAR2
find src/ -name "*.java" | while read f; do echo -n "$f "; cat "$f" | tr -dc '{}'; echo; done | awk '{ print length($2), $1 }' | sort -n
read -s -p"Password: " USER_PASSWORD_VARIABLE; echo
2011-07-20 20:22:42
User: h3nr1x
Functions: read
Tags: read

You can ask repeatedly for a non-blank password using this function:

function read_password() {

while [ ! -n "$USER_PASSWORD" ]; do

read -s -p"Password: " USER_PASSWORD

if [ ! -n "$USER_PASSWORD" ]; then

echo "ERROR: You must specify a valid password, please try again"





Also you can set a time out (in seconds) to write the password

read -t 10 -s -p"Password: " USER_PASSWORD_VARIABLE

if [ ! $? -eq 0 ]; then

echo "Time out!"


tail -n0 -f /var/log/messages | while read line; do notify-send "System Message" "$line"; done
2011-07-11 22:33:24
User: hukketto
Functions: read tail
Tags: notify-send

It willl popup a message for each new entry in /var/log/messages

found on the notify-send howto page on ubuntuforums.org.

Posted here only because it is one of the favourites of mine.

find ./ $1 -name "* *" | while read a ; do mv "${a}" "${a//\ /_}" ; done
while ping -c 1 > /dev/null; do acpi -t -f | while read tem; do notify-send "$tem"; done; sleep 300; done
2011-07-02 06:47:25
User: c0de
Functions: acpi ping read sleep

works best in a shell script run at startup. It will ping localhost once and output to null, after it does that, acpi is called for temperature in fahrenheit and piped through to another loop that feeds notify-send for a tooltip. After waiting five minutes, it will start over.

fdupes -r -1 path | while read line; do j="0"; for file in ${line[*]}; do if [ "$j" == "0" ]; then j="1"; else ln -f ${line// .*/} $file; fi; done; done
find . | grep ".*\[[Church|CPYAF].*" | while read f; do mv "$f" ../emails;done
read -t 0.1 -N 255
ls * | while read fin;do fout=$(echo -n $fin | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e);if [ "$fout" != "$fin" ];then echo "mv '$fin' '$fout'";fi;done | bash -x
2011-05-18 07:24:54
User: pawelb1973
Functions: bash echo ls read sed xargs

urldecode files in current directrory

cat search_items.txt | while read i; do surfraw google -browser=firefox $i; done
2011-05-12 09:27:08
User: bubo
Functions: cat read

tired of opening tabs and fill in search forms by hand? just pipe the search terms you need into this surfraw loop. you can use any browser you have installed, but a graphical browser with a tabbed interface will come in handy. surfraw can be found here: