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
grep URL ~/annex/.git/annex/webapp.html | tr -d '">' | awk -F= '{print $4 "=" $5}'
history |awk '{print $3}' |awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c |sort -rn |head -10
du -csh --time *|sort -n|tail
for i in *.JPG; do convert -resize 1000x1000 -quality 85 $i `basename $i .png`-klein.png; done
sed -e 's/ .*//' -e 's/\.//' -e 's/^0*//' /proc/loadavg
2014-04-18 19:12:05
User: flatcap
Functions: sed
5

Show the current load of the CPU as a percentage.

Read the load from /proc/loadavg and convert it using sed:

Strip everything after the first whitespace:

sed -e 's/ .*//'

Delete the decimal point:

sed -e 's/\.//'

Remove leading zeroes:

sed -e 's/^0*//'
top -bn2|awk -F, '/Cpu/{if (NR>4){print 100-gensub(/.([^ ]+).*/,"\\1","g",$4)}}'
2014-04-18 17:48:05
User: ichbins
Functions: awk top
Tags: top cpu cpuinfo
1

This version is precise and requires one second to collect statistics. Check sample output for a more generic version and also a remote computer invocation variant. It doesn't work with the busybox version of the 'top' command but can be adjusted

tar -cf - file1 dir1/ dir2/ | md5sum
2014-04-17 14:33:44
User: snipertyler
Functions: tar
-3

Doesn't create a file

Make sure to list the files / directories in the same order every time.

google() { Q="$@"; GOOG_URL='https://www.google.com/search?q='; AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36" elinks -dump "${GOOG_URL}${Q//\ /+}" | grep "\*" | head -1 }
say() { wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=$1" open output.mp3 &>/dev/null || xdg-open output.mp3 &>/dev/null }
cat dictionary.txt|while read a; do echo $a|cryptsetup luksOpen /dev/sda5 sda5 $a && echo KEY FOUND: $a; done
2014-04-16 18:49:53
User: rodolfoap
Functions: cat echo read
0

Lost your luks passphrase? You can always bruteforce from the command line. See the sample output, a simple command using a dictionary.

for a in {p,P}{a,A,4}{s,S,5}{s,S,5}; do echo $a|cryptsetup luksOpen /dev/sda5 $a && echo KEY FOUND: $a; done
2014-04-16 18:41:50
User: rodolfoap
Functions: echo
0

Lost your luks passphrase? You can always bruteforce from the command line. See the sample output, a simple command for the "pass" word, using combinations of upper/lowercase or number replacement. The generated combinations are:

for a in {p,P}{a,A,4}{s,S,5}{s,S,5}; do echo $a; done

pass

pasS

pas5

paSs

paSS

paS5

...

for id in `ls -1 ~/.ssh | grep -v "authorized\|known_hosts\|config\|\."` ; do echo -n "$id: " ; ssh-keygen -l -f .ssh/$id ; done
2014-04-16 14:12:20
User: drockney
Functions: echo grep id ssh-keygen
0

Find all private keys and dump their fingerprints.

cdn() { cd $(ls -1d */ | sed -n $@p); }
for file in $(find . -name *.mp4); do ogv=${file%%.mp4}.ogv; if test "$file" -nt "$ogv"; then echo $file' is newer then '$ogv; ffmpeg2theora $file; fi done
du -hsx * | sort -rh | head -10
sudo /sbin/route add -host 192.168.12.50 -interface ppp0
2014-04-13 00:17:53
User: jifilis
Functions: sudo
Tags: unix VPN route
0

In this example, 192.168.12.50 is the host that should be routed via the VPN and "ppp0" is the name of the VPN network interface (ifconfig shows you the list of network interfaces). Can be a IP or domain name.

(mountpoint -q "/media/mpdr1" && df /media/mpdr1/* > /dev/null 2>&1) || ((sudo umount "/media/mpdr1" > /dev/null 2>&1 || true) && (sudo mkdir "/media/mpdr1" > /dev/null 2>&1 || true) && sudo mount "/dev/sdd1" "/media/mpdr1")
2014-04-12 11:23:21
User: tweet78
Functions: df mkdir mount sudo umount
20

In my example, the mount point is /media/mpdr1 and the FS is /dev/sdd1

/mountpoint-path = /media/mpdr1

filesystem=/dev/sdd1

Why this command ?

Well, in fact, with some external devices I used to face some issues : during data transfer from the device to the internal drive, some errors occurred and the device was unmounted and remounted again in a different folder.

In such situations, the command mountpoint gave a positive result even if the FS wasn't properly mounted, that's why I added the df part.

And if the device is not properly mounted, the command tries to unmount, to create the folder (if it exists already it will also work) and finally mount the FS on the given mount point.

parallel convert {} {.}.png ::: *.svg
2014-04-12 06:39:02
0

Use GNU Parallel: short, easy to read, and will run one job per core.

sudo tcpdump -i wlan0 -n ip | awk '{ print gensub(/(.*)\..*/,"\\1","g",$3), $4, gensub(/(.*)\..*/,"\\1","g",$5) }' | awk -F " > " '{print $1"\n"$2}'
for w in [WORT1] [WORTn]; do wget -O $w.mp3 $(wget -O - "http://www.duden.de/rechtschreibung/$w" | grep -o "http://www.duden.de/_media_/audio/[^\.]*\.mp3"); done
find . -name \*.svg -print0 | xargs -0 -n1 -P4 -I{} bash -c 'X={}; convert "$X" "${X%.svg}.png"'
2014-04-11 14:30:30
User: flatcap
Functions: bash find xargs
2

Convert some SVG files into PNG using ImageMagick's convert command.

Run the conversions in parallel to save time.

This is safer than robinro's forkbomb approach :-)

xargs runs four processes at a time -P4

echo thisIsATest | sed -r 's/([A-Z])/_\L\1/g'
2014-04-11 13:36:08
User: flatcap
Functions: echo sed
Tags: sed
1

Convert a camelCase string into snake_case.

To complement senorpedro's command.

jkhgkjh; until [[ $? -eq 0 ]]; do YOURCOMMAND; done
2014-04-11 08:19:15
User: moiefu
-1

You want bash to keep running the command until it is successful (until the exit code is 0). Give a dummy command, which sets the exit code to 1 then keep running your command until it exits cleanly

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
2014-04-07 10:07:22
User: opexxx
Functions: head tr
0

This snippet will produce an alpha-numeric 16 character password

find -type f -exec grep -q "regexp" {} \; -delete
2014-04-06 19:06:50
User: gumnos
Functions: find grep
Tags: find grep
3

Deletes files in the current directory or its subdirectories that match "regexp" but handle directories, newlines, spaces, and other funky characters better than the original #13315. Also uses grep's "-q" to be quiet and quit at the first match, making this much faster. No need for awk either.