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 - 726 results
psu(){ command ps -Hcl -F S f -u ${1:-$USER}; }
2009-11-13 06:10:33
User: AskApache
Functions: command ps

An easy function to get a process tree listing (very detailed) for all the processes of any gived user.

This function is also in my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

curl -s -c /tmp/cookie -k -u tivo:$MAK --digest http://$tivo/download/$filename | tivodecode -m $MAK -- - | mplayer - -cache-min 50 -cache 65536
flac --best *.wav
2009-11-11 14:17:24
User: nickleus
Tags: bash Ubuntu

cd to the folder containing the wav files, then convert them all to flac. yeah baby!

in ubuntu, to get the flac program just:

sudo apt-get install flac

flac file input formats are wav, aiff, raw, flac, oga and ogg

find . -mmin -60 -not -path "*svn*" -print|more
2009-11-10 18:34:53
User: bloodykis
Functions: find
Tags: bash svn find

Find files recursively that were updated in the last hour ignoring SVN files and folders. Incase you do a full svn up on accident.

read -sn 1 -p 'Press any key to continue...';echo
for c in `seq 0 255`;do t=5;[[ $c -lt 108 ]]&&t=0;for i in `seq $t 5`;do echo -e "\e[0;48;$i;${c}m|| $i:$c `seq -s+0 $(($COLUMNS/2))|tr -d '[0-9]'`\e[0m";done;done
2009-11-03 09:12:13
User: AskApache
Functions: c++ echo

I've been using linux for almost a decade and only recently discovered that most terminals like putty, xterm, xfree86, vt100, etc., support hundreds of shades of colors, backgrounds and text/terminal effects.

This simply prints out a ton of them, the output is pretty amazing.

If you use non-x terminals all the time like I do, it can really be helpful to know how to tweak colors and terminal capabilities. Like:

echo $'\33[H\33[2J'
newest () { candidate=''; for i in "$@"; do [[ -f $i ]] || continue; [[ -z $candidate || $i -nt $candidate ]] && candidate="$i"; done; echo "$candidate"; }
2009-10-29 17:35:01
User: johnraff
Functions: echo
Tags: bash files

Usage example:

newest Desktop/*

Replace "-nt" with "-ot" for oldest.


shopt -s dotglob

first to include dotfiles.

echo {001..5}
2009-10-29 16:25:44
User: nanard06
Functions: echo
Tags: bash

bash2 : for X in $(seq 1 5); do printf "%03g " "$X";done

bash3 : for X in {1..5}; do printf "%03g " "$X";done

bash4 : echo {001..5}

shopt -s cdable_vars
2009-10-26 22:10:56
User: haivu
Tags: bash shell


mydir=/very/long/path/to/a/dir cd mydir

I often need to cd where no man wants to go (i.e. long path). by enabling the shell option cdable_vars, I can tell cd to assume the destination is the name of a variable.

ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub && ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub
2009-10-26 17:52:41
Functions: ssh ssh-keygen
Tags: ssh bash

Get your server's fingerprints to give to users to verify when they ssh in. Publickey locations may vary by distro. Fingerprints should be provided out-of-band.

declare -F | sed 's/^declare -f //'
curl -fs brandx.jp.sme 2&>1 > /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
2009-10-23 14:29:06
User: mccalni
Functions: echo mail ping
Tags: bash ping curl mail

Alternative to the ping check if your firewall blocks ping. Uses curl to get the landing page silently, or fail with an error code. You can probably do this with wget as well.

declare -f [ function_name ]
set | fgrep " ()"
2009-10-22 17:48:54
User: haivu
Functions: fgrep set

If you issue the "set" command, you'll see a list of variables and functions. This command displays just those functions' names.

sed -n '/^function h\(\)/,/^}/p' script.sh
2009-10-19 07:55:35
User: haivu
Functions: sed
Tags: bash sed

I often need to extract a function from a bash script and this command will do it.

argv=("$@"); rest=${argv[@]:1}; less -JMN +"/$1" `grep -l $1 $rest`
2009-10-16 17:36:16
User: lassel
Functions: less
Tags: bash less log grep

Really useful way to combine less and grep while browsing log files.

I can't figure out how to make it into a true oneliner so paste it into a script file called lgrep:


lgrep searchfor file1 [file2 file3]

Advanced example (grep for an Exception in logfiles that starts with qc):

lgrep Exception $(find . -name "qc*.log")

2009-10-15 06:01:18
User: haivu
Tags: bash prompt

For example, if you are the type who type ls very often, then


will ls after every command you issue.

h() { if [ -z "$1" ]; then history; else history | grep "$@"; fi; }
2009-10-13 21:49:37
User: haivu
Functions: grep
Tags: bash grep

Place this in your .bash_profile and you can use it two different ways. If you issue 'h' on its own, then it acts like the history command. If you issue:

h cd

Then it will display all the history with the word 'cd'

ping -q -c1 -w3 brandx.jp.sme 2&>1 /dev/null || echo brandx.jp.sme ping failed | mail -ne -s'Server unavailable' joker@jp.co.uk
2009-10-13 14:13:04
User: mccalni
Functions: echo mail ping
Tags: bash ping mail

Joker wants an email if the Brand X server is down. Set a cron job for every 5 mins with this line and he gets an email when/if a ping takes longer than 3 seconds.

( trap '' 1; ( nice -n 19 sleep 2h && command rm -v -rf /garbage/ &>/dev/null && trap 1 ) & )

Check out the usage of 'trap', you may not have seen this one much. This command provides a way to schedule commands at certain times by running them after sleep finishes sleeping. In the example 'sleep 2h' sleeps for 2 hours. What is cool about this command is that it uses the 'trap' builtin bash command to remove the SIGHUP trap that normally exits all processes started by the shell upon logout. The 'trap 1' command then restores the normal SIGHUP behaviour.

It also uses the 'nice -n 19' command which causes the sleep process to be run with minimal CPU.

Further, it runs all the commands within the 2nd parentheses in the background. This is sweet cuz you can fire off as many of these as you want. Very helpful for shell scripts.

sed -i '19375 s/^/#/' file
2009-10-07 17:50:40
User: TuxOtaku
Functions: sed

This will comment out a line, specified by line number, in a given file.

env PS4=' ${BASH_SOURCE}:${LINENO}(${FUNCNAME[0]}) ' sh -x /etc/profile
su -- user
2009-09-28 04:23:43
User: matthewdavis
Functions: su
Tags: bash root su

I've used this a number of times troubleshooting user permissions. Instead of just 'su - user' you can throw another hyphen and stay in the original directory.

md () { mkdir -p "$@" && cd "$@"; }
2009-09-24 16:09:19
User: drewk
Functions: cd mkdir

How often do you make a directory (or series of directories) and then change into it to do whatever? 99% of the time that is what I do.

This BASH function 'md' will make the directory path then immediately change to the new directory. By using the 'mkdir -p' switch, the intermediate directories are created as well if they do not exist.

grep -RnisI <pattern> *
2009-09-22 15:09:43
User: birnam
Functions: grep
Tags: bash grep

This is how I typically grep. -R recurse into subdirectories, -n show line numbers of matches, -i ignore case, -s suppress "doesn't exist" and "can't read" messages, -I ignore binary files (technically, process them as having no matches, important for showing inverted results with -v)

I have grep aliased to "grep --color=auto" as well, but that's a matter of formatting not function.