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 tagged bash from sorted by
Terminal - Commands tagged bash - 725 results
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
kill %1
history | grep ssh
2009-04-03 01:35:52
User: haivu
Functions: grep
Tags: bash csh

After seeing the command you wish to repeat, just invoke it using the ! syntax.

sudo vi /etc/fstab; Go//smb-share/gino /mnt/place smbfs defaults,username=gino,password=pass 0 0<esc>:wq; mount //smb-share/gino
2009-04-02 16:04:35
User: GinoMan2440
Functions: mount sudo vi

the middle command between the ; and ; is the vi commands that insert that line into the last line of the file, the esc with the carets is literally hitting the escape key, you have to have the smbfs package installed to do it, I use it to access my iTunes music on my mac from my linux PC's with amarok so I can play the music anywhere in the house. among other things, it allows you to access the files on that share from your computer anytime you're on that network.

read -p 'Username: ' u;sudo -H -u $u xauth add $(xauth list|grep :$(echo ${DISPLAY: -4:2}));sudo su - $u
echo $( (( $( (2**31 -1) ) - $(date +%s) )) )
2009-04-02 05:14:23
User: Chartreuse
Functions: date echo
Tags: bash echo date

Echos the number of seconds from the current time till the specified time (Example in command is (2**31-1)) aka the Unix epoch. Just replace that number with the specified date (in seconds past Jan. 1st 1970) and it will return the seconds.

NOTE: Only works in bash

while true; do X=$Y; sleep 1; Y=$(ifconfig eth0|grep RX\ bytes|awk '{ print $2 }'|cut -d : -f 2); echo "$(( Y-X )) bps"; done
function svnundopoint() { if [ -d .undo ]; then r=`svn info | grep Revision | cut -f 2 -d ' '` && t=`date +%F_%T` && f=${t}rev${r} && svn diff>.undo/$f && svn stat>.undo/stat_$f; else echo Missing .undo directory; fi }
2009-03-27 07:14:31
User: codeape
Functions: cut echo grep info
Tags: bash svn

Allows you to save progress without committing.

To revert to an undo point, svn revert then apply the undo point with patch.

svn revert -R . && patch -p0 < .undo/2009-03-27_08:08:11rev57

Similar: http://www.commandlinefu.com/commands/view/373/archive-all-files-containing-local-changes-svn

ls /some/directory | sed -rn -e 's/input_file_regex/mv -v & output_file_name/p' | sh
2009-03-25 09:20:15
User: polar
Functions: ls sed
Tags: bash sed

Allows for quick mass renaming, assuming the user has some familiarity with regular expressions. Basically, it replaces the original_file_name in the output of ls with

"mv -v original_file_name new_file_name"

and passes the output to sh.

find -depth . | (while read FULLPATH; do BASENAME=`basename "${FULLPATH}"`; DIRNAME=`dirname "${FULLPATH}"`; mv "${DIRNAME}/${BASENAME}" "${DIRNAME}/${BASENAME// /_}"; done)
2009-03-24 21:04:32
User: mohan43u
Functions: find mv read

Takes filenames and directory names and replace space to '_'.

echo -e "[client]\nuser = YOURUSERNAME\npassword = YOURPASSWORD" > ~/.my.cnf
2009-03-24 19:05:39
User: alperyilmaz
Functions: echo
Tags: mysql bash

The file .my.cnf located at user's home directory is used for mysql login. If this file exists, then


can be replaced with

mysql database -e 'SOME SQL COMMAND'

It saves you from typing!

This is valid for mysqladmin and mysqldump commands as well.

vimdiff foo.c <(bzr cat -r revno:-2 foo.c)
for dir in $(ls); do du -sk ${dir}; done
2009-03-24 13:42:55
User: morlockhq_
Functions: dir du

Sometimes you want to know the summary of the sizes of directories without seeing the details in their subdirectories. Especially if it is going to just scroll off the screen. This one liner summarizes the disk usage of any number of directories in a directory without giving all the details of whats happening underneath.

for i in $(pgrep -v -u root);do kill -9 $i;done
2009-03-24 02:54:52
User: lostnhell
Functions: kill


grep -- displays process ids

-v -- negates the matching, displays all but what is specified in the other options

-u -- specifies the user to display, or in this case negate

The process loops through all PIDs that are found by pgrep, then orders a forced kill to the processes in numerical order, effectively killing the parent processes first including the shells in use which will force the users to logout.

Tested on Slackware Linux 12.2 and Slackware-current

addfunction () { declare -f $1 >> ~/.bashrc ; }
2009-03-23 12:55:04
User: dagh
Tags: bash

Example: To store the function addfunction after you have defined it:

addfunction addfunction
ls | while read ITEM; do echo "$ITEM"; done
2009-03-22 23:33:13
User: fletch
Functions: echo ls read
Tags: bash

If you want to operate on a set of items in Bash, and at least one of them contains spaces, the `for` loop isn't going to work the way you might expect. For example, if the current dir has two files, named "file" and "file 2", this would loop 3 times (once each for "file", "file", and "2"):

for ITEM in `ls`; do echo "$ITEM"; done

Instead, use a while loop with `read`:

ls | while read ITEM; do echo "$ITEM"; done
svn status app/models/foo.rb; svn commit -m "Changed file" !$
2009-03-22 23:14:06
User: ggoodale

After a command is run in bash, !$ is set to the last (space-delimited) argument of the command. Great for running several commands against the same file in a row.