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:



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.

Top Tags



Commands tagged bash from sorted by
Terminal - Commands tagged bash - 724 results
function checkfor () { while :; do $*; sleep 5; done; }
2009-09-03 19:35:42
User: relay
Functions: sleep
Tags: bash

checkfor: have the shell check anything you're waiting for.

'while : ; do' is an infinite loop

'$*' executes the command passed in

'sleep 5' - change for your tastes, sleep for 5 seconds

bash, ksh, likely sh, maybe zsh

Ctrl-c to break the loop

function fcd () { [ -f $1 ] && { cd $(dirname $1); } || { cd $1 ; } pwd }
2009-09-03 18:58:13
User: relay
Functions: cd dirname pwd
Tags: bash ksh

fcd : file change directory

A bash function that takes a fully qualified file path and cd's into the directory where it lives. Useful on the commadline when you have a file name in a variable and you'd like to cd to the directory to RCS check it in or look at other files associated with it.

Will run on any ksh, bash, likely sh, maybe zsh.

echo "${PATH//:/$'\n'}"
for i in 192.168.1.{61..71};do ping -c 1 $i &> /dev/null && echo $i;fi;done
2009-08-26 06:04:24
User: AlecSchueler
Functions: echo ping
Tags: Network ssh bash

If you need to ssh into a computer on the local network but you're unsure of the ip to use, then ping them and see if you get a response. If you do, print out the address you got it from. Adjust the range to suit your network.

for file in <filename>; do cp $file{,.bak} && sed 's/old/new/g' $file.bak > $file; done
2009-08-25 16:19:45
User: Cenobite
Functions: cp file sed
Tags: bash sed

"&&" runs sed if and only if the backup completed and /bin/cp exited cleanly. Works for multiple files; just specify multiple filenames (or glob). Use -v switch for cp to play it safe.

eval $(sed -n "s/^d[^D]*DB_\([NUPH]\)[ASO].*',[^']*'\([^']*\)'.*/_\1='\2'/p" wp-config.php) && mysqldump --opt --add-drop-table -u$_U -p$_P -h$_H $_N | gpg -er AskApache >`date +%m%d%y-%H%M.$_N.sqls`
2009-08-18 07:03:08
User: AskApache
Functions: eval gpg sed

The coolest way I've found to backup a wordpress mysql database using encryption, and using local variables created directly from the wp-config.php file so that you don't have to type them- which would allow someone sniffing your terminal or viewing your shell history to see your info.

I use a variation of this for my servers that have hundreds of wordpress installs and databases by using a find command for the wp-config.php file and passing that through xargs to my function.

IFS=:; find $PATH | grep pattern
2009-08-14 13:38:58
User: camspiers
Functions: find grep
Tags: bash find grep

Best to put it in a file somewhere in your path. (I call the file spath)


IFS=:; find $PATH | grep $1

Usage: $ spath php

id="dMH0bHeiRNg";mplayer -fs http://youtube.com/get_video.php?video_id=$id\&t=$(curl -s http://www.youtube.com/watch?v=$id | sed -n 's/.*, "t": "\([^"]*\)", .*/\1/p')
2009-08-13 14:16:01
User: matthewbauer
Functions: id sed

The original doesn't work for me - but this does. I'm guessing that Youtube updated the video page so the original doesn't work.

not necessarily better, but many...!
2009-08-12 11:03:26
Tags: bash awk

( IFS=:; for i in $PATH; do echo $i; done; )

echo $PATH|sed -e 's/:/\n/g' # but the tr one is even better of course

echo $PATH|xargs -d: -i echo {} # but this comes up with an extra blank line; can't figure out why and don't have the time :(

echo $PATH|cut -d: --output-delimiter='

' -f1-99 # note -- you have to hit ENTER after the first QUOTE, then type the second one. Sneaky, huh?

echo $PATH | perl -l -0x3a -pe 1 # same darn extra new line; again no time to investigate

echo $PATH|perl -pe 's/:/\n/g' # too obvious; clearly I'm running out of ideas :-)

echo $PATH|awk -F: ' { for (i=1; i <= NF; i++) print $i }'
img test.jpg
2009-08-11 22:09:21
User: Inouire
Tags: bash java picture

This command allows you to see a preview of a picture via the terminal. It can be usefull when you are ssh'ing your server without X-forwarding.

To have en example of the output you can get with this command see http://www.vimeo.com/3721117

Download at http://inouire.net/image-couleur.html

Sources here: http://inouire.net/archives/image-couleur_source.tar.gz

find $MAILDIR/ -type f -printf '%T@ %p\n' | sort --reverse | sed -e '{ 1,100d; s/[0-9]*\.[0-9]* \(.*\)/\1/g }' | xargs -i sh -c "cat {}&&rm -f {}" | gzip -c >>ARCHIVE.gz
old='apt-get'; new="su-${old}"; command="sudo ${old}"; alias "${new}=${command}"; $( complete | sed -n "s/${old}$/${new}/p" ); alias ${new}; complete -p ${new}
2009-08-10 00:15:05
User: Josay
Functions: alias sed

In Bash, when defining an alias, one usually loses the completion related to the function used in that alias (that completion is usually defined in /etc/bash_completion using the complete builtin).

It's easy to reuse the work done for that completion in order to have smart completion for our alias.

That's what is done by this command line (that's only an example but it may be very easy to reuse).

Note 1 : You can use given command line in a loop "for old in apt-get apt-cache" if you want to define aliases like that for many commands.

Note 2 : You can put the output of the command directly in your .bashrc file (after the ". /etc/bash_completion") to always have the alias and its completion

cd `dirname $_`
cd !$:h
2009-08-07 00:37:08
User: lingo
Functions: cd

Uses the last argument of the last executed command, and gets the directory name from it.

Use $!:t for the filename alone, without the dirname.

wget `lynx -dump http://www.ebow.com/ebowtube.php | grep .flv$ | sed 's/[[:blank:]]\+[[:digit:]]\+\. //g'`
2009-08-02 14:09:53
User: spaceyjase
Functions: grep sed wget

I wanted all the 'hidden' .flv files from the http link in the command line; wget seemed appropriate, fed with output from lynx, grep the flv files and the normalised via sed (to remove the numeric bullet). Similar to the 'Grab mp3 files' fu. Replace link with your own, grep arg with something more interesting ;) See here for something along the same lines...


Hope you find it useful! Improvements welcome, naturally.

no_of_files=10; counter=1; while [[ $counter -le $no_of_files ]]; do echo Creating file no $counter; dd bs=1024 count=$RANDOM skip=$RANDOM if=/dev/sda of=random-file.$counter; let "counter += 1"; done
2009-07-31 16:34:47
User: rajaseelan
Functions: dd echo file
Tags: bash dd

Create a bunch of random files with random binary content. Basically dd dumps randomly from your hard disk to files random-file*.

[[ "$WINDOW" ]] && PS1="\u@\h:\w[$WINDOW]\$ "
2009-07-23 06:46:19
User: recursiverse
Tags: bash screen shell

Add this to your $HOME/.bashrc file. It will only set this prompt if it is running inside screen ($WINDOW var is set)

Looks like this...

lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|sed -nr s/'^.*My IP address city: (.+)$/\1/p'
ls -drt /var/log/* | tail -n5 | xargs sudo tail -n0 -f
2009-07-22 14:44:41
User: kanaka
Functions: ls sudo tail xargs
Tags: bash tail log watch

This command finds the 5 (-n5) most frequently updated logs in /var/log, and then does a multifile tail follow of those log files.

Alternately, you can do this to follow a specific list of log files:

sudo tail -n0 -f /var/log/{messages,secure,cron,cups/error_log}

tar -pczf archive_name.tar.gz /path/to/dir/or/file
2009-07-17 19:53:02
User: ryuslash
Functions: tar
Tags: bash tar gzip

Create a single tar.gz archive

I know it's a very basic one, but it's one I keep forgetting.

sudo rmmod pcspkr
2009-07-17 18:21:19
User: zombiedeity
Functions: rmmod sudo

To ensure that it will never come back, you can edit /etc/modprobe.d/blacklist

Add "blacklist pcspkr" sans quotes

infile=$1 for i in $(cat $infile) do echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//" echo done
2009-07-12 21:23:37
User: iframe
Functions: cat echo sed sort tr
Tags: cat bash sort sed tr

Save the script as: sort_file

Usage: sort_file < sort_me.csv > out_file.csv

This script was originally posted by Admiral Beotch in LinuxQuestions.org on the Linux-Software forum.

I modified this script to make it more portable.

iconv -f437 -tutf8 asciiart.nfo
2009-07-11 23:50:05
User: speaker
Functions: iconv

Files containing ascii art (e.g. with .nfo extension) are typically not correctly reproduced at the command line when using cat. With iconv one can easily write a wrapper to solve this:


if [ -z "$@" ]; then echo "Usage: $(basename $0) file [file] ..."

else iconv -f437 -tutf8 "$@"; fi

exit 0
for y in $(seq 2009 2011); do cal $y; done
2009-07-10 10:07:46
Functions: cal seq
Tags: bash seq cal

print multiple increasing years using cal - calendar -. You can also try

seq Start Increment End