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,605 results
reset
2009-02-22 01:24:14
User: Fate
Functions: reset
0

after a terminal gets clobbered (like after you cat a binary file by accident), this is the only way to get it back without re-starting it.

diff3 -a file1 file2 file3
2009-02-21 21:52:46
User: JollyJumper
Functions: diff3
-8

It takes a hunk and shows the different between the three file. Useful when you want to compare two different changed file which from the same base file.

(use emacs ediff3, eyecandy and more useful if you want to merge them from anywhere to anywhere)

setfacl -m u:john:r-- myfile
2009-02-21 21:05:45
User: betsubetsu
2

The file myfile is owned by tom and has read and write permissions for tom. Group and other permissions are empty which make myfile readable and writable only by tom. setfacl enables user tom to give read permission to user john only. The command 'ls -l' shows a '+' sign telling us that file access control list has been setup for myfile.

function encode4phone() { ffmpeg -acodec libamr_nb -vcodec h263 -i "$1" -s qcif -b 200k -ac 1 -ab 7.4k -ar 8000 "$1.3gp" }
grep --color=auto -iRnH "$search_word" $directory
2009-02-21 19:16:33
User: tobiasboon
Functions: grep
12

greps for search word in directory and below (defaults to cd).

-i case insensitive

-n shows line number

-H shows file name

FILE="`date +%m%d%H%M%S`.png"; URL="http://YOUR_HOST/YOUR/PATH/$FILE"; TMP="/tmp/$FILE"; import -frame $TMP; scp $TMP YOUR-USER@YOUR-HOST:/YOUR/PATH/; rm $TMP; firefox "$URL"
2009-02-21 18:42:33
User: TheBunman
Functions: rm scp
2

I often need to send screenshots to other people to explain settings and whatever.

So I created this oneline which I use to create the screenshot with imagemagik, upload it via scp to my server and then the command opens an firefox tab with the screenshot.

The screenshot can be a region or a window.

You just have to replace the parts beginning with YOUR.

date -d '2 weeks ago Saturday' +%Y-%m-%d
2009-02-21 16:42:52
User: NPH
Functions: date
15

Good for automating reports that need to run from between two dates.

sed -e "s/| /\n/g" ~/.bash_history | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head
ssh -A user@somehost
2009-02-21 16:01:36
User: f4nt
Functions: ssh
6

the -A argument forwards your ssh private keys to the host you're going to. Useful in some scenarios where you have to hop to one server, and then login to another using a private key.

find /path/to/files -type f -mtime +7 | grep -v \.gz | xargs gzip
ps -ef | grep PROCESS | grep -v grep | awk '{print $2}' | xargs kill -9
2009-02-21 15:55:38
User: f4nt
Functions: awk grep kill ps xargs
4

kills all pids matching the search term of "PROCESS". Be careful what you wish for :)

echo -en "stats\r\n" "quit\r\n" | nc localhost 11211 | tr -s [:cntrl:] " "| cut -f42,48 -d" " | sed "s/\([0-9]*\)\s\([0-9]*\)/ \2\/\1*100/" | bc -l
for x in `psql -e\l | awk '{print $1}'| egrep -v "(^List|^Name|\-\-\-\-\-|^\()"`; do pg_dump -C $x | gzip > /var/lib/pgsql/backups/$x-nightly.dmp.gz; done
2009-02-21 15:21:09
User: f4nt
Functions: awk egrep gzip
1

Ran as the postgres user, dumps each database individually. It dumps with the create statements as well, so you can just 'zcat $x-nightly.dmp.gz | psql' to reimport/recreate a database from a backup.

find . -type f -mtime +7 -exec ls -l {} \;
2009-02-21 08:03:24
User: senses0
Functions: find ls
5

Find files that are older than x days in the working directory and list them. This will recurse all the sub-directories inside the working directory.

By changing the value for -mtime, you can adjust the time and by replacing the ls command with, say, rm, you can remove those files if you wish to.

N="filepath" ; P=/proc/$(lsof +L1 | grep "$N" | awk '{print $2}')/fd ; ls -l $P | sed -rn "/$N/s/.*([0-9]+) ->.*/\1/p" | xargs -I_ cat $P/_ > "$N"
2009-02-21 02:31:24
User: laburu
Functions: awk cat grep ls sed xargs
5

Note that the file at the given path will have the contents of the (still) deleted file, but it is a new file with a new node number; in other words, this restores the data, but it does not actually "undelete" the old file.

I posted a function declaration encapsulating this functionality to http://www.reddit.com/r/programming/comments/7yx6f/how_to_undelete_any_open_deleted_file_in_linux/c07sqwe (please excuse the crap formatting).

doloop() { DONT=/tmp/do-run-run-run; while true; do touch $DONT; (sleep 30; rm $DONT;) & $1 ; if [ -e $DONT ]; then echo restarting too fast; return ; fi ; done }
2009-02-21 02:11:18
User: evil_otto
Functions: echo return rm sleep touch
-1

This runs a command continuously, restarting it if it exits. Sort of a poor man's daemontools. Useful for running servers from the command line instead of inittab.

"some line input" | sort | uniq -c | sort -nr
for i in `ps aux | grep ssh | grep -v grep | awk {'print $2'}` ; do kill $i; done
a() { alias $1=cd\ $PWD; }
2009-02-21 01:53:01
Functions: alias
0

An easy way to create aliases for moving between many directories

curl -s -u username:passwd http://twitter.com/statuses/friends_timeline.rss|grep title|sed -ne 's/<\/*title>//gp' |festival --tts
alias mine='ps xco pid,command,%cpu,%mem,state'
ssh -t user@some.domain.com /usr/bin/screen -xRR
2009-02-20 23:39:54
User: olifante
Functions: ssh
27

Long before tabbed terminals existed, people have been using Gnu screen to open many shells in a single text terminal. Combined with ssh, it gives you the ability to have many open shells with a single remote connection using the above options. If you detach with "Ctrl-a d" or if the ssh session is accidentally terminated, all processes running in your remote shells remain undisturbed, ready for you to reconnect. Other useful screen commands are "Ctrl-a c" (open new shell) and "Ctrl-a a" (alternate between shells). Read this quick reference for more screen commands: http://aperiodic.net/screen/quick_reference

alias timestamp='date "+%Y%m%dT%H%M%S"'
2009-02-20 23:18:30
User: olifante
Functions: alias
12

I often need to add a timestamp to a file, but I never seem to remember the exact format string that has to be passed to the date command to get a compact datetime string like 20090220T231410 (i.e yyyymmddThhmmss, the ISO 8601 format popular outside the US)

Play "foo.mpg" in your terminal using ASCII characters
2009-02-20 22:12:14
User: chrisclymer
1

mplayer -vo caca will give you a similar result but in color

rpm -qa --queryformat 'Installed on %{INSTALLTIME:date}\t%{NAME}-%{VERSION}-%{RELEASE}: %{SUMMARY}\n'