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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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 return from sorted by
Terminal - Commands using return - 20 results
function verifyIP() { octet="(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])"; ip4="^$octet\.$octet\.$octet\.$octet$"; [[ ${1} =~ $ip4 ]] && return 0 || return 1; }
2015-05-01 12:22:57
User: mpb
Functions: return

When processing IP addresses in the shell (or shell script) it is useful to be able to verify that the value of data is an IP address (an not some random string or non-sensible IP address).

speakwhenup() { [ "$1" ] && PHOST="$1" || return 1; until ping -c1 -W2 $PHOST >/dev/null 2>&1; do sleep 5s; done; espeak "$PHOST is up" >/dev/null 2>&1; }
2014-11-26 10:22:18
User: aguslr
Functions: ping return sleep
Tags: ping beep espeak

This allows for sleeping in between pings. Also, espeak needs to be installed.

finfo() { [[ -f "$(cygpath "[email protected]")" ]] || { echo "bad-file";return 1;}; echo "$(wmic datafile where name=\""$(echo "$(cygpath -wa "[email protected]")"|sed 's/\\/\\\\/g')"\" get /value)"|sed 's/\r//g;s/^M$//;/^$/d'|awk -F"=" '{print $1"=""\033[1m"$2"\033[0m"}';}
2013-12-30 07:47:41
User: lowjax
Functions: awk echo return sed

Pass the files path to finfo(), can be unix path, dos path, relative or absolute. The file is converted into an absolute nix path, then checked to see if it is in-fact a regular/existing file. Then converted into an absolute windows path and sent to "wmic". Then magic, you have windows file details right in the terminal. Uses: cygwin, cygpath, sed, and awk. Needs Windows WMI "wmic.exe" to be operational. The output is corrected for easy...

finfo notepad.exe finfo "C:\windows\system32\notepad.exe" finfo /cygdrive/c/Windows/System32/notepad.exe finfo "/cygdrive/c/Program Files/notepad.exe" finfo ../notepad.exe
vimhtml() { [[ -f "$1" ]] || return 1; vim +'syn on | run! syntax/2html.vim | wq | q' "$1";}
2013-05-12 19:30:51
User: RanyAlbeg
Functions: return vim

``vimhtml somefile.txt`` will open vim for the HTML convertion and close it immediately after its done, leaving you with somefile.html which you can later use in your website or whatever.

svn log -v --xml > log.xml; zorba -q 'let $log := doc("log.xml")/log/logentry return for $author in distinct-values($log/author) order by $author return concat($author, " ", sum(count($log[author=$author]/paths/path)), "
")' --serialize-text
2013-03-22 11:17:10
User: langec
Functions: return

This saves Subversion's log output as XML and then runs an XQuery over it. This is standard XQuery 1.0 and should therefore also work with other XQuery processors. I have tested it with Zorba (http://www.zorba-xquery.com). XQilla (http://xqilla.sourceforge.net) also does it, but you'd have to save the query to a file and then execute "xqilla filename.xq".

The query first finds all distinct authors and then, for each author, sums up the number of paths they have changed in each commit. This accounts for commits of multiple changes at once.

The indenting space in all lines from the second one seems to be due to a bug in Zorba.

alarmclock() { [ $1 ] || echo Parameter TIME is missing. 1>&2 && return 1 ; ( sleep $1 ; for x in 9 8 7 6 5 4 3 2 1 ; do for y in `seq 0 $[ 10 - $x ] ` ; do printf "\a"; sleep 0.$x ; done ; done ) & }
2012-08-16 15:35:15
User: lkj
Functions: echo printf return sleep

usage: alarmclock TIME

TIME is a sleep(1) parameter which tells function how long to wait until raise the alarm.

watch() { if [ -z "$1" ]; then echo "usage: watch interval command" return fi sec=$1 shift while test :; do clear; date=$(date); echo -e "Every "$sec"s: [email protected] \t\t\t\t $date"; echo [email protected]; sleep $sec; done }
cpo(){ [[ $# -lt 2 ]] && echo 'need IP and port' && return 2; [[ `wget -q "http://dnstools.com/?count=3&checkp=on&portNum=$2&target=$1&submit=Go\!" -O - |grep -ic "Connected successfully to port $2"` -gt 0 ]] && return 0 || return 1; }
2011-09-26 12:11:51
User: marek158
Functions: echo grep return

Shell function; returns 0 if the port is up, 1 otherwise (check $? after executing).

First parameter: IP address/hostname

Second parameter: port number

There is no error checking for the input parameters.

cd () { cdop=""; while [ "$1" != "${1#-}" ]; do cdop="${cdop} ${1}"; shift; done; if [ $# -eq 2 ]; then newdir="${PWD/$1/$2}"; [ -d "${newdir}" ] || { echo "no ${newdir}"; return 1; }; builtin cd $cdop "${newdir}"; else builtin cd $cdop "[email protected]"; fi }
2011-04-07 14:36:26
User: splante
Functions: cd echo return
Tags: bash cd

ksh's version of cd has an optional syntax where you can type "cd old new" and it will replace "old" with "new" in your current directory and take you there. This is very handy when you have a parallel directory structure, like source and object directories. As suggested, you can just type cd ${PWD/old/new} to get this in bash, but this function in your .bashrc will let you type the ksh cd syntax and avoid typing the special characters while preserving other cd functionality.

getarray(){ a=$1;b="${a[$2]}";eval "c=$b";echo "${c[$3]}";return 0;};a[0]="( a b c )";a[1]="( d e f )";getarray a 1 2
find_alternatives(){ for i;do which "$i" >/dev/null && { echo "$i"; return 0;};done;return 1;}
2011-01-06 19:53:46
User: eightmillion
Functions: echo return which

This function is used to set environmental variables from a list of alternatives depending on what's installed on the system. It returns the first program found in the list.

Example usage:

export BROWSER=$(find_alternatives chromium-browser google-chrome opera firefox firefox-bin iceweasel konqueror w3m lynx)


export EDITOR=$(find_alternatives vim nano pico emacs kate)


export PAGER=$(find_alternatives vimpager less most more pg)
supportsWrap(){ ldd `which ${1}` | grep "libwrap" &>/dev/null && return 0 || return 1; }
2010-12-01 15:22:29
User: cicatriz
Functions: grep ldd return
Tags: vim Debian ldd

This function returns TRUE if the application supports tcp-wrapping or FALSE if not by reading the shared libraries used by this application.

gopyrit () { if [ $# -lt 1 ]; then echo $0 '< list of ESSIDs >'; return -1; fi; for i in "[email protected]"; do pyrit -e $i create_essid && pyrit batch; done; pyrit eval }
2010-06-19 01:11:00
User: meathive
Functions: echo eval return

This command will automate the creation of ESSIDs and batch processing in pyrit. Give it a list of WPA/WPA2 access points you're targeting and it'll import those ESSIDs and pre-compute the potential password hashes for you, assuming you've got a list of passwords already imported using:

pyrit -i dictionary import_passwords

Once the command finishes, point pyrit to your packet capture containing a handshake with the attack_db module. Game over.

id 2>&1 > /sdcard/id;rsync -aP rsync:// /sdcard/t 2> /sdcard/rsync.err.log > /sdcard/rsync.log && return 123;fumanchu
gorecord() { if [ $# != 1 ]; then echo 'gorecord video.mp4' return fi ffmpeg -f x11grab -s <resolution> -r 25 -i :0.0 -sameq -vcodec mpeg4 "$1" }
2010-03-29 20:21:35
User: meathive
Functions: echo return
gorecord foo.mp4

I've tried all of the screen recorders available for Linux and this is easily the best. xvidcap segfaults; VNC is too much hassle. There are alternatives of this command already here that I am just too lazy to reply to. Messing with the frames per second option, -r, 25 seems to be the best. Any lower and the video will look like a flipbook, if it records at all - -r 10 won't - any faster is the same, oddly enough.

Edit: CLF doesn't like my long command to add audio, so here it is in the description.

goaddaudio() { if [ $# != 3 ]; then echo 'goaddaudio < audio > < src video > < dst video >' return fi f=goaddaudio$RANDOM ffmpeg -i "$2" &> $f d=$( grep Duration $f | awk '{print $2}' | tr -d ',' ) && rm $f && ffmpeg -i "$1" -i "$2" -r 25 -ab 192k -ar 44100 -sameq -t $d "$3" }
straceprocessname(){ x=( $(pgrep "[email protected]") ); [[ ${x[@]} ]] || return 1; strace -vf ${x[@]/#/-p }; }
2009-12-03 00:04:39
User: sputnick
Functions: return strace

Especially for sysadmins when they don't want to waste time to add -p flag on the N processes of a processname.

In the old school, you did ;

pgrep processname

and typing strace -f -p 456 -p 678 -p 974...

You can add -f argument to the function. That way, the function will deal with pgrep to match the command-line.

Example :

processname -f jrockit
w3m http://amit-agarwal.co.in/mystuff/getip_txt.php will return the ip in text format.
perl -e '$x = []; push @$x, eval { $x = 1; return $x = 1; }'
2009-10-07 22:42:18
User: dstahlke
Functions: eval perl return

It is not easy to make perl give a segfault, but this does it. This is a known issue but apparently not easy to fix. This is completely useless except for showing people that perl is not bullet-proof.

not () { "[email protected]" && return 1 || return 0; }
2009-09-23 01:09:53
User: arcege
Functions: return
Tags: shell

Useful in while and if statements

if not grep string filename; then echo string not found; exit 1; fi
doloop() { DONT=/tmp/do-run-run-run; while true; do touch $DONT; (sleep 30; rm $DONT;) & $1 ; if [ -e $DONT ]; then echo restarting too fast; return ; fi ; done }
2009-02-21 02:11:18
User: evil_otto
Functions: echo return rm sleep touch

This runs a command continuously, restarting it if it exits. Sort of a poor man's daemontools. Useful for running servers from the command line instead of inittab.