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,617 results
mpg123 "`locate -r '\.mp3$'|awk '{a[NR]=$0}END{print a['"$RANDOM"' % NR]}'`"
2009-02-23 13:53:12
Functions: mpg123
Tags: audio
5

Pick a mp3 at random and play it.

Assumes the availability of locate with an updated db and mpg123

Not the most useful command I guess, but all of the really useful ones are taken...

find ~/Music/iTunes/ -name *.mp3 -exec cp {} ~/Desktop/Music/ \;
rpm -qa --qf '%{name}-%{version}-%{release}.%{arch}\n'|egrep 'compat|glibc|gcc|libst|binu'|sort
2009-02-23 10:17:47
User: ovalerio
Functions: rpm
1

This command is very helpful when we need to duplicate a test scenario and first we want to find out the installed libraries together with the version and release numbers and architecture. (look example)

Command can be tuned by choosing just the names of libraries we are interested in. For example glibc and gcc.

for f in *.ogg ; do mplayer -quiet -vo null -vc dummy -ao pcm:waveheader:file="$f.wav" "$f" ; done
find start_dir -name *.txt | xargs -J % cp % end_dir/
amixer -c 0 set PCM 2dB+
2009-02-22 22:06:46
User: Fate
Functions: amixer set
Tags: audio
-4

this will increase the volume by 2 decibels on the pcm channel. the argument to -c is for which sound card to use, the arg after set is the channel (PCM, Master, etc.) and what to set by.

related commands:

amixer -c 0 set PCM 2dB-

decrease volume by 2 decibels

amixer -c 0 set PCM toggle

toggle mute/unmute

this is for alsa systems

for mac os (and maybe other UNIX systems) osascript -e 'set Volume *' where * is any number (can have decimal points) between 0 and 10

rename 'y/A-Z/a-z/' *
less =rcsyslog
OLD_IFS="$IFS"; IFS=: ARRAY=($PATH); echo ${ARRAY[2]}; IFS="$OLD_IFS"
echo *.log | xargs <command>
2009-02-22 11:32:55
User: mikeda
Functions: echo xargs
5
grep ERROR *.log

-bash: /bin/grep: Argument list too long

echo *.log | xargs grep ERROR /dev/null

20090119.00011.log:DANGEROUS ERROR

read VAR1 VAR2 VAR3 < <(echo aa bb cc); echo $VAR2
ARRAY=(aa bb cc);echo ${ARRAY[1]}
history -r
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
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.