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
ssh-keygen; ssh-copy-id user@host; ssh user@host
2009-03-18 07:59:33
User: bwoodacre
Functions: ssh ssh-keygen
Tags: openssh
18

This command sequence allows simple setup of (gasp!) password-less SSH logins. Be careful, as if you already have an SSH keypair in your ~/.ssh directory on the local machine, there is a possibility ssh-keygen may overwrite them. ssh-copy-id copies the public key to the remote host and appends it to the remote account's ~/.ssh/authorized_keys file. When trying ssh, if you used no passphrase for your key, the remote shell appears soon after invoking ssh user@host.

ssh user@host "cd targetdir; tar cfp - *" | dd of=file.tar
2009-03-18 07:43:22
User: bwoodacre
Functions: dd ssh
6

This invokes tar on the remote machine and pipes the resulting tarfile over the network using ssh and is saved on the local machine. This is useful for making a one-off backup of a directory tree with zero storage overhead on the source. Variations on this include using compression on the source by using 'tar cfvp' or compression at the destination via

ssh user@host "cd dir; tar cfp - *" | gzip - > file.tar.gz
for file in `find . -type f`; do cat $file; done | wc -l
echo -n 'text to be encrypted' | openssl md5
2009-03-18 00:11:46
User: Zenexer
Functions: echo
17

Thanks to OpenSSL, you can quickly and easily generate MD5 hashes for your passwords.

Alternative (thanks to linuxrawkstar and atoponce):

echo -n 'text to be encrypted' | md5sum -

Note that the above method does not utlise OpenSSL.

top
2009-03-18 00:03:42
User: Zenexer
Functions: top
-5

A simple but effective replacement for ps aux. I used to waste my time running ps over and over; top is the way to go. It also allows complex sorting options. Press q to exit "nicely" (Ctrl + C is always an option, of course). Note that the list updates each second, resorting in the process; if you're trying to grab a specific PID, you might be better off with ps.

htop

Alternatively, htop is available, though it may not come pre-installed. htop is slightly more interactive than top and includes color coding, visuals, and a nice interface for selecting and then killing processes. (Thanks to bwoodacre for this great tool.)

^Z $bg $disown
2009-03-17 21:52:52
User: fall0ut
47

You're running a script, command, whatever.. You don't expect it to take long, now 5pm has rolled around and you're ready to go home... Wait, it's still running... You forgot to nohup it before running it... Suspend it, send it to the background, then disown it... The ouput wont go anywhere, but at least the command will still run...

sed ':a;N;$!ba;s/\n/ /g'
2009-03-17 20:54:04
User: cidiom
Functions: sed
Tags: Linux
0

Ever had a file with a list of numbers you wanted to add, use:

cat file | sed ':a;N;$!ba;s/\n/+/g' | bc
echo "test" | lp -d $PRINTER
2009-03-17 20:07:41
User: nauseaboy
Functions: echo lp
1

This will send a test print job to a networked printer.

vos listvldb | agrep LOCKED -d RWrite | grep RWrite: | awk -F: '{print $2}' | awk '{printf("%s ",$1)} END {printf("\n")}'
2009-03-17 19:55:39
User: mpb
Functions: awk grep
0

This command shows if there are any locked AFS volumes.

The output is a list of AFS volume IDs (or nothing if there are none locked).

mplayer tv:// -tv driver=v4l:width=352:height=288
2009-03-17 19:37:23
User: Robertof
9

Show the webcam output with mplayer.

startx > startx.log 2>&1
shopt -s nocaseglob
2009-03-17 17:51:42
User: josx
3

Change bash autocomplete case search to insensitive when pressing tab for completion.

shopt -s cdspell
ls -1 | rename 's/\ /_/'
2009-03-17 17:33:41
Functions: ls rename
4

Substitute spaces in filename with underscore, it work on the first space encountered.

<space>command
2009-03-17 16:25:29
User: eaZy
362

Prepending one or more spaces to your command won't be saved in history.

Useful for pr0n or passwords on the commandline.

Tested on BASH.

for i in `find /sys/devices/*/*/usb* -name level` ; do echo -n "$i: " ; cat $i ; done
rpm -qa --queryformat '%{installtime} \"%{vendor}\" %{name}-%{version}-%{release} %{installtime:date}\n' | grep "Thu 05 Mar"
2009-03-17 13:38:20
User: mpb
Functions: grep rpm
4

Find out which RPMs were installed on a particular date.

These would (naturally) include update RPMs.

This example shows searching for "Thu 05 Mar" (with grep).

Alternatively, pipe it to less so you can search inside less (with less's neat text highlighting of the search term):

rpm -qa --queryformat '%{installtime} \"%{vendor}\" %{name}-%{version}-%{release} %{installtime:date}\n' | less # (this example) search term: Thu 05 Mar

play $audio_file
2009-03-17 11:30:02
User: mpb
-3

"play" is part of "SoX"

SoX - Sound eXchange, the Swiss Army knife of audio manipulation.

For details, see: man sox

find -maxdepth 1 -type d -print0 | xargs -0 -I {} tar -cvzf {}.tar.gz {}
2009-03-17 11:12:53
User: piemme
Functions: find tar xargs
2

Create backup (.tar.gz) for all first-level directory from current dir.

lynx -accept_all_cookies -cmd_script=/your/keystroke-file
2009-03-17 00:38:36
User: Alanceil
25

This command will tell lynx to read keystrokes from the specified file - which can be used in a cronjob to auto-login on websites that give you points for logging in once a day *cough cough* (which is why I used -accept_all_cookies).

For creating your keystroke file, use:

lynx -cmd_log yourfile
:!cp % %-
2009-03-17 00:34:24
User: mpb
5

At the start of a vi session and *before* saving any changes use ":!cp % %-" to make a backup of the current file being edited.

example: vi /data/some/long/path/file

:!cp% %-

creates /data/some/long/path/file-

# indicates a comment in shell
2009-03-16 23:15:33
User: mpb
-4
#

Using the "#" in shell is surprisingly useful.

Some of the uses I found:

a) As a visible copy buffer in shell history (caveat: do not use for passwords :-)

b) To build complex commands until ready then hit the HOME, DEL, ENTER keys to run it

c) Placing reference data into shell history (search for tags with CTRL-R TAGNAME)

d) Putting aside a "work in progress" command to focus on another task (HOME # ENTER)

ctrl-z
2009-03-16 20:58:31
User: mallegonian
Tags: bash hotkey
11

Hold ctrl and press z to pause the current thread. Run

fg

to resume it.

convert -font -misc-fixed-*-*-*-*-*-*-*-*-*-*-*-* -fill black -draw "text 270,260 \" `cal` \"" testpic.jpg newtestpic.jpg
2009-03-16 16:09:58
User: dasmna
Tags: ImageMagick
15

Add calendar to desktop wallpaper , mess with the coordinates to place where you like

alias lvim="vim -c \"normal '0\""