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,226 results
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
3

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
0

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"
for key in `echo 'KEYS pattern*' | redis-cli | awk '{print $1}'`; do echo DEL $key; done | redis-cli
2013-10-29 04:50:35
User: caiosba
Functions: awk echo
0

Delete items from Redis, using redis-cli, by matching a key pattern.

while true; do echo "message here" | mutt something@something.com ; done
2013-10-27 23:32:54
User: KcidKcus
Functions: echo
0

To send a text message to a phone, you need to send the text message via email. To find out how to send text messages to cell phones via email, go here - http://en.wikipedia.org/wiki/List_of_carriers_providing_Email_or_Web_to_SMS and find the carrier of the cell phone you want to send the text message to.

logme(){ echo "$(date +%d-%m-%Y-%H:%M) => $0 @ $1 returned error" >> persoscript.log }
2013-10-22 15:55:01
User: qcjn
Functions: echo
Tags: logging
0

Put this logging function in you're script, when need it call it.

command || logme "Error with command"

openssl rand -hex 1 | tr '[:lower:]' '[:upper:]' | xargs echo "obase=2;ibase=16;" | bc | cut -c1-6 | sed 's/$/00/' | xargs echo "obase=16;ibase=2;" | bc | sed "s/$/:$(openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | tr '[:lower:]' '[:upper:]')/"
2013-10-22 08:40:46
User: 4fd
Functions: bc cut echo sed tr xargs
0

I did not come up with this one myself, but found this somewhere else several months ago.

find . -user root
for file in "$@"; do name=$(basename "$file" .webm) echo ffmpeg -i $file -vn -c:a copy $name.ogg ffmpeg -i "$file" -vn -c:a copy "$name.ogg" done
2013-10-05 14:49:07
User: hoodie
Functions: basename echo file
0

Strips the audio track from a webm video. Use this in combination with clive or youtube-dl.

find .git/objects -type f -printf "%P\n" | sed s,/,, | while read object; do echo "=== $obj $(git cat-file -t $object) ==="; git cat-file -p $object; done
clear; tput cup 8 8; for i in $(seq 1 10); do echo -n "$((11-$i)) ";sleep 1; done; tput cup 10 8; echo -e "DONE\n\n"