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,604 results
grep -Rl "pattern" files_or_dir
2014-04-06 18:18:07
User: N1nsun
Functions: grep
Tags: awk find grep
0

Grep can search files and directories recursively. Using the -Z option and xargs -0 you can get all results on one line with escaped spaces, suitable for other commands like rm.

find . | xargs grep -l "FOOBAR" | awk '{print "rm -f "$1}' > doit.sh
2014-04-06 15:48:41
User: sergeylukin
Functions: awk find grep xargs
Tags: awk find grep
-2

After this command you can review doit.sh file before executing it.

If it looks good, execute: `. doit.sh`

<CTRL+Z>; fg
2014-04-06 14:21:08
User: kbrotheridge
-1

Saves opening another console terminal (eg. CTRL+ALT+F[n]) or opening another remote terminal.

Ctrl+Z pauses the current task and pushed it to the background, leaving you with a command prompt for those "Oh crap I forgot to change xyz before I ran that and it'll take forever if I Ctrl+C and start again..." situations. Typing 'fg' (shorthand for foreground, that's how I remember it) will resume the paused task.

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-") | sudo tee /sys/bus/usb/drivers/usb/unbind
2014-04-06 12:06:29
User: tweet78
Functions: awk cut df echo grep head sudo tail tee tr
29

You have an external USB drive or key.

Apply this command (using the file path of anything on your device) and it will simulate the unplug of this device.

If you just want the port, just type :

echo $(sudo lshw -businfo | grep -B 1 -m 1 $(df "/path/to/file" | tail -1 | awk '{print $1}' | cut -c 6-8) | head -n 1 | awk '{print $1}' | cut -c 5- | tr ":" "-")

VAR=$(head -5)
2014-04-05 13:45:18
User: rodolfoap
Functions: head
Tags: read stdin head,
-2

Reads n lines from stdin and puts the contents in a variable. Yes, I know the read command and its options, but find this logical even for one line.

command_line 2>&1 | tee -a output_file
for i in {1..256};do p=" $i";echo -e "${p: -3} \\0$(($i/64*100+$i%64/8*10+$i%8))";done|cat -t|column -c120
2014-04-04 16:54:53
User: AskApache
Functions: cat column echo
6

Prints out an ascii chart using builtin bash! Then formats using cat -t and column.

The best part is:

echo -e "${p: -3} \\0$(( $i/64*100 + $i%64/8*10 + $i%8 ))";

From: http://www.askapache.com/linux/ascii-codes-and-reference.html

echo "this_is_a_test" | sed -r 's/_([a-z])/\U\1/g'
C:\> shutdown /f /r /t 0
2014-04-02 22:35:00
User: mpb
Functions: shutdown
-1

Today, I needed to reboot a Windoze machine on another continent which had no shutdown or restart options via "Start" in the remote desktop (the only options available were: "Logoff, Disconnect, or Lock").

Fortunately, I found how to shutdown and restart from the command line.

hgrep() { ... } longer then 255 characters, see below
2014-04-02 16:40:36
User: Xk2c
0

hgrep()

{

if [[ ${#} -eq 0 ]]

then

printf "usage:\nhgrep [--nonum | -N | -n | --all-nonum | -an | -na] STRING\n"

return 1

fi

while [[ ${#} -gt 0 ]]

do

case ${1} in

--nonum | -N | -n | --all-nonum | -an | -na)

builtin history | sed 's/^[[:blank:]]\+[[:digit:]]\{1,5\}[[:blank:]]\{2\}//' | grep -iE "(${*:2})"

break

;;

*)

builtin history | grep -iE "(${*})"

break

;;

esac

done

}

'hgrep -n' helps in using full grep support, e.g. search for _beginning_ of specific commands,

see example output

rename 's/\.sh//' ./*
2014-04-02 16:33:25
User: abhikeny
Functions: rename
0

The 'rename' command with the first argument as "'s/\.//'" and the second argument as "" will remove the specified extension from the filenames.

function hg () { history | grep $* ; } # define a function combining history and grep to save typing :-)
2014-04-02 15:17:31
User: mpb
Functions: grep
4

By defining a function "hg" as shown here, it saves me typing "history | grep" every time I need to search my shell history because now I only have to type "hg".

A nifty time saver :-)

You can also add the "hg" function definition to your .bashrc so it is defined each time you login.

(prefix="10.59.21" && for i in `seq 254`; do (sleep 0.5 && ping -c1 -w1 $prefix.$i &> /dev/null && arp -n | awk ' /'$prefix'.'$i' / { print $1 " " $3 } ') & done; wait)
2014-04-02 11:20:57
User: smoky
Functions: arp awk ping sleep
Tags: ping
1

Waits for all pings to complete and returns ip with mac address

diff -qr /dirA /dirB
rsync -avz --dry-run /somewhere/source_directory /somewhereelse/target_directory
2014-04-01 20:55:59
User: tsener
Functions: rsync
Tags: diff rsync
-1

--dry-run will only show you which files would be otherwise synced with rsync.

-z is for compressio

-v vervose

-a "as is" - permissions, ownership etc.

for output in $(find . ! -name movie.nfo -name "*.nfo") ; do rm $output ; done
2014-04-01 17:41:50
User: analbeard
Functions: find rm
-1

Finds all nfo files without the filename movie.nfo and deletes them.

find directory -maxdepth 1 -iname "*" | awk 'NR >= 2'
2014-04-01 00:09:12
User: chilicuil
Functions: awk find
-3

find . -maxdepth 1 -iname ".*" | awk 'NR >= 2'

Can be used to list only dotfiles without . nor ..

curl 'AudioStream' | ffmpeg -i - -acodec libvorbis file.ogg
2014-03-31 02:07:20
User: snipertyler
0

This encodes it in ogg format.

Does on-the-fly encoding of the incoming stream.

Great for radio streams as they're often flv format.

watch ls -lh /path/to/folder
2014-03-27 10:51:36
User: vonElfensenf
Functions: ls watch
Tags: pv
0

forgot to use a pv or rsync and want to know how much has been copied.

awk '$1=$1' FS=" " OFS=":" file
for i in *.svg; do convert "$i" "${i%.svg}.png" & done
2014-03-26 15:03:54
User: robinro
0

convert all *.svg to png _in parallel_

this speeds up the conversion for many files, but might also not be feasible for a large number of files

xxd -l 20 -c 20 -p /dev/urandom
2014-03-26 09:44:04
User: jt
Tags: password
0

Dumps 20 bytes from /dev/urandom and converts them to hex. -c and -p are needed to prevent splitting over lines.

echo 1395767373016 | gawk '{print strftime("%c", ( $0 + 500 ) / 1000 )}'
dpipe /usr/lib/openssh/sftp-server = ssh $REMOTE_HOST sshfs whatever:$LOCAL_PATH $REMOTE_PATH -o slave
2014-03-25 17:40:34
User: em
Functions: ssh
5

While `sshfs $REMOTE_HOST:$REMOTE_PATH $LOCAL_PATH` "pulls" a directory from the remote server to the local host, the above command does the reverse and "pushes" a directory from the local host to the remote server.

This makes use of the "slave" option of sshfs which instructs it to communicate over plain stdin/stdout and the `dpipe` tool from vde2 to connect the sftp-server stdout to the sshfs stdin and vice-versa.

find . -name '*.mp3' | sort | while read -r mp3; do echo -e "<h3>$mp3</h3>\n<audio controls src=\"$mp3\"></audio>"; done > index.html; python -m http.server
2014-03-24 15:01:49
User: hendry
Functions: echo find python read sort
Tags: audio browser
1

I tried a few curses based mp3 players for playing back choir practice songs for my wife.

Unfortunately none of the ones I tried were capable of scrubbing a track.

Firefox saves the day.