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,263 results
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).

for i in */; do echo tar -cjf "${i%/}.tar.bz2" "$i"; done
2013-08-20 22:13:28
User: sudopeople
Functions: echo tar
1

Remove the "echo" to actually archive.

Many similar commands are found on commandlinefu but I end up needing this very specific one from time to time.

To extract any of them, use the standard tar.bz2 extract command:

tar xvjf folder1.tar.bz2
output=$(ps -ef|grep -i java 2>/dev/null); for w in ${output[@]}; do if [[ $w =~ .*Xmx.* ]]; then result=$(grep -oP "[0-9]+" <<< $w); echo $result ;fi ; done| awk 'BEGIN {sum=0} {for(i=1; i<=NF; i++) sum+=$i } END {print sum}'
2013-08-13 14:04:17
User: massiverobot
Functions: awk echo grep ps
Tags: java jvm xmx
0

This will find all the -Xmx[BIGINT] running on a system, add them up for you and give you the total.

KEY_HASH=`openssl rsa -in $KEY_PATH -modulus -noout | openssl md5` && CRT_HASH=`openssl x509 -in $CERT_PATH -modulus -noout | openssl md5`; if [ "$KEY_HASH" != "$CRT_HASH" ];then echo "MD5 sums between .key and .crt files DO NOT MATCH";fi
google() { gg="https://www.google.com/search?q=";q="";if [[ $1 ]]; then for arg in "$@" ; do q="$q+$arg"; done ; if [[ -f /usr/bin/chromium ]]; then chromium "$gg"$q; else firefox -new-tab "$gg"$q; fi else echo 'Usage: google "[seach term]"'; fi }
function ytmp3() { while (($#)); do (cd ~/Music; echo "Extracting mp3 from $(youtube-dl -e $1)"; /usr/bin/youtube-dl -q -t --extract-audio --audio-format mp3 $1); shift; done ; }
2013-08-08 06:44:29
User: snipertyler
Functions: cd echo
2

Usage: ytmp3 "YTurl" "YTurl2" "YTurl3" "YTurlN"

Uses the shift command to let you extract the .mp3 from as many youtube urls as you like (or wherever else youtube-dl is supported)

*Requires youtube-dl

Orginal chunk of code:

youtube-dl -q -t --extract-audio --audio-format mp3 URL

taken from here http://www.commandlinefu.com/commands/view/9701/convert-youtube-videos-to-mp3

IFS=$(echo -en "\n\b"); input="/my/input/dir/*.flac"; mkdir -p $(dirname $f)/mp3; for f in $input; do ffmpeg -i $f -ab 196k -ac 2 -ar 48000 $(dirname $f)/mp3/$(basename "${f:0:${#f}-4}mp3"); done
google() { gg="https://www.google.com/search?q="; ff="firefox"; if [[ $1 ]]; then "$ff" -new-tab "$gg"$(echo ${1//[^a-zA-Z0-9]/+}); else echo 'Usage: google "[seach term]"'; fi }
2013-08-01 22:21:53
User: lowjax
Functions: echo
2

Usage: google "[search string]"

Example: google "something im searching for"

This will launch firefox and execute a google search in a new tab with the provided search string.

You must provide the path to your Firefox binary if using cygwin to $ff or create an alias like follows:

alias firefox='/cygdrive/c/Program Files (x86)/Mozilla Firefox/firefox.exe'

Most Linux flavors with Firefox installed will use just ff="firefox" and even OSX.

rhost() { if [[ $1 =~ ^[0-9]+$ ]]; then sed -i "$1"d ${HOME}/.ssh/known_hosts; else echo "rhost [n]"; fi }
2013-08-01 21:10:34
User: lowjax
Functions: echo sed
-1

Quickly remove the conflicting line (key) from current users known_hosts file when there is an SSH host conflict. Very nice when you get tired of writing out full commands. Ideally you would place this into your .bash_profile

Usage: rhost [n]

Example: rhost 33 (removes line 33 from ~/.ssh/known_hosts)

Function assumes the $HOME exists, you could alternatively use "~/.ssh/known_hosts"

Mac OSX likes a space for sed -i "$1" d

for m in `df -P | awk -F ' ' '{print $NF}' | sed -e "1d"`;do n=`df -P | grep "$m$" | awk -F ' ' '{print $5}' | cut -d% -f1`;i=0;if [[ $n =~ ^-?[0-9]+$ ]];then printf '%-25s' $m;while [ $i -lt $n ];do echo -n '=';let "i=$i+1";done;echo " $n";fi;done
2013-07-29 20:12:39
User: drockney
Functions: awk cut echo grep printf sed
Tags: bash
5

Automatically drops mount points that have non-numeric sizes (e.g. /proc). Tested in bash on Linux and AIX.

find (); { ls $1 | while read line; do [[ -d $1/$line ]] && find $1/$line $2 || echo $1/$line | grep $2; done; }
2013-07-28 22:21:39
User: a8ksh4
Functions: echo find grep ls read
3

basic find implementation for systems that don't actually have find, like an android console without busybox installed.

echo -e "\e[3$(( $RANDOM * 6 / 32767 + 1 ))mHello World!"
2013-07-28 13:01:12
User: nst
Functions: echo
Tags: bash color random
0

The expression $(( $RANDOM * 6 / 32767 + 1 )) generates a random number between 1 and 6, which is then inserted into the escape sequence \e[3_m to switch the foreground color of the terminal to either red, green, yellow, blue, purple or cyan.

The color can be reset using the escape sequence \e[0m.

The full list of colors can be found here: https://wiki.archlinux.org/index.php/Color_Bash_Prompt#List_of_colors_for_prompt_and_Bash

for i in *.CR2; do dcraw -c -a -h $i | ppmtojpeg > `basename $i CR2`JPG; echo $i done; done
2013-07-25 14:58:15
User: optyler
Functions: echo
0

This command takes all CR2 files in the current directory and convert them into JPG

echo "q+werty%3D%2F%3B" | php -r "echo urldecode(file_get_contents('php://stdin'));"
2013-07-17 11:37:36
User: kartikssj
Functions: echo
Tags: urldecod PHP
0

Using PHP shell to URL decode a string.

while [ 1 ]; do echo -e "220 ProFTPD 1.3.3c Server [ProFTPD] \nFAILED FTP ATTEMPT - PORT 21" | nc -vvv -l 192.168.1.65 21 >> /var/log/honeylog.log 2>> /var/log/honeylog.log; done
2013-07-16 19:05:37
User: xmuda
Functions: echo
-1

[root@dhcppc1 windows]# cat /var/log/honeylog.log

Connection from 192.168.1.71 port 21 [tcp/ftp] accepted

Connection from 192.168.1.65 port 21 [tcp/ftp] accepted

[root@dhcppc1 windows]# nc 192.168.1.65 21

220 ProFTPD 1.3.3c Server [ProFTPD]

FAILED FTP ATTEMPT - PORT 21

*You can not run it if you have activated the ftp server.

ps aux | grep $(echo $1 | sed "s/^\(.\)/[\1]/g")
2013-07-16 10:10:51
User: opexxx
Functions: echo grep ps sed
Tags: sed grep ps
1

grep по ps aux

echo "disable" > /sys/firmware/acpi/interrupts/gpeXX
2013-07-16 03:53:20
User: m33600
Functions: echo
0

change gpeXX by the culprit you discovered on phase 1

In case of this example, the culprit is the biggest number, ie, gpe1C

/sys/firmware/acpi/interrupts/ff_gbl_lock: 0 enabled

/sys/firmware/acpi/interrupts/gpe01: 0 enabled

/sys/firmware/acpi/interrupts/gpe06: 0 enabled

/sys/firmware/acpi/interrupts/gpe17: 2 enabled

/sys/firmware/acpi/interrupts/gpe18: 0 enabled

/sys/firmware/acpi/interrupts/gpe1C: 19 enabled

This procedure,if solved this universal issue all linix distros are experimenting for more than 2 years, may be included at startup, via cron. But try first commandline.

while curl -dsL example.com 2>&1 | grep 503;do sleep 8;done;echo server up
echo 'HelloWorld!' | gpg --symmetric | base64
2013-07-06 08:30:35
User: ryanchapman
Functions: echo gpg
Tags: GPG
0

Need to encrypt something from the command line? I've used this before to encrypt passwords with a master password that was shared amongst a team.

To decrypt:

~$ echo -n 'jA0EAwMCPdknsznAww5gySL1/quqhXg6QgQkIz5abzGP5EZgTbXCFU+y6dP8ySWovytc' | base64 --decode | gpg --decrypt

gpg: CAST5 encrypted data

Enter passphrase: secret

gpg: encrypted with 1 passphrase

HelloWorld!