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.


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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

All commands from sorted by
Terminal - All commands - 12,371 results
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
2011-02-17 11:13:19
User: glaudiston
Functions: sleep tput
110

A nice way to use the console in full screen without forget the current time.

you can too add other infos like cpu and mem use.

ping -i 60 -a IP_address
2009-03-04 06:21:22
User: haivu
Functions: ping
Tags: Network
109

Waiting for your server to finish rebooting? Issue the command above and you will hear a beep when it comes online. The -i 60 flag tells ping to wait for 60 seconds between ping, putting less strain on your system. Vary it to your need. The -a flag tells ping to include an audible bell in the output when a package is received (that is, when your server comes online).

echo "You can simulate on-screen typing just like in the movies" | pv -qL 10
2010-01-14 20:17:44
User: dennisw
Functions: echo
105

This will output the characters at 10 per second.

getconf LONG_BIT
2009-08-08 21:22:19
User: caiosba
Functions: getconf
Tags: getconf 32 64
105

Easy and direct way to find this out.

<alt> + <print screen/sys rq> + <R> - <S> - <E> - <I> - <U> - <B>
2009-02-20 07:28:56
User: dizzgo
105

If the machine is hanging and the only help would be the power button, this key-combination will help to reboot your machine (more or less) gracefully.

R - gives back control of the keyboard

S - issues a sync

E - sends all processes but init the term singal

I - sends all processes but init the kill signal

U - mounts all filesystem ro to prevent a fsck at reboot

B - reboots the system

Save your file before trying this out, this will reboot your machine without warning!

http://en.wikipedia.org/wiki/Magic_SysRq_key

ps aux | sort -nk +4 | tail
2009-01-23 17:12:33
User: root
Functions: ps sort
99

ps returns all running processes which are then sorted by the 4th field in numerical order and the top 10 are sent to STDOUT.

mv filename.{old,new}
pushd /tmp
2009-02-16 16:52:59
User: ruedu
94

If are a Bash user and you are in a directory and need to go else where for a while but don't want to lose where you were, use pushd instead of cd.

cd /home/complicated/path/.I/dont/want/to/forget

pushd /tmp

cd thing/in/tmp

popd (returns you to /home/complicated/path/.I/dont/want/to/forget)

\[command]
2009-02-11 19:34:21
User: wwest4
91

e.g. if rm is aliased for 'rm -i', you can escape the alias by prepending a backslash:

rm [file] # WILL prompt for confirmation per the alias

\rm [file] # will NOT prompt for confirmation per the default behavior of the command

lsof -i
some_very_long_and_complex_command # label
2009-09-08 05:58:27
User: jamolkhon
89

When using reverse-i-search you have to type some part of the command that you want to retrieve. However, if the command is very complex it might be difficult to recall the parts that will uniquely identify this command. Using the above trick it's possible to label your commands and access them easily by pressing ^R and typing the label (should be short and descriptive).

UPDATE:

One might suggest using aliases. But in that case it would be difficult to change some parts of the command (such as options, file/directory names, etc).

echo "!!" > foo.sh
2009-02-25 00:37:25
User: dnavarre
Functions: echo
89

Sometimes commands are long, but useful, so it's helpful to be able to make them permanent without having to retype them. An alternative could use the history command, and a cut/sed line that works on your platform.

history -1 | cut -c 7- > foo.sh
rm !(*.foo|*.bar|*.baz)
2010-04-13 15:13:54
User: hutch
Functions: rm
88

Deletes all files in a folder that are NOT *.foo, *.bar or *.baz files. Edit the pattern inside the brackets as you like.

nc -v -l 80 < file.ext
2009-02-17 14:39:52
User: moz667
87

From the other machine open a web navigator and go to ip from the machine who launch netcat, http://ip-address/

If you have some web server listening at 80 port then you would need stop them or select another port before launch net cat ;-)

* You need netcat tool installed

diff <(sort file1) <(sort file2)
2009-02-04 22:20:13
User: systemj
Functions: diff sort
87

bash/ksh subshell redirection (as file descriptors) used as input to diff

!*
2009-06-21 17:58:01
User: Neo23x0
86

!* is all of the arguments to the previous command rather than just the last one.

This is useful in many situations.

Here's a simple example:

vi cd /stuff

oops!

[exit vi, twice]

!*

expands to: cd /stuff

CDPATH=:..:~:~/projects
2009-03-20 14:50:25
User: haivu
Tags: bash
84

CDPATH tells the cd command to look in this colon-separated list of directories for your destination. My preferred order are 1) the current directory, specified by the empty string between the = and the first colon, 2) the parent directory (so that I can cd lib instead of cd ../lib), 3) my home directory, and 4) my ~/projects directory.

awk '/start_pattern/,/stop_pattern/' file.txt
2009-03-28 14:28:59
User: atoponce
Functions: awk
83

I find this terribly useful for grepping through a file, looking for just a block of text. There's "grep -A # pattern file.txt" to see a specific number of lines following your pattern, but what if you want to see the whole block? Say, the output of "dmidecode" (as root):

dmidecode | awk '/Battery/,/^$/'

Will show me everything following the battery block up to the next block of text. Again, I find this extremely useful when I want to see whole blocks of text based on a pattern, and I don't care to see the rest of the data in output. This could be used against the '/etc/securetty/user' file on Unix to find the block of a specific user. It could be used against VirtualHosts or Directories on Apache to find specific definitions. The scenarios go on for any text formatted in a block fashion. Very handy.

echo "The date is: $(date +%D)"
2009-03-07 15:51:59
User: atoponce
Functions: echo
83

This is a simple example of using proper command nesting using $() over ``. There are a number of advantages of $() over backticks. First, they can be easily nested without escapes:

program1 $(program2 $(program3 $(program4)))

versus

program1 `program2 \`program3 \`program4\`\``

Second, they're easier to read, then trying to decipher the difference between the backtick and the singlequote: `'. The only drawback $() suffers from is lack of total portability. If your script must be portable to the archaic Bourne shell, or old versions of the C-shell or Korn shell, then backticks are appropriate, otherwise, we should all get into the habit of $(). Your future script maintainers will thank you for producing cleaner code.

ifconfig | convert label:@- ip.png
man hier
2010-01-26 16:31:05
User: haivu
Functions: man
77

Curious about differences between /bin, /usr/bin, and /usr/local/bin? What should be in the /sbin dir? Try this command to find out.

Tested against Red Hat & OS X

vim -x <FILENAME>
2009-05-05 23:24:17
User: denzuko
Functions: vim
77

While I love gpg and truecrypt there's some times when you just want to edit a file and not worry about keys or having to deal needing extra software on hand. Thus, you can use vim's encrypted file format.

For more info on vim's encrypted files visit: http://www.vim.org/htmldoc/editing.html#encryption

fuser -k filename
2009-02-05 20:16:11
User: fzero
Functions: fuser
76

Useful when you're trying to unmount a volume and other sticky situations where a rogue process is annoying the hell out of you.