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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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



All commands from sorted by
Terminal - All commands - 12,413 results
grep '' -m X file1 > file2
2009-03-22 04:34:43
User: sardanapalos
Functions: grep

It extracts X number of lines from file1 and dumps them to file2.Using grep with the empty string '' extracts the complete lines (i.e. no filtering takes place) and the -m flag indicates how many lines to extract out from the given file. Then using the redirect > operator we send the extracted lines to a new file.

for i in {1..15}; do echo $i; done
2009-03-21 23:08:41
User: haivu
Functions: echo
Tags: bash

The brace expansion also allows you to count backward:

for i in {15..1}; do echo $i; done

You can also use this construct to create new file or new directory:

mkdir dir{1..3} # Same as mkdir dir1 dir2 dir3

mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=dbname &
2009-03-21 18:45:15
User: root
Functions: awk
Tags: mysql

This loops through all tables and changes their collations to UTF8. You should backup beforehand though in case some data is lost in the process.

watch -n 1 uptime\;myqladmin --user=<user> --password=<password> --verbose processlist
2009-03-21 18:29:28
User: root
Functions: watch
Tags: mysql

Useful for monitoring both MySQL and the server load at the same time.

cat file.txt | sort | uniq -dc
2009-03-21 18:15:14
User: Vadi
Functions: cat sort uniq

Displays the duplicated lines in a file and their occuring frequency.

j mp3
sudo zcat /var/log/auth.log.*.gz | awk '/Failed password/&&!/for invalid user/{a[$9]++}/Failed password for invalid user/{a["*" $11]++}END{for (i in a) printf "%6s\t%s\n", a[i], i|"sort -n"}'
2009-03-21 06:41:59
Functions: awk printf sudo zcat

Show the number of failed tries of login per account. If the user does not exist it is marked with *.

ls -hog
2009-03-21 05:24:49
User: haivu
Functions: ls
Tags: shell

I often deal with long file names and the 'ls -l' command leaves very little room for file names. An alternative is to use the -h -o and -g flags (or together, -hog).

* The -h flag produces human-readable file size (e.g. 91K instead of 92728)

* The -o suppresses the owner column

* The -g suppresses the group column

Since I use to alias ll='ls -l', I now do alias ll='ls -hog'

time read (ctrl-d to stop)
2009-03-20 22:50:06
User: mrttlemonde
Functions: read time

time read -sn1 (s:silent, n:number of characters. Press any character to stop)

ls -1 static/images/ | while read line; do echo -n $line' '[; grep -rc $line *|grep -v ".svn"|cut -d":" -f2|grep -vc 0| tr "\n" -d; echo -n ]; echo ; done
2009-03-20 20:33:36
User: psytek
Functions: cut echo grep ls read tr

This command will grep the entire directory looking for any files containing the list of files. This is useful for cleaning out your project of old static files that are no longer in use. Also ignores .svn directories for accurate counts. Replace 'static/images/' with the directory containing the files you want to search for.

curl -s -u user:password http://twitter.com/statuses/friends_timeline.rss | grep title | sed -ne 's/<\/*title>//gp' | head -n 4 | say -v Bruce
2009-03-20 20:14:13
User: sixtus
Functions: grep head sed

You need curl.. and a Mac of course.

xvkbd -xsendevent -text "Hello world"
2009-03-20 18:58:05
User: Alanceil

This is a (last resort) way to automate applications that provide no other ways for automation, it would send 'Hello world' to the currently active window. See the manpage (and the -text and -window entries) for how to send special characters and target specific windows.

An example:

Using xwininfo, I get the id of my XPlanet background window:

[email protected]:19:51:0:~> xwininfo

xwininfo: Please select the window about which you

would like information by clicking the

mouse in that window.

xwininfo: Window id: 0x3600001 "Xplanet 1.2.0"

Absolute upper-left X: 0


Now I use xvkbd to tell it to close itself:

xvkbd -xsendevent -window 0x3600001 -text "Q"

Obviously, the best way is to put these commands in a shellscript - just make sure to include a short sleep (sleep .1 should suffice) after each xvkbd call, or some programs will become confused.

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> -DgeneratePom=true
2009-03-20 16:29:08
User: virtualshock
Tags: maven java

If you have a JAR that is not in a central Maven repository this will add it to your repository.

set -o vi
2009-03-20 16:29:03
User: Viaken
Functions: set
Tags: readline vi

This lets you use your favorite vi edit keys to navigate your term.

To set it permanently, put "set editing-mode vi" in your ~/.inputrc or /etc/inputrc.

fc [history-number]
2009-03-20 15:09:43
User: haivu
Functions: fc
Tags: bash

If you would like to edit a previous command, which might be long and complicated, you can use the fc (I think it stands for fix command). Invoke fc alone will edit the last command using the default editor (specified by $FCEDIT, $EDITOR, or emacs, in that order). After you make the changes in the editor, save and exit to execute that command. The fc command is more flexible than what I have described. Please 'man bash' for more information.

2009-03-20 14:50:25
User: haivu
Tags: bash

CDPATH tells the cd command to look in this colon-separated list of directories for your destination. My preferred order are 1) the current directory, specified by the empty string between the = and the first colon, 2) the parent directory (so that I can cd lib instead of cd ../lib), 3) my home directory, and 4) my ~/projects directory.

watch 'iptables -vL'
2009-03-20 14:49:12
User: alanr723
Functions: watch

Watch the number of packets/bytes coming through the firewall. Useful in setting up new iptables rules or chains. Use this output to reorder rules for efficiency.

stty -F "/dev/ttyUSB0" 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke time 5 min 1 line 0
2009-03-20 14:48:32
User: Alanceil
Functions: stty time

I had a hard time in finding the correct settings to get reasonable output from a coin selector which sends its data over a serial line. In the end, minicom came to the rescue and pointed me on the right track.

So, if you need to do something similar, these settings may help you.

Replace ttyUSB0 with your device file, 9600 with your baud rate, 5 with your read timeout (10ths of a second), and 1 with the minimum numbers of characters you want to read.

You can then open the device file like you are used to do, example:

DATA="`xxd -ps -l 5 \"$DEV\"`"
sed /BEGIN/,/END/s/xxx/yyy/g input.txt
2009-03-20 14:34:32
User: kayowas
Functions: sed

Replace the words between BEGIN and END.

mount | column -t
2009-03-20 14:18:56
User: thechile
Functions: column mount

Particularly useful if you're mounting different drives, using the following command will allow you to see all the filesystems currently mounted on your computer and their respective specs with the added benefit of nice formatting.

nawk '{line[NR]=$0} END{for (; NR>=1; NR--){print line[NR]}}' FILENAME
echo ${file##*/}
ntpdate ntp.ubuntu.com pool.ntp.org
ls -l | sort -nk5
'ALT+.' or '<ESC> .'
2009-03-20 11:36:04
User: atoponce

When typing out long arguments, such as:

cp file.txt /var/www/wp-content/uploads/2009/03/

You can put that argument on your command line by holding down the ALT key and pressing the period '.' or by pressing <ESC> then the period '.'. For example:

cd 'ALT+.'

would put '/var/www/wp-content/uploads/2009/03/ as my argument. Keeping pressing 'ALT+.' to cycle through arguments of your commands starting from most recent to oldest. This can save a ton of typing.