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 sed from sorted by
Terminal - Commands tagged sed - 322 results
sed 's/^\s*//;s/\s*$//' -i file
2010-08-24 05:03:48
User: kaedenn
Functions: sed
Tags: sed

Sometimes those files have more than just spaces and tabs around them. Plus, this is just a little shorter.

man $(ls /bin | sed -n $((RANDOM % $(ls /bin | wc -l) + 1))p)
2010-08-20 17:15:33
User: putnamhill
Functions: ls man sed wc
Tags: man sed ls wc random

Great idea camocrazed. Another twist would be to display a different man page based on the day of the year. The following will continuously cycle through all man pages:

man $(ls /bin | sed -n $(($(date +%j) % $(ls /bin | wc -l)))p)
dir="/bin"; man $(ls $dir |sed -n "$(echo $(( $RANDOM % $(ls $dir |wc -l | awk "{ print $1; }" ) + 1 )) )p")
2010-08-20 16:31:50
User: camocrazed
Functions: dir ls man sed
Tags: man sed awk echo wc

Broaden your knowledge of the utilities available to you in no particular order whatsoever! Then use that knowledge to create more nifty one-liners that you can post here. =p

Takes a random number modulo the number of files in $dir, prints the filename corresponding to that number, and passes it as an argument to man.

ls | sed 's/.*/"&"/'
2010-08-17 15:38:51
User: putnamhill
Functions: ls sed
Tags: sed ls

Looks like you're stuck with sed if your ls doesn't have a -Q option.

sed -i 's/[ \t]*$//' file
2010-07-28 11:41:34
User: yohananov
Functions: sed
Tags: sed

The command removes all space and/or tabulation characters preceding new line

echo -n "text" | od -A n -t x1 |sed 's/ /\\x/g'
2010-07-14 15:31:36
User: camocrazed
Functions: echo od sed
Tags: sed hex ascii od

If you're going to use od, here's how to suppress the labels at the beginning. Also, it doesn't output the \x, hence the sed command at the end. Remove it for space separated hex values instead

echo -n 'text' | xxd -ps | sed 's/[[:xdigit:]]\{2\}/\\x&/g'
2010-07-13 21:46:30
User: camocrazed
Functions: echo sed
Tags: sed hex ascii

Same as another one I saw, just with a cleaner sed command

Edit: updated the sed command to use the [[:xdigit:]] character class - more portable between locales

Note that it will have a newline inserted after every 32 characters of input, due to the output of xxd

echo "${1}" | egrep '^[[:digit:]]*$' ; if [ "$?" -eq 0 ] ; then sed -i "${1}"d $HOME/.ssh/known_hosts ; else printf "\tYou must enter a number!\n\n" ; exit 1 ; fi
2010-07-11 23:09:11
User: DaveQB
Functions: echo egrep exit printf sed
Tags: sed

I have this as a file called deletekey in my ~/bin.

Makes life a little easier.

ssh-keygen -R <the_offending_host>
2010-07-11 19:37:24
User: bunam
Functions: ssh ssh-keygen
Tags: sed

In this case it's better do to use the dedicated tool

sed -i 8d ~/.ssh/known_hosts
sed -n '4{p;q}'
variable="foo" && sed 's/bar/'$variable'/g' $variable.conf >> $variable.temp && sed '1,5d' $variable.temp && mv $variable.temp $variable.conf
2010-07-09 22:12:51
User: jdorfman
Functions: mv sed
Tags: bash sed nginx

I wrote this script to speed up Nginx configs. This (long) one liner can be run via BASH. You will see that we set a variable in bash called 'foo' and the streamline editor (sed) finds 'bar' in 'foo.conf' next it writes that output to a temp file (foo.temp) and removes the first 5 lines (that aren't needed in this case) & lastly it moves (overwrites) foo.temp to foo.conf

cut -d: -f1 /etc/passwd | sort
sudo lsof|sed 's/ */ /g'|cut -f3 -d' '|sort -u
2010-07-07 08:20:28
User: binaryten
Functions: cut sed sort sudo

Most systems (at least my macbook) have system users defined, such as _www and using "users" for example will not list them. This command allows you to see who the 'virtual' users are on your system.

vim --version | grep -P '^(\+|\-)' | sed 's/\s/\n/g' | grep -Pv '^ ?$'
2010-07-02 02:57:19
User: evaryont
Functions: grep sed vim
Tags: vim sed grep

The above output is for a custom compiled version of Vim on Arch Linux.

Just a quick shell one liner, and presents a list of all the enabled and disabled (those prefixed with a '-') features.

statt(){ C=c;stat --h|sed '/Th/,/NO/!d;/%/!d'|while read l;do p=${l/% */};[ $p == %Z ]&&C=fc&&echo ^FS:^;echo "`stat -$C $p \"$1\"` ^$p^${l#%* }";done|column -ts^; }
2010-06-11 23:31:03
User: AskApache
Functions: column read sed

This shows every bit of information that stat can get for any file, dir, fifo, etc. It's great because it also shows the format and explains it for each format option.

If you just want stat help, create this handy alias 'stath' to display all format options with explanations.

alias stath="stat --h|sed '/Th/,/NO/!d;/%/!d'"

To display on 2 lines:

( F=/etc/screenrc N=c IFS=$'\n'; for L in $(sed 's/%Z./%Z\n/'<<<`stat --h|sed -n '/^ *%/s/^ *%\(.\).*$/\1:%\1/p'`); do G=$(echo "stat -$N '$L' \"$F\""); eval $G; N=fc;done; )

For a similarly powerful stat-like function optimized for pretty output (and can sort by any field), check out the "lll" function


From my .bash_profile ->


alias sorth='sort --help|sed -n "/^ *-[^-]/s/^ *\(-[^ ]* -[^ ]*\) *\(.*\)/\1:\2/p"|column -ts":"'

Once you get into advanced/optimized scripts, functions, or cli usage, you will use the sort command alot. The options are difficult to master/memorize however, and when you use sort commands as much as I do (some examples below), it's useful to have the help available with a simple alias. I love this alias as I never seem to remember all the options for sort, and I use sort like crazy (much better than uniq for example).

# Sorts by file permissions

find . -maxdepth 1 -printf '%.5m %10M %p\n' | sort -k1 -r -g -bS 20%

00761 drwxrw---x ./tmp

00755 drwxr-xr-x .

00701 drwx-----x ./askapache-m

00644 -rw-r--r-- ./.htaccess

# Shows uniq history fast

history 1000 | sed 's/^[0-9 ]*//' | sort -fubdS 50%

exec bash -lxv

export TERM=putty-256color

Taken from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
2010-06-10 20:33:32
User: mitzip
Functions: sed sudo xargs

This will remove all installed kernels on your debian based install, except the one you're currently using.



IFS=`echo -en "\n\b"`; for i in $(curl http://feeds.digg.com/digg/container/technology/popular.rss | grep '<title>' | sed -e 's#<[^>]*>##g' | tail -n10); do echo $i; echo $i | sed 's/^/Did you hear about /g' | say; sleep 30; done
2010-06-07 22:16:19
User: echosedawk
Functions: echo grep sed sleep tail
Tags: bash sed curl osx

Instead of having someone else read you the Digg headlines, Have OSX do it. Requires Curl+Sed+Say. This could probably be easily modified to use espeak for Linux.

find ~/.thunderbird/*.default/ -name *.msf | sed 's/ /\\ /g' | xargs rm {} \;
2010-06-04 12:35:24
User: allrightname
Functions: find rm sed xargs

The thunderbird message datastores get corrupt some times causing random failures, compaction to fail and general suck in thunderbird. Removing them causes thunderbird to rebuild the indexes and makes things quick again.

ls -rl --time-style=+%s * | sed '/^$/,/^total [0-9]*$/d' | sort -nk6
sed -n 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/\nip&\n/gp' ips.txt | grep ip | sed 's/ip//'| sort | uniq
2010-05-23 11:26:32
User: rubenmoran
Functions: grep sed sort
Tags: sed ip address

Extracts ip addressess from file using sed. Uses a tag(ip) to grep the IP lines after extracting. Must be a way to just output regex matched on sed.

sortwc () { local L;while read -r L;do builtin printf "${#L}@%s\n" "$L";done|sort -n|sed -u 's/^[^@]*@//'; }
2010-05-20 20:13:52
User: AskApache
Functions: printf read sed sort

This provides a way to sort output based on the length of the line, so that shorter lines appear before longer lines. It's an addon to the sort that I've wanted for years, sometimes it's very useful. Taken from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

command ps wwo pid,user,group,vsize:8,size:8,sz:6,rss:6,pmem:7,pcpu:7,time:7,wchan,sched=,stat,flags,comm,args k -vsz -A|sed -u '/^ *PID/d;10q'

I've wanted this for a long time, finally just sat down and came up with it. This shows you the sorted output of ps in a pretty format perfect for cron or startup scripts. You can sort by changing the k -vsz to k -pmem for example to sort by memory instead.

If you want a function, here's one from my http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

aa_top_ps(){ local T N=${1:-10};T=${2:-vsz}; ps wwo pid,user,group,vsize:8,size:8,sz:6,rss:6,pmem:7,pcpu:7,time:7,wchan,sched=,stat,flags,comm,args k -${T} -A|sed -u "/^ *PID/d;${N}q"; }
find . -type f -not -regex ".*\/.svn\/.*" -exec sed -i 's/oldstring/newstring/g' {} +