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,611 results
who -b
2009-05-19 11:52:54
User: sharfah
Functions: who
15

Displays time of last system boot

sar -r
2009-05-19 11:47:38
User: sharfah
-2

Report memory and swap space utilization statistics e.g. memory free/used, swap free/used

kill -0 [pid]
2009-05-19 11:37:20
User: sharfah
Functions: kill
Tags: kill
5

Send signal 0 to the process. The return status ($?) can be used to determine if the process is running. 0 if it is, non-zero otherwise.

tar cvf - foodir | gzip > foo.tar.gz
2009-05-19 11:33:51
User: sharfah
Functions: gzip tar
Tags: tar gzip
-8

Where foodir is the directory you want to zip up.

gunzip < foo.tar.gz | tar xvf -
curl -d 'username=root&password=your-good-password' "http://router/cgi-bin/luci/admin/system/backup?backup=kthxbye" > `date +%Y%d%m`_config_backup.tgz
2009-05-19 11:32:30
User: GeckoDH
3

You only have to fill in your administrative account and password, and the router FQDN!

I recommand to execute this command not over the internet, because there is no encryption (the username and password will be transmitted in plaintext!)

VARNAMES='ID FORENAME LASTNAME ADDRESS CITY PHONE MOBILE MAIL' ; cat customer.csv | while read LINE ; do COUNT=1 ; for VAR in $VARNAMES ; do eval "${VAR}=`echo $LINE | /usr/bin/awk {'print $'$COUNT''}`" ; let COUNT=COUNT+1 ; done ; done
2009-05-19 11:23:00
User: GeckoDH
Functions: cat eval read
-1

VARNAMES='ID FORENAME LASTNAME ADDRESS CITY PHONE MOBILE MAIL ...'

cat customer.csv | while read LINE ; do

COUNT=1

for VAR in $VARNAMES ; do

eval "${VAR}=`echo $LINE | /usr/bin/awk {'print $'$COUNT''}`"

let COUNT=COUNT+1

done

done

Maybe you have a CSV-File with addresses, where you have to process each contact (one per line, write each value to own variable). Of course you can define every variable, but this way is more simple and faster (to write).

VARNAMES includes the variable names. Pay attention: the number of names in VARNAMES have to be the same than in the CSV-file the fields. If the CSV is not seperated with ";", you can set the seperator after the awk-binary with -F"_" for example.

find . -type d -exec rmdir {} \;
2009-05-19 08:59:15
User: sharfah
Functions: find rmdir
Tags: find rm
1

Deletes empty directories and prints an error if directory is not empty.

TOM=`perl -w -e '@tom=localtime(time+86400);printf "%d%.2d%.2d",$tom[5]+1900,$tom[4]+1,$tom[3];'`
2009-05-19 08:54:27
User: sharfah
Tags: perl
-2

Returns tomorrow's date in the format yyyyMMdd

YEST=`perl -w -e '@yest=localtime(time-86400);printf "%d%.2d%.2d",$yest[5]+1900,$yest[4]+1,$yest[3];'`
2009-05-19 08:54:06
User: sharfah
Tags: perl
-2

Returns yesterday's date in the format yyyyMMdd

INPUT=<input_video> && ffmpeg -i "$INPUT" -vn -f wav - | oggenc -o ${INPUT%%.*}.ogg -
2009-05-19 07:57:43
User: DaveQB
1

Assumes you have ffmpeg and oggenc.

Similar to other scripts here, but this time outputting to Ogg Vorbis.

I added the variable assignment for a nice output name.

This is part of an interactive bash script I have with a few little multimedia tasks in it.

http://www.dward.us/software/VSAK.sh

uuencode archive.tar.gz archive.tar.gz | mail -s "Emailing: archive.tar.gz" user@example.com
2009-05-19 07:17:37
Functions: mail uuencode
5

The uuencode utility will encode your file so that it can be sent as an attachment to an email. It is part of the sharutils package in RHEL/CentOS/Fedora.

mailx bar@foo.com -s "HTML Hello" -a "Content-Type: text/html" < body.htm
2009-05-19 04:49:26
User: ethanmiller
Functions: mailx
Tags: mail smtp html
2

Note, this works because smtp is running

ffmpeg -i filename.flv
2009-05-18 22:57:46
Tags: ffmpeg
12

I used an flv in my example, but it'll work on any file ffmpeg supports. It says it wants an output file, but it tells what you want to know without one.

for f in *.m4a; do ffmpeg -i "$f" "${f%.m4a}.flv"; done
2009-05-18 22:38:49
Tags: ffmpeg flv
2

This converts all m4a files in a dir to flv. You can just swap the m4a bit to anything else ffmpeg supports though, and it'll work.

for f in *.flv; do ffmpeg -y -i "$f" -f image2 -ss 10 -vframes 1 -an "${f%.flv}.jpg"; done
2009-05-18 22:34:59
Tags: ffmpeg jpg flv
12

This is handy for making screenshots of all your videos for referring to in your flv player.

ssh -i $PRIVATEKEY $HOST -C 'cd $SOURCE; tar -cz --numeric-owner .' | tee $DESTINATION/backup.tgz | tar -tz
2009-05-18 20:36:45
User: GeckoDH
Functions: ssh tar tee
1
PRIVATEKEY - Of course the full path to the private key \n HOST - The host where to get the backup \n SOURCE - The directory you wish to backup \n DESTINATION - The destination for the backup on your local machine
date -d2009-05-18 > /dev/null 2>&1 ; echo $?
2009-05-18 20:30:05
User: GeckoDH
Functions: date echo
2

Important to know: a valid date will return 0, otherwise 1!

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.

transcode -i myvideo.avi -x mplayer="-sub myvideo.srt" -o myvideo_subtitled.avi -y xvid
2009-05-18 19:39:51
User: norby
6

It prints myvideo.srt subtitle files in myvideo.avi, saving it in myvideo_subtitled.avi

wget -H -r -nv --level=1 -k -p -erobots=off -np -N --exclude-domains=del.icio.us,doubleclick.net --exclude-directories=
rec -c 2 -r 44100 -s -t wav - | oggenc -q 5 --raw --raw-chan=2 --raw-rate=44100 --raw-bits=16 - > MyLiveRecording.ogg
2009-05-18 15:26:34
User: bohwaz
5

This will record the capture channel of your soundcard, directly encoded in Ogg Vorbis, in stereo at quality 5 (I'm using this to record live jam sessions from my line input). You can choose which device to capture (eg. line input, microphone or PCM output) with

alsamixer -V capture

You can do the same thing and live encode in MP3 or FLAC if you wish, just check FLAC and LAME man pages.

cd ../"$(ls -F ..|grep '/'|grep -B1 `basename $PWD`|head -n 1)"
2009-05-18 06:44:02
User: gwiener
Functions: cd
6

Based on linkinpark342 suggestion.

Sometimes you have to browse your way through a lot of sub-directories. This command cd to the previous sub-directory in alphabetical order. For example, if you have the directories "lectures/01-intro", "lectures/02-basic", "lectures/03-advanced" and so on, and your PWD is "02-basic", it jumps to "01-intro".

cd ../"$(ls -F ..|grep '/'|grep -A1 `basename $PWD`|tail -n 1)"
2009-05-18 06:41:37
User: gwiener
Functions: cd
3

Another version based on linkinpark342's contribution.

Sometimes you have to browse your way through a lot of sub-directories. This command cd to the next sub-directory in alphabetical order. For example, if you have the directories "lectures/01-intro", "lectures/02-basic", "lectures/03-advanced" and so on, and your PWD is "02-basic", it jumps to "03-advanced".

function autoCompleteHostname() { local hosts; local cur; hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1)); cur=${COMP_WORDS[COMP_CWORD]}; COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur )) } complete -F autoCompleteHostname ssh
2009-05-17 23:12:34
User: sbisordi
Functions: awk cut
6

This is meant for the bash shell. Put this function in your .profile and you'll be able to use tab-completion for sshing any host which is in your known_hosts file. This assumes that your known_hosts file is located at ~/.ssh/known_hosts. The "complete" command should go on a separate line as such:

function autoCompleteHostname() {

local hosts=($(awk '{print $1}' ~/.ssh/known_hosts | cut -d, -f1));

local cur=${COMP_WORDS[COMP_CWORD]};

COMPREPLY=($(compgen -W '${hosts[@]}' -- $cur ))

}

complete -F autoCompleteHostname ssh