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.


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.




Commands tagged bash from sorted by
Terminal - Commands tagged bash - 716 results
chronic () { t=$1; shift; while true; do $@; sleep $t; done & }
2009-06-13 05:57:54
User: rhythmx
Functions: sleep

Chronic Bash function:

chronic 3600 time # Print the time in your shell every hour chronic 60 updatedb > /dev/null # update slocate every minute

Note: use 'jobs' to list background tasks and fg/bg to take control of them.

URL=http://svn.example.org/project; diff -u <(TZ=UTC svn -q log -r1:HEAD $URL | grep \|) <(TZ=UTC svn log -q $URL | grep \| | sort -k3 -t \|)
2009-06-03 14:26:55
User: sunny256
Functions: diff grep sort
Tags: bash svn

Lists revisions in a Subversion repository with a timestamp that doesn't follow the revision numbering order. If everything is OK, nothing is displayed.

for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r
2009-06-03 08:25:00
User: brunost
Functions: echo head perl sort

Print out list of all branches with last commit date to the branch, including relative time since commit and color coding.

svn log fileName|cut -d" " -f 1|grep -e "^r[0-9]\{1,\}$"|awk {'sub(/^r/,"",$1);print "svn cat fileName@"$1" > /tmp/fileName.r"$1'}|sh
2009-05-27 02:11:58
User: fizz
Functions: awk cut grep
Tags: bash svn awk grep

exported files will get a .r23 extension (where 23 is the revision number)

sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e
2009-05-25 05:37:44
User: mohan43u
Functions: echo sed xargs
echo "http%3A%2F%2Fwww.google.com" | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e


Works under bash on linux. just alter the '-e' option to its corresponding equivalence in your system to execute escape characters correctly.

diff -uw <(fmt -1 {file1, file2})
2009-05-23 03:11:08
User: sunny256
Functions: diff fmt
Tags: bash diff

It can be hard to spot differences in reformatted files, because of all the diff noise created by word wrapped lines. This command removes all the noise and performs a word-by-word diff. To ignore empty lines, add -B to the diff command. Also, if this is something you do often, you might want to check out the wdiff(1) program.

diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
2009-05-21 04:44:29
User: mbirk
Functions: cd diff find
Tags: bash diff find

This uses Bash's "process substitution" feature to compare (using diff) the output of two different process pipelines.

cd "$(mktemp -d)"
2009-05-20 11:48:12
User: Weboide
Functions: cd
Tags: bash directory

This command create a new temp directory using mktemp (to avoid collisions) and change the current working directory to the created directory.

expanded_script=$(eval "echo \"$(cat ${sed_script_file})\"") && sed -e "${expanded_script}" your_input_file
2009-05-07 14:21:14
Functions: eval sed

With this command you can use shell variables inside sed scripts.

This is useful if the script MUST remain in an external file, otherwise you can simply use an inline -e argument to sed.

screen -d -m nautilus --no-desktop `pwd`
2009-05-07 00:49:07
User: windsurfer
Functions: screen

This opens up nautilus in the current directory, which is useful for some quick file management that isn't efficiently done from a terminal.

2009-05-06 08:01:06
User: P17
Tags: bash

The colors are defined as variables.




shopt -s globstar
2009-05-05 16:02:44
User: Alanceil

Since bash 4.0, you can use ** to recursively expand to all files in the current directory. This behaviour is disabled by default, this command enables it (you'd best put it in your .profile). See the sample output for clarification.

In my opinion this is much better than creating hacks with find and xargs when you want to pass files to an application.

alias somafm='read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls'
2009-05-05 12:13:46
User: denzuko
Functions: alias

This is the alias command that I discussed in my prior release which you can add to your ~/.bashrc.

This command asks for the station name and then connects to somafm, Great for those who have linux home entertainment boxes and ssh enabled on them, just for the CLI fiends out there ( I know I'm one of them ;)

You can find future releases of this and many more scripts at the teachings of master denzuko - denzuko.co.cc.

lynx -dump randomfunfacts.com | grep -A 3 U | sed 1D
2009-05-05 07:52:10
User: xizdaqrian
Functions: grep sed

This is a working version, though probably clumsy, of the script submitted by felix001. This works on ubuntu and CygWin. This would be great as a bash function, defined in .bashrc. Additionally it would work as a script put in the path.

export LSCOLORS=gxfxcxdxbxegedabagacad
2009-05-04 04:07:36
User: haivu
Functions: export
Tags: bash ls osx

I use terminal with black background on the Mac. Unfortunately, the default ls color for the directory is blue, which is very hard to see. By including the line above in my ~/.bash_profile file, I changed the directory's color to cyan, which is easer to see. For more information on the syntax of the LSCOLORS shell variable:

man ls

I tested this command on Mac OS X Leopard

read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls
2009-05-04 00:26:19
User: denzuko
Functions: read

This command asks for the station name and then connects to somafm, Great for those who have linux home entertainment boxes and ssh enabled on them, just for the CLI fiends out there ( I know I'm one of them ;)

Also, don't forget to add this as alias(ie alias somafm="read -p 'Which Station? "; mplayer --reallyquite -vo none -ao sdl

cd $(dirname $(find ~ -name emails.txt))
2009-05-01 21:26:58
User: haivu
Functions: cd dirname find
Tags: bash dirname

This command looks for a single file named emails.txt which is located somewhere in my home directory and cd to that directory. This command is especially helpful when the file is burried deep in the directory structure. I tested it against the bash shells in Xubuntu 8.10 and Mac OS X Leopard 10.5.6

while :; do xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$(find <image-directory> -type f -iregex '.*\.\(bmp\|gif\|jpg\|png\)$' | sort -R | head -1)"; sleep 30m; done
2009-04-30 03:09:52
Functions: sleep
Tags: bash find xfce

Change your wallpaper every thirty minutes (or however long you like, I suppose) to a randomly selected image in a directory and subdirectories. Bear in mind this is not safe to use if anyone else has write access to your image directory.

ls -S -lhr
2009-04-28 01:28:57
User: rez0r
Functions: ls

This command list and sort files by size and in reverse order, the reverse order is very helpful when you have a very long list and wish to have the biggest files at the bottom so you don't have scrool up.

The file size info is in human readable output, so ex. 1K..234M...3G

Tested with Linux (Red Hat Enterprise Edition)

history -d
2009-04-27 20:19:09
User: sud0er
Tags: bash

If you're a moron like me, sometimes your fingers get away from you and you, for example, enter your password when you're already authenticated to ssh-agent, sudo, etc., and your password ends up in shell history. Here's how to get it out.

for i in sys dev proc; do sudo mount --bind /$i /mnt/xxx/$i; done
2009-04-20 16:52:14
User: amosshapira
Functions: mount sudo
Tags: bash mount chroot

The command is useful when, e.g., booting an existing system with a rescue or installation CD where you need to chroot into the hard-disk and be able to do stuff which accesses kernel info (e.g. when installing Ubuntu desktop with LVM2 you need to mount and chroot the hard disk from a shell window in order to install packages and run initramfs inside chroot).

The command assumes that /mnt/xxx is where the chroot'ed environment's root file system on the hard disk is mounted.

xmms2 mlib search NOT +rating | grep -r '^[0-9]' | sed -r 's/^([0-9]+).*/\1/' | sort -R | head | xargs -L 1 xmms2 addid
2009-04-16 20:27:30
Functions: grep head sed sort xargs

If you're like me and want to keep all your music rated, and you use xmms2, you might like this command.

I takes 10 random songs from your xmms2 library that don't have any rating, and adds them to your current playlist. You can then rate them in another xmms2 client that supports rating (I like kuechenstation).

I'm pretty sure there's a better way to do the grep ... | sed ... part, probably with awk, but I don't know awk, so I'd welcome any suggestions.

disown -a && exit