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 read from sorted by
Terminal - Commands using read - 282 results
find . -type d -print0 | while read -d $'\0' dir; do cd "$dir"; echo " process $dir"; find . -maxdepth 1 -name "*.ogg.mp3" -exec rename 's/.ogg.mp3/.mp3/' {} \; ; cd -; done
2014-08-25 11:28:43
Functions: cd echo find read rename
0

This is probably overkill, but I have some issues when the directories have spaces in their names.

The

find . -type d -print0 | while read -d $'\0' dir; do xxx; done

loops over all the subdirectories in this place, ignoring the white spaces (to some extend).

cd "$dir"; echo " process $dir"; cd -;

goes to the directory and back. It also prints some info to check the progress.

find . -maxdepth 1 -name "*.ogg.mp3" -exec rename 's/.ogg.mp3/.mp3/' {} \;

renames the file within the current directory.

The whole should work with directories and file names that include white spaces.

pgrep 'sleep' | while read pid; do pstree -sa -H$pid $pid ; done
echo -n test@example.com | md5sum | (read hash dash ; echo "https://secure.gravatar.com/avatar/${hash}")
cat dictionary.txt|while read a; do echo $a|cryptsetup luksOpen /dev/sda5 sda5 $a && echo KEY FOUND: $a; done
2014-04-16 18:49:53
User: rodolfoap
Functions: cat echo read
0

Lost your luks passphrase? You can always bruteforce from the command line. See the sample output, a simple command using a dictionary.

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.

while read i; do [ ${#i} -gt 72 ] && echo "$i"; done < /path/to/file
2014-03-20 12:27:06
User: flatcap
Functions: echo read
1

Filter out lines of input that contain 72, or fewer, characters.

This uses bash only. ${#i} is the number of characters in variable i.

alias ...='while read line; do echo -n "."; done && echo ""'
read -p "Please enter the 4chan url: "|egrep '//i.4cdn.org/[a-z0-9]+/src/([0-9]*).(jpg|png|gif)' - -o|nl -s https:|cut -c7-|uniq|wget -nc -i - --random-wait
inotifywait -mr -e CREATE $HOME/bin/ | while read i; do chmod +x $(echo "$i" | sed 's/ \S* //'); done
find . -type d| while read i; do echo $(ls -1 "$i"|wc -m) $(du -s "$i"); done|sort -s -n -k1,1 -k2,2 |awk -F'[ \t]+' '{ idx=$1$2; if (array[idx] == 1) {print} else if (array[idx]) {print array[idx]; print; array[idx]=1} else {array[idx]=$0}}'
2014-02-25 22:50:09
User: knoppix5
Functions: awk du echo find ls read sort wc
1

Very quick! Based only on the content sizes and the character counts of filenames. If both numbers are equal then two (or more) directories seem to be most likely identical.

if in doubt apply:

diff -rq path_to_dir1 path_to_dir2

AWK function taken from here:

http://stackoverflow.com/questions/2912224/find-duplicates-lines-based-on-some-delimited-fileds-on-line

(read -r passphrase; b58encode 80$( brainwallet_exponent "$passphrase" )$( brainwallet_checksum "$passphrase" ))
4

A bitcoin "brainwallet" is a secret passphrase you carry in the "wallet" of your brain.

The Bitcoin Brainwallet Private Key Calculator calculates the standard base58 encoded bitcoin private key from your "brainwallet" passphrase.

The private key is the most important bitcoin number. All other numbers can be derived from it.

This command uses 3 other functions - all 3 are defined on my user page:

1) brainwallet_exponent() - search for Bitcoin Brainwallet Exponent Calculator

2) brainwallet_checksum() - search for Bitcoin Brainwallet Exponent Calculator

3) b58encode() - search for Bitcoin Brainwallet Base58 Encoder

Do make sure you use really strong, unpredictable passphrases (30+ characters)!

http:brainwallet.org can be used to check the accuracy of this calculator.

function b58encode () { local b58_lookup_table=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z}); bc<<<"obase=58;ibase=16;${1^^}"|(read -a s; for b58_index in "${s[@]}" ; do printf %s ${b58_lookup_table[ 10#"$b58_index" ]}; done); }
4

A bitcoin "brainwallet" is a secret passphrase you carry in your brain.

The Bitcoin Brainwallet Private Key Base58 Encoder is the third of three functions needed to calculate a bitcoin PRIVATE key from your "brainwallet" passphrase.

This base58 encoder uses the obase parameter of the amazing bc utility to convert from ASCII-hex to base58. Tech note: bc inserts line continuation backslashes, but the "read s" command automatically strips them out.

I hope that one day base58 will, like base64, be added to the amazing openssl utility.

any command | while read line; do echo "[`date -Iseconds`] $line"; done
2014-02-07 22:27:29
User: ayosec
Functions: command echo read
3

Useful to add a timestamp to every line printed to stdout.

You can use `-Ins` instead of `-Iseconds` if you want more precision.

find . -name "*.URL" | while read file ; do cat "$file" | sed 's/InternetShortcut/Desktop Entry/' | sed '/^\(URL\|\[\)/!d' > "$file".desktop && echo "Type=Link" >> "$file".desktop ; done
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
8

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

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.

git status -s | grep -o ' \S*php$' | while read f; do php -l $f; done
2013-12-14 11:47:54
User: ruslan
Functions: grep read
Tags: git PHP lint
1

Checks for syntax errors in PHP files modified in current working copy of a Git repository.

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)

find /mnt/storage/profiles/ -maxdepth 1 -mindepth 1 -type d | while read d; do tarfile=`echo "$d" | cut -d "/" -f5`; destdir="/local/backupdir/"; tar -g "$destdir"/"$tarfile".snar -czf "$destdir"/"$tarfile"_`date +%F`.tgz -P $d; done
find /mnt/storage/profiles/ -maxdepth 1 -mindepth 1 -type d | while read d; do tarfile=`echo "$d" | cut -d "/" -f5`; destdir="/local/backupdir"; tar -czvf "$destdir"/"$tarfile"_`date +%F`.tgz -P $d; done
2013-12-05 19:18:03
User: jaimerosario
Functions: cut find read tar
1

Problem: I wanted to backup user data individually, using and incremental method. In this example, all user data is located in "/mnt/storage/profiles", and about 25 folders inside, each with a username ( /mnt/storage/profiles/mike; /mnt/storage/profiles/lucy ...)

I need each individual folder backed up, not the whole "/mnt/storage/profiles". So, using find while excluding directories depth and creating two variables (tarfile=username & desdir=destination), tar will create a .tgz file for each folder, resulting in a "mike_2013-12-05.tgz" and "lucy_2013-12-05.tgz".

find /mnt/storage/profiles/ -maxdepth 1 -mindepth 1 -type d | while read d; do tarfile=`echo "$d" | cut -d "/" -f5`; destdir="/local/backupdir/"; tar -czf $destdir/"$tarfile"_full.tgz -P $d; done
2013-12-05 19:07:17
User: jaimerosario
Functions: cut find read tar
1

Problem: I wanted to backup user data individually. In this example, all user data is located in "/mnt/storage/profiles", and about 25 folders inside, each with a username ( /mnt/storage/profiles/mike; /mnt/storage/profiles/lucy ...)

I need each individual folder backed up, not the whole "/mnt/storage/profiles". So, using find while excluding directories depth and creating two variables (tarfile=username & desdir=destination), tar will create a .tgz file for each folder, resulting in a "mike_full.tgz" and "lucy_full.tgz".

find . -user root
read -N1
2013-10-10 10:09:43
User: totti
Functions: read
Tags: read user
6

Usage exaple cmd

echo 'Sure to continue ??'; read -n1 choi; if [ "$choi" = 'y' ] || [ "$choi" = 'Y' ]; then echo -e '\nExecuting..'; else echo 'Aborted'; fi
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
find -f . png | while read line; do pngcrush -ow -brute $line; done
2013-09-25 13:40:14
User: mildfuzz
Functions: find read
0

Find's all png's in the current folder and all of its children

pngcrushes all results.

Destructive.