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,591 results
strings * |grep -v "Apple" |grep http |uniq |sed "s/<[^>]\+>//g"
for i in `grep -ri "?\|?\|?\|?\|?" * --col | cut -d: -f1 |sort -u `;do sed -i "s/?/\&aacute;/g" $i; sed -i "s/?/\&eacute;/g" $i; sed -i "s/?/\&iacute;/g" $i; sed -i "s/?/\&oacute;/g" $i; sed -i "s/?/\&uacute;/g" $i; echo "HTMLizing file [$i]";done
2014-04-24 11:43:36
User: linuxninja
Functions: cut echo sed sort
0

If we've many files containing (?, ?, ?, ?, ? ) characters instead of ?, ?,... etc,...

we can ue this simple command line running a sed command inside a for loop searching for files containing that characters.

Hope u like it! Enjoy! ;)

openssl rand -hex 6 | sed 's/\(..\)/:\1/g; s/^.\(.\)[0-3]/\12/; s/^.\(.\)[4-7]/\16/; s/^.\(.\)[89ab]/\1a/; s/^.\(.\)[cdef]/\1e/'
2014-04-23 21:43:38
User: phil_m
Functions: sed
Tags: Network bash mac
0

The sed script ensures that the "random" MAC address is unicast (bit 0 of first byte == 0) and local (bit 1 of first byte == 1)

for file in /usr/bin/*; do pacman -Qo "$file" &> /dev/null || echo "$file"; done
2014-04-22 21:57:08
User: malathion
Functions: echo file
0

In this example I am returning all the files in /usr/bin that weren't put there by pacman, so that they can be moved to /usr/local/bin where they (most likely) belong.

ruby -rrubygems/commands/mirror_command -S gem mirror
2014-04-22 18:03:01
User: rubiojr
Tags: ruby
2

https://github.com/rubygems/rubygems-mirror/issues/20

tree /dev/disk
getlunid() { lv=$(df -P $1|grep "^/dev/"|awk '{print $1}'|awk -F/ '{print $3}'); hd=$(lslv -l $lv|tail -1|awk '{print $1}');id=$(odmget -q "name like $hd AND attribute=unique_id" CuAt|grep "value ="|awk -F= '{print $2}'|tr -d '"');echo $id;}
2014-04-20 18:43:21
User: bigstupid
Functions: awk df echo grep tail tr
Tags: aix lvm SAN odm
0

For a given filesystem return the LUN ID. Command assumes 1:1 relationship between fs:lv:hdisk:lun which may not be the case in all environments.

: $(cal [$month $year]) ; echo $_
echo -n test@example.com | md5sum | (read hash dash ; echo "https://secure.gravatar.com/avatar/${hash}")
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.