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

Commands using echo from sorted by
Terminal - Commands using echo - 1,210 results
echo -e 'alias exit='\''pwd > ~/.lastdir;exit'\''\n[ -n "$(cat .lastdir 2>/dev/null)" ] && cd "$(cat .lastdir)"' >> ~/.bash_aliases
2014-01-28 18:02:04
User: ichbins
Functions: cd echo
Tags: exit pwd
-1

this command will add the following two lines into the ~/.bash_aliases:

alias exit='pwd > ~/.lastdir;exit'

[ -n "$(cat .lastdir 2>/dev/null)" ] && cd "$(cat .lastdir)"

or redirect it to the ~/.bashrc if you like

Donno, I find it usefull. You may also define an alias for 'cd ~' like - alias cdh='cd ~'

for i in {0..255} ; do (ping 192.168.1.$i -c 1 > /dev/null && echo "192.168.1.$i" & ) ; done
2014-01-26 18:31:34
User: bugmenot
Functions: echo ping
7

Pings all the hosts on 192.168.1.0/24 in parallel, so it is very fast. Alive host IP addresses are echoed to stdout as pings are responded to.

server$ while true; do read -n30 ui; echo $ui |openssl enc -aes-256-cbc -a -k PaSSw; done | nc -l -p 8877 | while read so; do decoded_so=`echo "$so"| openssl enc -d -a -aes-256-cbc -k PaSSw`; echo -e "Incoming: $decoded_so"; done
2014-01-16 14:36:09
User: arno
Functions: echo read
7

client$ while true; do read -n30 ui; echo $ui |openssl enc -aes-256-cbc -a -k PaSSw ; done | nc localhost 8877 | while read so; do decoded_so=`echo "$so"| openssl enc -d -a -aes-256-cbc -k PaSSw`; echo -e "Incoming: $decoded_so"; done

This will establish a simple encrypted chat with AES-256-CBC using netcat and openssl only.

More info here http://www.nixaid.com/linux/network/encrypted-chat-with-netcat

echo $(($(find . -name "pattern" -type f -printf "+%s")))
2014-01-16 03:14:36
User: flatcap
Functions: echo find
3

Use find's internal stat to get the file size then let the shell add up the numbers.

IFS=$(echo -en "\n\b"); mkdir Epub; for i in `find . | grep epub`; do echo "epub: $i"; mv -v "$i" Epub; done
for s in 172.16.{1,2}2.9{0,1,2}; do echo "Repairing node $s..."; nodetool -h $s repair -pr; done
2014-01-01 17:05:40
User: mrwulf
Functions: echo
0

Repair each node in sequence. Make sure you change the IP replacements to match your environment

finfo() { [[ -f "$(cygpath "$@")" ]] || { echo "bad-file";return 1;}; echo "$(wmic datafile where name=\""$(echo "$(cygpath -wa "$@")"|sed 's/\\/\\\\/g')"\" get /value)"|sed 's/\r//g;s/^M$//;/^$/d'|awk -F"=" '{print $1"=""\033[1m"$2"\033[0m"}';}
2013-12-30 07:47:41
User: lowjax
Functions: awk echo return sed
0

Pass the files path to finfo(), can be unix path, dos path, relative or absolute. The file is converted into an absolute nix path, then checked to see if it is in-fact a regular/existing file. Then converted into an absolute windows path and sent to "wmic". Then magic, you have windows file details right in the terminal. Uses: cygwin, cygpath, sed, and awk. Needs Windows WMI "wmic.exe" to be operational. The output is corrected for easy...

finfo notepad.exe finfo "C:\windows\system32\notepad.exe" finfo /cygdrive/c/Windows/System32/notepad.exe finfo "/cygdrive/c/Program Files/notepad.exe" finfo ../notepad.exe
cd ~/wallpapers;arr=( * );num_wallpapers=${#arr[@]};wallpaper=${arr[$(echo $RANDOM%$num_wallpapers|bc)]};feh --bg-fill $wallpaper
2013-12-30 01:56:24
User: Evidlo
Functions: cd echo
0

Generates a bash array and uses it to select a random image from ~/wallpapers.

for i in *.djvu; do djvu2pdf $i && echo "Finished -> $i"; done;
for i in $(ls *.mp3); do mplayer $i && echo "delete it? [y/n]" && read trash && if [ "$trash" == "y" ]; then rm $i; fi; do
2013-12-23 20:09:56
User: wibbel4
Functions: echo ls read rm
-2

Old drive with lots of music or unsorted drive? This command will play all mp3 files in a folder and after playing one song or pressing q, it will ask you if you want to delete the file.

for i in */; do echo $(find $i -type f -regextype posix-extended -regex ".*\.(mp3|ogg|wav|flac)" | wc -l) $i ; done
find . -type d -maxdepth 1 -print0 | xargs -0 -I{} sh -c 'find "{}" -type f | grep "ogg\|mp3\|wav\|flac$" | wc -l | tr -d "\n"; echo " {}"'
2013-12-22 13:40:29
User: dbrgn
Functions: echo find grep sh tr wc xargs
0

This lists the number of ogg/mp3/wav/flac files in each subdirectory of the current directory. The output can be sorted by piping it into "sort -n".

echo "text to prepend" | cat - file
2013-12-18 15:54:17
User: leni536
Functions: cat echo
Tags: cat replace
2

Prepend text to a file. It doen't need temporary files, ed or sed.

ram() { mt=/mnt/ram && grep "$mt" < /proc/mts > /dev/null; if [ $? -eq 0 ] ; then read -p"Enter to Remove Ram Partition ";sudo umount "$mt" && echo $mt 0; else sudo mt -t tmpfs tmpfs "$mt" -o size=$(( ${1:-1} * 1024 ))m && echo $mt '-' "${1:-1}"gb; fi; }
2013-12-13 05:22:02
User: snipertyler
Functions: echo grep mt read sudo umount
Tags: sudo ram tmpfs
1

Creates a temporary ram partition

To use:

ram 3

to make a 3gb partition (Defaults to 1gb)

for fn in *.epub; do echo mv \"$fn\" \"`echo "$fn" | sed -E 's/\.*\/*(.*)( - )(.*)(\.[^\.]+)$/\3\2\1\4/' | sed -E 's/(.*) ([^ ]+)( - )(.*)/\2, \1\3\4/' `\";done | sh
2013-11-30 05:29:52
User: woohoo
Functions: echo mv sed
0

If you want to test output, run it like this:

for fn in *.epub; do echo mv \"$fn\" \"`echo "$fn" | sed -E 's/\.*\/*(.*)( - )(.*)(\.[^\.]+)$/\3\2\1\4/' | sed -E 's/(.*) ([^ ]+)( - )(.*)/\2, \1\3\4/' `\";done > rename.txt

for i in */; do echo run_command "${i}"; done
2013-11-25 18:33:26
User: mirams
Functions: echo
0

Change the "run_command" to whatever command you want, and remove the "echo" to run it once you are happy.

echo "Hello WOrld"
2013-11-24 10:26:50
User: Abhay_k
Functions: echo
Tags: echo
1

This will display --> Hello World

for file in $(git ls-files | grep old_name_pattern); do git mv $file $(echo $file | sed -e 's/old_name_pattern/new_name_pattern/'); done
nc -kl 5432 -c 'echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n";echo "<p>How are you today?</p>"'
2013-11-12 14:00:11
User: gvitalie
Functions: echo
-1

-k, --keep-open will keep connection alive, and we could exclude using 'while true'

nc is such a powerful command, it could be used instead of any OS! :p

nc -kl 5432 -c 'echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n";echo "<p>How are you today?</p>"'
2013-11-12 13:43:38
User: gvitalie
Functions: echo
0

nc is such a powerful command, it could be used instead of any OS! :p

(echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 open" || echo "TCP port 25 close"
2013-11-12 02:11:01
User: YouM
Functions: echo
0

Check trough unix sockets if tcp port is open or close

echo $(curl http://ipecho.net/plain 2> /dev/null)
echo $(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c10)
2013-11-07 21:51:38
User: santizo
Functions: echo head tr
0

Uses urandom and tr to generate a random password. Change the value of head -c## for the lenght of the password.

echo "rootgot" | awk 'BEGIN{FIELDWIDTHS="4 3"}{print $2$1}'
# [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "Alive"