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,847 results
rename foo bar filename
lynx --dump http://en.trending-topic.com/countries/Mexico/ | grep "62]#" | sed 's/\[62\]//g'
2013-03-12 16:25:14
User: xmuda
Functions: grep sed
-1

In these command i use lynx to get the top trend topic of Mexico, if you replace Mexico with other country, you will get the #1 Trending topic

echo sortmeplease | awk '{l=split($1,a,"");asort(a);while(x<=l){printf "%s",a[x];x++ }print "";}'
2009-09-03 10:22:39
User: foob4r
Functions: awk echo
-1

using awk

missed the last char thanks @Josay

grep -rl string_to_find public_html/css/ | xargs -I '{}' vim +/string_to_find {} -c ":s/string_to_find/string_replaced"
2012-11-07 14:44:51
User: algol
Functions: grep vim xargs
-1

Open all files which have some string go directly to the first line where that string is and run command on it.

Other examples:

Run vim only once with multiple files (and just go to string in the first one):

grep -rl string_to_find public_html/css/ | xargs vim +/string_to_find

Run vim for each file, go to string in every one and run command (to delete line):

grep -rl string_to_find public_html/css/ | xargs -I '{}' vim +/string_to_find {} -c ":delete"
arp-scan -l -g -interface (nic)
2011-01-13 20:29:37
User: pebkac
Functions: arp
-1

This command will scan the subnet and exlude duplicates

cat "log" | grep "text to grep" | awk '{print $1}' | sort -n | uniq -c | sort -rn | head -n 100
sed 's/pattern/^[[1m&^[[0m/g'
perl -ne 'push(@w, length); END {printf "%0d\n" , (sort({$b <=> $a} @w))[0]}' *.cpp
2010-05-11 19:46:37
User: asolkar
Functions: perl
-1

Find the length of the longest line of code in your files.

wget -q ip.nu && cat index.html
cdparanoia -B
2009-07-29 07:56:12
User: hemanth
Functions: cdparanoia
-1

Rip audio tracks from CD to wav files in current dir

<ctrl+z> %1 &
2010-10-25 17:43:38
User: joem86
-1

Often times you run a command in the terminal and you don't realize it's going to take forever. You can open a new terminal, but you lose the local history of the suspended one. You can stop the running command using , but that may produce undesirable side-effects. suspends the job, and (assuming you have no other jobs running in the background) %1 resumes it. Appending & tells it to run in the background.

You now have a job running concurrently with your terminal. Note this will still print any output to the same terminal you're working on.

Tested on zsh and bash.

lshw -C display
2009-04-28 11:49:08
User: gwir
-1

Use the lshw command to display information about your video card. Give more ouput when run as root.

HDD=$(df | awk ' NR>3 (S=$5) (M=$6) { if (S>90) print "Your Systems "M" is """S" Full" } ') ; [[ $HDD ]] && echo "$HDD" | mail -s "Hard-Drives Full" TO@EMAIL.com -- -f FROM@EMAIL.com >/dev/null
sed = <file> | sed 'N;s/\n/\t/'
2009-12-11 14:39:14
User: jgc
Functions: sed
Tags: sed
-1

Print out contents of file with line numbers.

This version will print a number for every line, and separates the numbering from the line with a tab.

curl -u username:password -d status="blah blah blah" https://twitter.com/statuses/update.xml
\<command>
2011-05-06 15:45:31
User: panlm
-1

if you have a alias like this:

alias cp='cp -i'

# cp file1 file1.bak

equal to

# cp -i file1 file1.bak

(it will not overwrite file1.bak if it exist)

# \cp file1 file1.bak

equal to

# /bin/cp file1 file1.bak

(skip alias settings, it will overwrite file1.bak if it exist)

sed '4!d'
2011-09-05 17:54:57
User: totti
Functions: sed
-1

d --> delete

!d ---> delete others

for i in *; do inkscape $i --export-png=`echo $i | sed -e 's/svg$/png/'`; done
lame -h -V 6 track9.wav track9.mp3
2009-05-23 23:27:20
User: dcabanis
-1

The quality ranges between 0 to 9, with the smaller number indicates a higher quality file but bigger too.

file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -i x86_64 |nl |tail -1 |cut -f1 -f3 && file /System/Library/Extensions/*.kext/Contents/MacOS/* |grep -v x86_64 |nl |tail -1 |cut -f1 -f3
2009-09-03 14:28:02
Functions: cut file grep nl tail
-1

Next time you see a mac fanboy bragging about 64-bitness of 10.6 give him this so he might sh?

wmic process where (caption="notepad.exe") call terminate
mailq |awk '/MAILER-DAEMON/{gsub("*","");printf("postsuper -d %s\n",$1)}'|bash
2010-11-24 11:42:54
User: unefunge
Functions: awk mailq
-1

Used it on daily basis, not sure if it's any better than the OPs version, tho'

One advantage is - you can replace 'bash' at the end of the line with eg. cat - to check if the generated command is OK.

perl -ne '$. == 4 && print && exit'
2011-09-05 21:52:16
User: bashrc
Functions: perl
-1

Perl version - just for completeness sake ;)

echo text | sed "s/\(.\)/\1-/g"
2012-03-23 17:18:54
User: ppaschka
Functions: echo sed
-1

Uses Unicode combining characters to produce strikethrough effect. Since commandlinefu doesn't display Unicode properly, you will need to replace the dash in the code above with the Unicode long stroke overlay (U+0336).

mkdir r1 && sshfs remote1:/home/user r1 && rsync r1/stuff remote2:~/backups/
2013-01-11 14:12:22
User: unhammer
Functions: mkdir rsync
Tags: rsync sshfs
-1

rsync by itself doesn't support copying between two remote hosts, but if you use sshfs you can pretend one of them is local. If you have a passphrase-less ssh-key, you can even put this script into a cron job.

A faster alternative is to run ssh-keygen on remote1 and put the pubkey into remote2:~/.ssh/authorized_keys, running rsync on remote1 (or vice versa), but the problem with that is that now a hacker on remote1 can access remote2 at any time. The above method ensures your local computer stays the weak link.