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,925 results
pdftotext [file] - | grep 'YourPattern'
2010-02-14 21:42:35
User: drewk
Functions: grep
Tags: pipe grep pdf
27

PDF files are simultaneously wonderful and heinous. They are wonderful in being ubiquitous and mostly being cross platform. They are heinous in being very difficult to work with from the command line, search, grep, use only the text inside the PDF, or use outside of proprietary products.

xpdf is a wonderful set of PDF tools. It is on many linux distros and can be installed on OS X. While primarily an open PDF viewer for X, xpdf has the tool "pdftotext" that can extract formated or unformatted text from inside a PDF that has text. This text stream can then be further processed by grep or other tool. The '-' after the file name directs output to stdout rather than to a text file the same name as the PDF.

Make sure you use version 3.02 of pdftotext or later; earlier versions clipped lines.

The lines extracted from a PDF without the "-layout" option are very long. More paragraphs. Use just to test that a pattern exists in the file. With "-layout" the output resembles the lines, but it is not perfect.

xpdf is available open source at http://www.foolabs.com/xpdf/

grep . filename > newfilename
lshw -html > hardware.html
2009-06-04 07:14:03
User: flart
27

After the command is done, open the html file in a browser

mkfifo /tmp/fifo; cat /tmp/fifo | nc -l -p 1234 | tee -a to.log | nc machine port | tee -a from.log > /tmp/fifo
2009-02-20 04:00:15
User: prutser
Functions: cat mkfifo tee
27

Forwards localhost:1234 to machine:port, running all data through your chain of piped commands. The above command logs inbound and outbound traffic to two files.

Tip: replace tee with sed to manipulate the data in real time (use "sed -e 's/400 Bad Request/200 OK/'" to tweak a web server's responses ;-) Limitless possibilities.

mysqldump --add-drop-table --extended-insert --force --log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME"
2009-02-17 17:57:53
User: Tscherno
Functions: ssh
27

Dumps a MySQL database over a compressed SSH tunnel and uses it as input to mysql - i think that is the fastest and best way to migrate a DB to a new server!

for file in `ls *.pdf`; do convert -verbose -colorspace RGB -resize 800 -interlace none -density 300 -quality 80 $file `echo $file | sed 's/\.pdf$/\.jpg/'`; done
2009-02-15 23:27:43
User: brettalton
Functions: file sed
27

(relies on 'imagemagick')

This command will convert all .pdf files in a directory into a 800px (wide or height, whichever is smaller) image (with the aspect ratio kept) .jpg.

If the file is named 'example1.pdf' it will be named 'example1.jpg' when it is complete.

This is a VERY worthwhile command! People pay hundreds of dollars for this in the Windows world.

My .jpg files average between 150kB to 300kB, but your's may differ.

tidy -xml -i -m [file]
2009-02-05 21:22:33
User: scw
27

Generated XML files often are poorly formatted. Use this command to properly indent and normalize the file in-place.

<ctrl+z> fg; notify_me
2010-05-20 16:16:43
User: recursiverse
26

If you want to be notified when a long-running command is finished, but you have already started it:

CTRL+Z

fg; echo "finished" | sendmail me@example.com

I use a script to post a tweet, which sends me a txt:

fg; echo "finished" | tweet
alias head='head -n $((${LINES:-`tput lines 2>/dev/null||echo -n 12`} - 2))'
26

Run the alias command, then issue

ps aux | head

and resize your terminal window (putty/console/hyperterm/xterm/etc) then issue the same command and you'll understand.

${LINES:-`tput lines 2>/dev/null||echo -n 12`}

Insructs the shell that if LINES is not set or null to use the output from `tput lines` ( ncurses based terminal access ) to get the number of lines in your terminal. But furthermore, in case that doesn't work either, it will default to using the deafault of 12 (-2 = 10).

The default for HEAD is to output the first 10 lines, this alias changes the default to output the first x lines instead, where x is the number of lines currently displayed on your terminal - 2. The -2 is there so that the top line displayed is the command you ran that used HEAD, ie the prompt.

Depending on whether your PS1 and/or PROMPT_COMMAND output more than 1 line (mine is 3) you will want to increase from -2. So with my prompt being the following, I need -7, or - 5 if I only want to display the commandline at the top. ( http://www.askapache.com/linux-unix/bash-power-prompt.html )

275MB/748MB

[7995:7993 - 0:186] 06:26:49 Thu Apr 08 [askapache@n1-backbone5:/dev/pts/0 +1] ~

In most shells the LINES variable is created automatically at login and updated when the terminal is resized (28 linux, 23/20 others for SIGWINCH) to contain the number of vertical lines that can fit in your terminal window. Because the alias doesn't hard-code the current LINES but relys on the $LINES variable, this is a dynamic alias that will always work on a tty device.

ssh root@server.com 'tshark -f "port !22" -w -' | wireshark -k -i -
2009-12-17 23:03:24
User: markdrago
Functions: ssh
26

This captures traffic on a remote machine with tshark, sends the raw pcap data over the ssh link, and displays it in wireshark. Hitting ctrl+C will stop the capture and unfortunately close your wireshark window. This can be worked-around by passing -c # to tshark to only capture a certain # of packets, or redirecting the data through a named pipe rather than piping directly from ssh to wireshark. I recommend filtering as much as you can in the tshark command to conserve bandwidth. tshark can be replaced with tcpdump thusly:

ssh root@example.com tcpdump -w - 'port !22' | wireshark -k -i -
nl
killall -STOP -m firefox
2009-05-18 20:02:44
Functions: killall
26

Continue with:

killall -CONT -m firefox

Suspends all Firefox Threads. Results in Zero CPU load.

Useful when having 100+ Tabs open and you temporarily need the power elsewhere.

Be careful - might produce RACE CONDITIONS or LOCKUPS in other processes or FF itself.

matching is case sensitive.

:mksession! <filename>
2009-02-26 05:03:07
User: neutral
Tags: vim
26

Creates a full snapshot of your current vim session, including tabs, open buffers, cursor positions, everything. Can be resumed with vim -S . Useful for those times when you HAVE to close vim, but you don't want to lose all your hard-opened buffers and windows. The ! will cause vim to overwrite the file if it already exists. It is not necessary, but useful if you frequently save to the same file (like session.vim or something).

svn log -q|grep "|"|awk "{print \$3}"|sort|uniq -c|sort -nr
2009-02-17 21:37:03
User: psytek
26

Use this command to find out a list of committers sorted by the frequency of commits.

<COMMAND> |:
2011-08-28 23:48:29
User: h3xx
25

This is shorter and actually much faster than >/dev/null (see sample output for timings)

Plus, it looks like a disappointed face emoticon.

bind -x '"\C-l":ls -l'
2010-12-25 22:55:33
User: roalddevries
25

the -x option is for binding to a shell command

python -m http.server
2010-12-17 12:52:45
User: Alanceil
Functions: python
25

In Python version 3, the module was merged into http.server. Gentlemen, change your aliases.

DISPLAY=:0.0 import -window root /tmp/shot.png
2010-10-28 12:00:00
User: depesz
25

Of course it requires import command, from imagemagick tools, but it's simpler to type, and imagemagick is usefull anyway.

lsof -Pan -i tcp -i udp
2010-06-07 15:22:44
User: atoponce
Tags: netstat lsof
25

This command is more portable than it's cousin netstat. It works well on all the BSDs, GNU/Linux, AIX and Mac OS X. You won't find lsof by default on Solaris or HPUX by default, but packages exist around the web for installation, if needed, and the command works as shown. This is the most portable command I can find that lists listening ports and their associated pid.

vi +/pattern [file]
2010-04-24 22:15:12
User: punkwalrus
Functions: vi
Tags: vim edit vi
25

Open up vi or vim at the first instance of a pattern in [file]. Useful if you know where you want to be, like "PermitRootLogin" in sshd_config. Also, vi +10 [file] will open up a file at line 10. VERY useful when you get "error at line 10" type of output.

apt-get install most && update-alternatives --set pager /usr/bin/most
2010-01-04 14:13:55
User: aurium
Functions: apt install
25

That command installs "most" and make this command as the default man reader. The "most" works like "less" (the current man reader), but it render colors for manpages and may do more things. Read "man most".

You can see a preview here: http://www.dicas-l.com.br/dicas-l/20090718.php

tar -cf - . | pv -s $(du -sb . | awk '{print $1}') | gzip > out.tgz
2009-12-18 17:09:08
User: opertinicy
Functions: awk du gzip tar
25

What happens here is we tell tar to create "-c" an archive of all files in current dir "." (recursively) and output the data to stdout "-f -". Next we specify the size "-s" to pv of all files in current dir. The "du -sb . | awk ?{print $1}?" returns number of bytes in current dir, and it gets fed as "-s" parameter to pv. Next we gzip the whole content and output the result to out.tgz file. This way "pv" knows how much data is still left to be processed and shows us that it will take yet another 4 mins 49 secs to finish.

Credit: Peteris Krumins http://www.catonmat.net/blog/unix-utilities-pipe-viewer/

some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr)
2009-12-01 03:58:04
User: tylerl
25

You can use [n]> combined with >(cmd) to attach the various output file descriptors to be the input of different commands.

date -d @1234567890
( ( sleep 2h; your-command your-args ) & )
2009-08-19 17:39:11
User: sitaram
Functions: sleep
25

doesn't require "at", change the "2h" to whatever you want... (deafult unit for sleep is seconds)