Hide

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.

Hide

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:

Hide

News

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.
Hide

Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 11,847 results
lsof -p 15857
find /usr/ -type l ! -xtype f ! -xtype d -ok rm -f {} \;
xmlstarlet ed -d '//comment()' $XML_FILE
2009-04-16 23:00:47
User: minliu2k
Functions: ed
2

In Redhat, xmlstarlet is called just xml which can be found in xmlstarlet RPM.

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
3

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.

sort -u filename > newfilename
2009-04-16 20:00:34
User: deijmaster
Functions: sort
-7

For users looking to simplify management of large entries in files and directories, this command is the key to fun and simplicity. Using the power sort, only a couple of seconds are necessary to accomplish what would take minutes or hours in ?standard? client applications.

svn list -R https://repository.com --xml >> svnxxmlinfo.xml
2009-04-16 18:58:18
User: iheartbash
4

I use this to pull the last commit date for everything in my repo, so I can tell the client which files haven't been touched or updated since the repo was created. Another way to do it is to use svn log, but that does not pull the "kind" attribute. It does, however, give you the commit message. Both are very useful.

ffmpeg -i movie.avi -y -f vcd -vcodec mpeg1video -map 0.0:0.0 -b 1150 -s 352x240 -r 29.97 -g 12 -qmin 3 -qmax 13 -acodec mp2 -ab 224 -ar 44100 -ac 2 -map 0.1:0.1 movie.mpg
lsof -i tcp:80
2009-04-16 14:51:53
User: ar_levi
32

Sometimes you need to use a port that is already opened by some program , and you don't know who to "kill" for it to release - so, now you do !

find /path/to/my/files/ -type f -name "*txt*" | xargs du -k | awk 'BEGIN{x=0}{x=x+$1}END{print x}'
2009-04-16 14:17:04
Functions: awk du find xargs
2

Use the find command to match certain files and summarise their total size in KBytes.

locate searchstring | xargs grep foo
2009-04-16 12:51:24
User: zimon
Functions: grep locate xargs
Tags: grep locate
-3

Greps located files for an expression.

Example greps all LaTeX files for 'foo':

locate *.tex | xargs grep foo

To avoid searching thousands of files with grep it could be usefull to test first how much files are returned by locate:

locate -c *.tex
sed '/^/R addfile' targetfile > savefile
2009-04-16 05:11:07
User: tobiasboon
Functions: sed
1

After every line in targetfile (empty lines included) insert in a line from addfile. "Save" results to savefile. Addfile should be longer than targetfile since this doesn't loop back to the top of addfile.

/^/R addfile -- says for every line that matches "has a start of line" output a line from the file addfile.

> savefile (optional) -- redirect output to savefile file.

awk '/sshd/ && /Failed/ {gsub(/invalid user/,""); printf "%-12s %-16s %s-%s-%s\n", $9, $11, $1, $2, $3}' /var/log/auth.log
2009-04-16 00:56:23
User: frailotis
Functions: awk printf
11

A variation of a script I found on this site and then slimmed down to just use awk. It displays all users who have attempted to login to the box and failed using SSH. Pipe it to the sort command to see which usernames have the most failed logins.

x0vnc4server -display :0 -PasswordFile ~/.vnc/passwd
egrep -ci ^[aoeuidhtns-]+$ /usr/share/dict/words
2009-04-15 20:31:46
User: atoponce
Functions: egrep
5

Quick and dirty command that counts how many words can be typed just using the home row on the Dvorak Simplified Keyboard layout from a dictionary file, in this case /usr/share/dict/words.

According to the regular expression supplied, each word must contain all the keys on the Dvorak home row, and no other characters. For comparison, I've shown how many words are installed in my dictionary, how many can be typed with just the Dvorak home row and how many can be typed with just the QWERTY home row in the sample output. Nearly 10 times the amount.

If you want to see the words, remove the -c switch, and each word will be printed out.

cat <filename> | perl -e '$/ = ""; $_ = <>; s/<!--.*?-->//gs; print;'
2009-04-15 20:29:11
User: unixx
Functions: cat perl
0

xml with verbose commenting can be difficult to read. remove comments from xml.

echo -e "aa\nbb\ncc\ndd\n123" | sed -e :a -e "/$/N; s/\n/;/; ta"
wget -q -O - "$@" <url>
echo "12345,12346" |sed -e's/ //'|tr "," "\n"| while read line; do echo -n $line" "; svn log -vr $line https://url/to/svn/repository/|grep "/"|head -1|cut -d"/" -f2; done
date -r foo
ufraw-batch --out-type=jpeg --out-path=./jpg ./*.NEF
2009-04-15 09:03:59
User: sudopeople
Tags: photo photos
6

converts RAW files from a Nikon DSLR to jpg for easy viewing etc.

requires ufraw package

sudo cat /dev/vcs1 | fold -w 80
2009-04-15 08:49:48
User: animoid
Functions: cat fold sudo
16

This will view the console and assumes the screen is 80 characters wide.

Use /dev/vcs2 for the next virtual console.. etc.

/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -1
echo 'wget url' | at 12:00
convert images*.* <my_pdf>.pdf
watch "dmesg |tail -15"
2009-04-14 03:13:17
User: Buzzcp
Functions: watch
11

Other logs can be monitored similarly, e.g.

watch "tail -15 /var/log/daemon.log"