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,610 results
grep ^Dirty /proc/meminfo
2011-08-24 08:48:49
User: h3xx
Functions: grep
30

Ever ask yourself "How much data would be lost if I pressed the reset button?"

Scary, isn't it?

find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01"
2011-06-26 09:52:26
User: flatcap
Functions: find
Tags: find dates
30

Find files in a specific date range - in this case, the first half of last year.

-newermt = modification time of the file is more recent than this date

GNU find allows any date specfication that GNU date would accept, e.g.

find . -type f -newermt "3 years ago" ! -newermt "2 years ago"

or

find . -type f -newermt "last monday"
[enter]~?
2011-06-17 18:29:59
User: xeor
Tags: ssh
30

SSH can be controlled trough an ~ escape sequence. Example, to terminate the current ssh connection, type a newline, then the ~ character, and last a . character.

This is useful eg when an ssh connection hangs after you reboot a machine and the connection hangs.

ssh host -l user $(<cmd.txt)
2010-06-04 17:47:00
User: recursiverse
Functions: host ssh
30

Much simpler method. More portable version: ssh host -l user "`cat cmd.txt`"

ccze
2009-11-23 15:54:33
User: tiagofischer
30

CCZE is a robust and modular log coloriser, with plugins for apm, exim, fetchmail, httpd, postfix, procmail, squid, syslog, ulogd, vsftpd, xferlog and more.

Examples:

tail -f /var/log/messages | ccze -A

tail -f /var/log/exim4/mainlog | ccze -A

lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2
diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
2009-05-21 04:44:29
User: mbirk
Functions: cd diff find
Tags: bash diff find
30

This uses Bash's "process substitution" feature to compare (using diff) the output of two different process pipelines.

leave +15
2009-03-13 06:09:52
User: haivu
Tags: PIM
30

If you spend most of your time in front of the terminal, leave is a useful reminder. Leave can have absolute form: leave 1555 reminds you to leave at 3:55PM

echo | openssl s_client -connect www.google.com:443 2>/dev/null |openssl x509 -dates -noout
2009-02-05 15:21:50
User: systemj
Functions: echo
30

remotely connects to an https site, fetches the ssl certificate and displays the valid dates for the cert

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-") | sudo tee /sys/bus/usb/drivers/usb/unbind
2014-04-06 12:06:29
User: tweet78
Functions: awk cut df echo grep head sudo tail tee tr
29

You have an external USB drive or key.

Apply this command (using the file path of anything on your device) and it will simulate the unplug of this device.

If you just want the port, just type :

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-")

showkey -a
youtube-dl -t --extract-audio --audio-format mp3 YOUTUBE_URL_HERE
2011-11-15 20:11:20
User: menachem
Tags: youtube mp3
29

youtube-dl has this functionality built in. If you're running an older version of youtube-dl, you can update it using `youtube-dl -U` (although if you have an older version, it probably doesn't download youtube videos anyway.)

youtube-dl --help will show you other options that may come in useful.

ls !(*.gz)
2010-07-29 23:47:26
User: c0t0d0
Functions: ls
Tags: ls glob
29

Negative shell globs already come with bash. Make sure to turn on extended pattern matching with 'shopt -e extglob'.

sed -i 8d ~/.ssh/known_hosts
kill -9 $$
2009-03-27 23:13:53
User: stu
Functions: kill
Tags: history exit
29

this exits bash without saving the history. unlike explicitly disabling the history in some way, this works anywhere, and it works if you decide *after* issuing the command you don't want logged, that you don't want it logged

... $$ ( or ${$} ) is the pid of the current bash instance

this also works perfectly in shells that don't have $$ if you do something like

kill -9 `readlink /proc/self`
awk '{$1=$3=""}1' file
2011-10-25 22:15:06
User: zlemini
Functions: awk
28

Print all columns except the 1st and 3rd.

echo ${SSH_CLIENT%% *}
alias dush="du -sm *|sort -n|tail"
2010-03-26 10:18:57
User: funky
Functions: alias
28

sorts the files by integer megabytes, which should be enough to (interactively) find the space wasters. Now you can

dush

for the above output,

dush -n 3

for only the 3 biggest files and so on. It's always a good idea to have this line in your .profile or .bashrc

tar --exclude-vcs -cf src.tar src/
md () { mkdir -p "$@" && cd "$@"; }
2009-09-24 16:09:19
User: drewk
Functions: cd mkdir
28

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.

history -d
2009-04-27 20:19:09
User: sud0er
Tags: bash
28

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.

sudo find / -mmin 60 -type f
2009-03-01 23:03:31
User: atoponce
Functions: find sudo
28

Useful mainly for debugging or troubleshooting an application or system, such as X11, Apache, Bind, DHCP and others. Another useful switch that can be combined with -mmin, -mtime and so forth is -daystart. For example, to find files that were modified in the /etc directory only yesterday:

sudo find /etc -daystart -mtime 1 -type f
:TOhtml
2010-03-26 15:01:34
User: bartonski
27

While editing a source file in vim, or using vimdiff to compare two or more files, the ':TOhtml' command can be used to export each buffer as an html file, including syntax highlighting and vimdiff colorization. If you are in insert mode in vim, you will have to type

:TOhtml

This will open a new buffer filled with html, which you can then save.

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