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,848 results
find ~/bin/ -name "*sh" -print0 | xargs -0t tar -zcvf foofile.tar.gz
2009-02-17 08:48:34
User: lhb
Functions: find tar xargs
5

tar options may change ;)

c to compress into a tar file, z for gzip (j for bzip) man tar

-print0 and -0t are usefull for names with spaces, \, etc.

for i in `seq -f %03g 5 50 111`; do echo $i ; done
2009-02-17 08:41:44
User: lhb
Functions: echo
4

seq allows you to format the output thanks to the -f option. This is very useful if you want to rename your files to the same format in order to be able to easily sort for example:

for i in `seq 1 3 10`; do touch foo$i ;done

And

ls foo* | sort -n

foo1

foo10

foo4

foo7

But:

for i in `seq -f %02g 1 3 10`; do touch foo$i ;done

So

ls foo* | sort -n

foo01

foo04

foo07

foo10

find /directory/to/search/ -type f -print0 | xargs -0 grep "findtext"
2009-02-17 07:16:32
User: dingobytes
Functions: find grep xargs
2

this will find text in the directory you specify and give you line where it appears.

dd if=/dev/random of=bigfile bs=1024 count=102400
grep "processor" /proc/cpuinfo | wc -l
2009-02-17 05:39:49
User: jbcurtis
Functions: grep wc
4

/proc/cpuinfo contains information about the CPU.

Search for "processor" in the /proc/cpuinfo file

wc -l, counts the number of lines.

grep -sq "" /etc/lsb-release && lsb_release -rd
2009-02-17 05:21:20
User: int19h
Functions: grep
-3

grep -sq "" filename && command

grep can be used in combination with && to run a command if a file exists.

START=20; END=50 echo $(($START+(`od -An -N2 -i /dev/random`)%($END-$START+1)))
2009-02-17 05:05:30
User: pyrho
Functions: echo
0

This commands lets you generate a random number between the range [$START; $END].

ps aux | grep "[s]ome_text"
2009-02-17 02:10:50
User: SiegeX
Functions: grep ps
11

The trick here is to use the brackets [ ] around any one of the characters of the grep string. This uses the fact that [?] is a character class of one letter and will be removed when parsed by the shell. This is useful when you want to parse the output of grep or use the return value in an if-statement without having its own process causing it to erroneously return TRUE.

cd /source/directory; tar cf - . | tar xf - -C /destination/directory
some_command | tee >(command1) >(command2) >(command3) ... | command4
2009-02-17 01:55:07
User: SiegeX
Functions: tee
23

Using process substitution, we can 'trick' tee into sending a command's STDOUT to an arbitrary number of commands. The last command (command4) in this example will get its input from the pipe.

:%s/<control-VM>//g
2009-02-17 01:23:39
User: roliver
-2

Files saved on a windows machine use different ascii characters for lines turns. When viewing such files in VI the will most often have a ^M(control-VM) character at the end of each line. This command will remove all occurrences of that character

for i in {001..999}; print $i
2009-02-16 23:03:53
User: karld
-1

zsh only

I always hated resorting to using $(seq -w 1 99) to pad numbers. zsh provides a shortcut that couldn't be more intuitive. It also works in reverse {99..01}

tail -f /path/to/timestamped/files/file-*(om[1])
2009-02-16 22:55:16
User: karld
Functions: tail
5

zsh only

If you have this command in your history, you can always re-run it and have it reference the latest file.

The glob matches all timestamped files and then the resulting array is sorted by modification time (m) and then the first element in the sorted array is chosen (the latest)

nc localhost 10000 <<< "message"
2009-02-16 22:14:28
User: karld
1

zsh only - This avoids the need for echo "message" | which creates an entire subshell. Also, the text you are most likely to edit is at the very end of the line, which, in my opinion, makes it slightly easier to edit.

fortune -s -c -a | cowthink -d -W 45
2009-02-16 21:45:33
User: starchox
-4

The popular fortune program telling by a cow (see sample).

- fortune

- cowsay

cut -d: -f1 /etc/passwd | sort
2009-02-16 21:42:34
User: neW1
Functions: cut
0

Easily list all users

grep -v "^\($\|#\)" <filenames>
2009-02-16 21:40:09
User: raphink
Functions: grep
2

Filter comments and empty lines in files. I find this very useful when trying to find what values are actually set in a very long example config file.

I often set an alias for it, like :

alias nocomment='grep -v "^\($\|#\)"'

cd !$
2009-02-16 21:33:14
User: raphink
Functions: cd
1

!$ recalls the last argument of the previous command. This is very useful when you have to operate several operations on the same file for example.

awk -F'^"|", "|"$' '{ print $2,$3,$4 }' file.csv
2009-02-16 21:32:46
User: SiegeX
Functions: awk
7

The $2, $3, $4 fields are arbitrary but note that the first field starts from $2 and the last field is $NF-1. This is due to the fact that the leading and trailing quotes are treated as field delimiters.

kquitapp plasma
2009-02-16 21:27:16
User: raphink
Tags: KDE
-2

KDE4 is great, but still a bit buggy, and sometimes plasma requires to be restarted. Instead of quitting it with "killall plasma", which might loose your preferences (widgets, etc.), kquitapp will cleanly quit it. Tip: you can type this in the "Alt+F2" window, and then type "plasma" in Alt+F2 again to restart plasma (be patient though...).

diff dir1 dir2 | diffstat
2009-02-16 21:21:16
User: raphink
Functions: diff
0

See which files differ in a diff, and how many changes there are. Very useful when you have tons of differences.

echo *
2009-02-16 21:20:13
User: grep
Functions: echo
2

I know its not much but is very useful in time consuming scripts (cron, rc.d, etc).

perl -pi -e 's:^V^M::g' <filenames>
2009-02-16 21:17:40
User: starchox
Functions: perl
1

That "^M" is Ctrl-M, which is a carriage return, and is not needed in Unix file systems.

Where ^V is actually Ctrl-V and ^M is actually Ctrl-M (you must type these yourself, don't just copy and paste this command). ^V will not be displayed on your screen.

cd /usr/ports; grep -F "`for o in \`pkg_info -qao\` ; \ do echo "|/usr/ports/${o}|" ; done`" `make -V INDEXFILE` | \ grep -i \|ports@freebsd.org\| | cut -f 2 -d \|
2009-02-16 21:07:35
User: grep
Functions: cd cut grep
-1

only works for freeBSD where ports are installed in /usr/ports

credit to http://wiki.freebsd.org/PortsTasks

sed '1!G;h;$!d'