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,621 results
wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world
2011-03-08 14:05:36
User: sairon
Functions: wget
35

EDIT: command updated to support accented characters!

Works in any of 58 google supported languages (some sound like crap, english is the best IMO).

You get a mp3 file containing your query in spoken language. There is a limit of 100 characters for the "q" parameter, so be careful. The "tl" parameter contains target language.

curl ifconfig.me
find . -type d -empty -delete
diff <(lsof -p 1234) <(sleep 10; lsof -p 1234)
2010-03-15 22:55:32
User: zlemini
Functions: diff sleep
35

This command takes a snapshot of the open files for a PID 1234 then waits 10 seconds and takes another snapshot of the same PID, it then displays the difference between each snapshot to give you an insight into what the application is doing.

watch -n 1 'echo "obase=2;`date +%s`" | bc'
grep -RnisI <pattern> *
2009-09-22 15:09:43
User: birnam
Functions: grep
Tags: bash grep
35

This is how I typically grep. -R recurse into subdirectories, -n show line numbers of matches, -i ignore case, -s suppress "doesn't exist" and "can't read" messages, -I ignore binary files (technically, process them as having no matches, important for showing inverted results with -v)

I have grep aliased to "grep --color=auto" as well, but that's a matter of formatting not function.

du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e'
cp file.txt{,.bak}
2009-01-26 12:11:29
User: root
Functions: cp
35

Uses shell expansion to create a back-up called file.txt.bak

ssh root@remote.host "rpm -qa" | xargs yum -y install
cp /work/host/phone/ui/main.cpp !#$:s/host/target
2011-09-20 06:48:07
User: kev
Functions: cp
Tags: !!
33

expand to:

cp /work/host/phone/ui/main.cpp /work/target/phone/ui/main.cpp

!#

The entire command line typed so far.

for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done
knock <host> 3000 4000 5000 && ssh -p <port> user@host && knock <host> 5000 4000 3000
2009-07-28 14:08:01
User: din7
Functions: ssh
33

Knock on ports to open a port to a service (ssh for example) and knock again to close the port. You have to install knockd.

See example config file below.

[options]

logfile = /var/log/knockd.log

[openSSH]

sequence = 3000,4000,5000

seq_timeout = 5

command = /sbin/iptables -A INPUT -i eth0 -s %IP% -p tcp --dport 22 -j ACCEPT

tcpflags = syn

[closeSSH]

sequence = 5000,4000,3000

seq_timeout = 5

command = /sbin/iptables -D INPUT -i eth0 -s %IP% -p tcp --dport 22 -j ACCEPT

tcpflags = syn

find . -name "*.[ch]" -exec grep -i -H "search pharse" {} \;
2009-05-06 15:22:49
User: bunedoggle
Functions: find grep
Tags: find grep
33

I have a bash alias for this command line and find it useful for searching C code for error messages.

The -H tells grep to print the filename. you can omit the -i to match the case exactly or keep the -i for case-insensitive matching.

This find command find all .c and .h files

wget -qO - http://infiltrated.net/blacklisted|awk '!/#|[a-z]/&&/./{print "iptables -A INPUT -s "$1" -j DROP"}'
2009-02-18 16:08:23
User: sil
Functions: wget
33

Blacklisted is a compiled list of all known dirty hosts (botnets, spammers, bruteforcers, etc.) which is updated on an hourly basis. This command will get the list and create the rules for you, if you want them automatically blocked, append |sh to the end of the command line. It's a more practical solution to block all and allow in specifics however, there are many who don't or can't do this which is where this script will come in handy. For those using ipfw, a quick fix would be {print "add deny ip from "$1" to any}. Posted in the sample output are the top two entries. Be advised the blacklisted file itself filters out RFC1918 addresses (10.x.x.x, 172.16-31.x.x, 192.168.x.x) however, it is advisable you check/parse the list before you implement the rules

wget -r -l1 --no-parent -nH -nd -P/tmp -A".gif,.jpg" http://example.com/images
2009-01-27 17:31:22
User: root
Functions: wget
33

This recursively downloads all images from a given website to your /tmp directory. The -nH and -nd switches disable downloading of the directory structure.

until !!; do :; done
date --set="$(ssh user@server date)"
2011-08-30 20:03:06
User: splante
Functions: date
Tags: ssh ,NTP ,Date
32

Shorter, easier to remember version of cmd#7636

NTP is better, but there are situations where it can't be used. In those cases, you can do this to sync the local time to a server.

cat /dev/urandom | hexdump -C | grep "ca fe"
2010-09-27 08:20:44
User: BOYPT
Functions: cat grep hexdump
32

just make some data scrolling off the terminal. wow.

echo "tee can split a pipe in two"|tee >(rev) >(tr ' ' '_')
2010-08-14 20:38:59
User: axelabs
Functions: echo tee tr
32

Tee can be used to split a pipe into multiple streams for one or more process to work it. You can add more " >()" for even more fun.

export HISTTIMEFORMAT="%F %T "
2009-05-12 14:36:42
User: fritz_monroe
Functions: export
32

History usually only gives the command number and the command. This will add a timestamp to the history file.

Note: this will only put the correct timestamp on commands used after the export is done. You may want to put this in your .bashrc

lsof -i tcp:80
2009-04-16 14:51:53
User: ar_levi
32

Sometimes you need to use a port that is already opened by some program , and you don't know who to "kill" for it to release - so, now you do !

:<C-R><C-W>
2009-04-08 21:25:06
User: pyrho
Tags: vim
32

Very handy to bring the word currently under the cursor into a :s command in Vim.

Example:

If the cursor was on the word "eggs":

:s/ ==> :s/eggs

grep --color=always | less -R
2009-05-20 20:30:19
User: dinomite
Functions: grep less
31

Get your colorized grep output in less(1). This involves two things: forcing grep to output colors even though it's not going to a terminal and telling less to handle those properly.

find ./ -type f -exec chmod 644 {} \;
mkdir -p work/{d1,d2}/{src,bin,bak}