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,256 results
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"
echo 0$(awk '/Pss/ {printf "+"$2}' /proc/$PID/smaps)|bc
2013-09-26 18:20:22
User: atoponce
Functions: awk echo
Tags: Linux awk echo bc proc
5

The "proportional set size" is probably the closest representation of how much active memory a process is using in the Linux virtual memory stack. This number should also closely represent the %mem found in ps(1), htop(1), and other utilities.

echo "This is the message body" | mutt -s "Message subject" -a file_to_attach.zip fred@example.com
2013-09-26 08:05:26
User: jedifu
Functions: echo
Tags: Linux shell email
1

This uses mutt to send the file, and doesn't require uuencode etc

(IFS=,; echo "${array[*]}")
2013-09-25 10:36:38
User: aspiers
Functions: echo
2

This type of join is clearly documented in the bash manual. Only the first character of IFS is used for the delimiter.

for i in `find -L /var/ -wholename \*log\* -type d`; do COUNT=`ls -1U $i | wc -l`; if [ $COUNT -gt 10 ]; then echo $i $COUNT; fi; done
for ff in directory; do numLines=`wc -l $ff`; numLines=$(echo $numLines | sed 's/ .*//g'); min=$(sort -nrk 1 $ff | tail -1); if [ $numLines -gt 100 ]; then echo $min >> minValues; fi;done;
for menu in {1..4}; do echo -e "<ul>\n <li>menu $menu</li>\n <ul>"; for items in {1..5}; do echo " <li>item $items</li>"; if [ $items -eq 5 ];then echo -e " </ul>";fi;done; echo "</ul>";done | xclip
2013-09-22 06:34:32
User: qcjn
Functions: echo
-1

This will create rapidly an html menu. You can paste it in you're html page with the middle button(the wheel). I didn't test it in an html with css, but it seems alright.

echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bash_profile
2013-09-19 03:25:14
Functions: echo
0

If the HISTTIMEFORMAT is set, the time stamp information associated with each history entry is written to the history file, marked with the history comment character.

echo '#! /usr/bin/ksh\ncat $2 | openssl dgst -sha256 | read hashish; if [[ $hashish = $1 ]]; then echo $2: OK; else echo $2: FAILED; fi;' > shacheck; chmod +x shacheck; cat hashishes.sha256 | xargs -n 2 ./shacheck;
2013-09-18 21:51:20
User: RAKK
Functions: cat chmod echo read xargs
0

This command is used to verify a sha256sum-formatted file hash list on IBM AIX or any other UNIX-like OS that has openssl but doesn't have sha256sum by default. Steps:

1: Save to the filesystem a script that:

A: Receives as arguments the two parts of one line of a sha256sum listing

B: Feeds a file into openssl on SHA256 standard input hash calculation mode, and saves the result

C: Compares the calculated hash against the one received as argument

D: Outputs the result in a sha256sum-like format

2: Make the script runnable

3: Feed the sha256sum listing to xargs, running the aforementioned script and passing 2 arguments at a time

echo '#! /usr/bin/ksh\necho `cat $1 | openssl dgst -sha256` $1' > sslsha256; chmod +x sslsha256; find directory -type f -exec ./sslsha256 \{\} \;
2013-09-18 17:37:50
User: RAKK
Functions: chmod echo find
0

This command is for producing GNU sha256sum-compatible hashes on UNIX systems that don't have sha256sum but do have OpenSSL, such as stock IBM AIX.

1.- Saves a wrapper script for UNIX find that does the following:

A.- Feeds a file to openssl on SHA256 hash calculation mode

B.- Echoes the output followed by the filename

2.- Makes the file executable

3.- Runs find on a directory, only processing files, and running on each one the wrapper script that calculates SHA256 hashes

Pending is figuring out how to verify a sha256sum file on a similar environment.

echo -n 023135 | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie' | nc -4u -q1 -p5001 192.168.0.100 2000
2013-09-18 14:31:47
User: sucotronic
Functions: echo perl
1

Use it to send raw data to a networked device. Used to interact with relay controller board whose documentation is lost, so use wireshark to sniff the sent data and replayed using the command.

color () { local color=39; local bold=0; case $1 in green) color=32;; cyan) color=36;; blue) color=34;; gray) color=37;; darkgrey) color=30;; red) color=31;; esac; if [[ "$2" == "bold" ]]; then bold=1; fi; echo -en "\033[${bold};${color}m"; }
for i in `ip addr show dev eth1 | grep inet | awk '{print $2}' | cut -d/ -f1`; do echo -n $i; echo -en '\t'; host $i | awk '{print $5}'; done
for i in {1..6};do c=;d=;for u in `eval echo {1..$i}`;do c="$c../";d="$d..";eval "$d(){ cd $c;}"; eval "$d.(){ cd $c;}";done;done
2013-09-04 20:12:45
User: bbbco
Functions: echo eval
Tags: cd eval dot
1

Use dots to cd down directories instead of having to remember all of the pesky back slashes!

Better yet, works on even and odd number of dots! Now, just estimate how far down you want to traverse.

sh -c 'url="http://youtu.be/MejbOFk7H6c"; vid="`for i in ".*youtu\.be/\([^\/&?#]\+\)" ".*youtu.\+v[=/]\([^\/&?#]\+\)" ".*youtu.\+embed/\([^\/&?#]\+\)"; do expr "${url}" : "${i}"; done`"; if [ -n "${vid}" ]; then echo ${vid}; else echo "${url}"; fi'
2013-09-04 19:33:09
User: qwertyroot
Functions: echo sh
2

url can be like any one of followings:

url="MejbOFk7H6c" url="http://youtu.be/MejbOFk7H6c" url="https://youtube.com/watch?feature=player_embedded&v=MejbOFk7H6c#t" url="//www.youtube.com/v/MejbOFk7H6c?hl=ru_RU&version=3&rel=0" url="http://www.youtube.com/embed/MejbOFk7H6c?feature=player_embedded"

If url mismatching, whole url will be returned.

port=32768; while netstat -atn | grep -q :$port; do port=$(expr $port + 1); done; echo $port
printTextInColorRed () { echo -e '\033[01;31m\033[K'"$@"'\033[m\033[K' ;} ## print text/string in color red
2013-08-28 10:06:59
User: totti
Functions: echo
1

eg:

printTextInColorRed foo bar

foo bar [in red color]

mogrify -format gif -auto-orient -thumbnail 250x90 '*.JPG'&&(echo "<ul>";for i in *.gif;do basename=$(echo $i|rev|cut -d. -f2-|rev);echo "<li style='display:inline-block'><a href='$basename.JPG'><img src='$basename.gif'></a>";done;echo "</ul>")>list.html
2013-08-25 20:45:49
User: ysangkok
Functions: cut echo
1

The input images are assume to have the "JPG" extension. Mogrify will overwrite any gif images with the same name! Will not work with names with spaces.

word=$(shuf -n4 /usr/share/dict/words); for w in ${word[@]}; do w=${w^}; w=${w//\'/}; p+=$w; done; echo $p
for i in *.flv *.mkv *.avi; do mplayer -ao null -vo null -ss 0 -endpos 1 >/dev/null "$i" 2> >(grep -qi error && echo >&2 "$i seems bad"); done
2013-08-20 22:23:11
User: sputnick
Functions: echo grep
0

If you want avoid to be annoyed when playing your favourite video files with your video player, first run this command to stash wrong files (and test tricks to play these wrong files).