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
alias ip4rev "echo \!* | sed 's/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\4.\3.\2.\1/'"
2009-02-09 17:00:03
Functions: alias
-1

For 'bash'

function ip4rev() { echo $@ | sed 's/^\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)/\4.\3.\2.\1/'; }
comm --nocheck-order -31
find -type f | xargs -I{} du -s "{}" | sort -rn | head | cut -f2 | xargs -I{} du -sh "{}"
2011-01-04 11:10:56
User: glaudiston
Functions: cut du find head sort xargs
-1

Show the top file size in human readable form

cvlc "v4l2:///dev/video0" --sout "#transcode{vcodec=mp2v,vb=800,scale=0.25,acodec=none}:file{mux=mpeg1,dst=/PATH/TO/OUTPUT/FILE}"
ssh user@remote "cat /remote/music/dir/*.mp3" | mpg123 -
2013-02-15 20:03:21
User: erix
Functions: mpg123 ssh
Tags: ssh Linux mpg123
-1

This is just a quick and dirty way to play remote audio files *locally* in your PC.

The best way is to mount the remote Music directory into the local FS, say by using sshfs:

sshfs user@remote:/remote/music/dir/ /mnt/other_pc/

ping -n 1 -w 10000 224.0.0.0
2009-08-26 02:25:07
User: DocGyver
Functions: ping
-1

In the vein of "can you do it better", here is my take on using "ping" to emulate sleep in a DOS/BAT script. If one can use ping at all then the multicast address will be valid but will not respond. By doing only one ping (-n 1) and setting a timeout in milliseconds (-w 10000) you have a fairly accurate timer. This example gives about a 10 second sleep.

ls -l directory | awk 'BEGIN { SUM=0 } { SUM+=$5 } END { print SUM/1024/1024"M" }'
convert -quality 40% original_image reduced_image
for L in `echo :$PATH | tr : '\n'`; do F=${L:-"."}/fileName; if [ -f ${F} -o -h ${F} ]; then echo ${F}; break; fi; done
2009-09-11 16:14:36
User: arcege
Functions: echo tr
-1

Searches in order of the directories of $PATH. Stops after finding the entry; looks for only that fileName. Works in Bourne, Korn, Bash and Z shells.

ffmpeg -i input.flv -vhook '/usr/lib/vhook/imlib2.so -c white -x 250 -y H+(-1.8*N+80) -t Hallo! -A max(0,255-exp(N/16))' -sameq -acodec copy output.flv
find -type f -exec du -sh {} + | sort -rh | head
find . -name '*.txt' -exec mv {} {}.sh \ ;
2011-06-02 23:47:19
Functions: find mv
-1

You can replace "." with your folder

MAC=$((date +'%Y%m%d%H%M%S%N'; cat /proc/interrupts) | md5sum | sed -r 's/(..)/\1:/g' | cut -d: -f 1-6)
2011-07-14 13:11:50
User: jazznjam
Functions: cat cut date md5sum sed
-1

I liked vaporub's suggestion, here a little simplification of the sed command.

convert example.png -resize 100x100 output.png
2010-08-08 16:40:21
User: dbbolton
-1

You can also specify width and height of the resized image (in pixels), as an alternative to using a percentage.

find . -name "*.png" | tee images.txt | grep book
2011-06-02 23:55:50
Functions: find grep tee
-1

You can find every file with png extension and redirect its output to file. Later you can find a string inside the file.

cp -dpRx /* /mnt/target/
2011-08-22 14:22:01
Functions: cp
-1

preserve all except context and xattr. useful when moving a running system to a new partition.

vgdisplay -v 2>/dev/null | grep "^ LV Name" | while read A B LVDEV; do echo $LVDEV; done
NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)
2010-06-04 07:58:53
User: keymon
Functions: mount perl umount
-1

Based on the execute with timeout command in this site.

A more complex script:

#!/bin/sh

# This script will check the avaliability of a list of NFS mount point,

# forcing a remount of those that do not respond in 5 seconds.

#

# It basically does this:

# NFSPATH=/mountpoint TIMEOUT=5; perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $NFSPATH" || (umount -fl $NFSPATH; mount $NFSPATH)

#

TIMEOUT=5

SCRIPT_NAME=$(basename $0)

for i in $@; do

echo "Checking $i..."

if ! perl -e "alarm $TIMEOUT; exec @ARGV" "test -d $i" > /dev/null 2>&1; then

echo "$SCRIPT_NAME: $i is failing with retcode $?."1>&2

echo "$SCRIPT_NAME: Submmiting umount -fl $i" 1>&2

umount -fl $i;

echo "$SCRIPT_NAME: Submmiting mount $i" 1>&2

mount $i;

fi

done

cat /etc/passwd | wc -l
iostat -En $(iostat -en|grep c#t|awk '$2 > 0 {print $NF}')
2011-12-09 15:54:28
Functions: awk grep
-1

This helps quickly get information for each disk that is seemingly having hardware issues.

do-release-upgrade
2009-03-25 07:25:33
User: dizzgo
Tags: Ubuntu
-1

release upgrades are major upgrades as from 8.04 to 8.10 or from 8.10 to 9.04

update-manager-core must be installed:

sudo apt-get install update-manager-core

in some cases you must edit '/etc/update-manager/release-upgrades' and set 'Prompt=normal'

recoll -t -q "keyword" | grep message/rfc822 | sed -s 's,^.*\('$MAILDIR'[^]]*\)\].*$,\"\1\",' | xargs ln -sft $MAILDIR/bingo/cur/
ls -d */* | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs mv -t $(pwd)
2010-03-01 23:43:26
User: leovailati
Functions: ls mv sed xargs
-1

You WILL have problems if the files have the same name.

Use cases: consolidate music library and unify photos (especially if your camera separates images by dates).

After running the command and verifying if there was no name issues, you can use

ls -d */ | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs rm -r

to remove now empty subdirectories.

dc -e "1 1 + p"
ff() { find -maxdepth 3 -type f -iname "$1"; }; fd() { find -maxdepth 4 -type d -iname "$1"; }
2012-08-15 15:04:48
User: plasticboy
Functions: find
Tags: find simple
-1

These should be a little faster since they don't have to spawn grep.