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,227 results
echo -e "HEAD / HTTP/1.1\nHost: slashdot.org\n\n" | nc slashdot.org 80 | egrep "Bender|Fry" | sed "s/X-//"
2009-07-30 19:15:07
Functions: echo egrep sed
5

slashdot.org webserver adds an X-Bender or X-Fry HTTP header to every response!

buffer () { tty -s && return; tmp=$(mktemp); cat > "${tmp}"; if [ -n "$1" ] && ( ( [ -f "$1" ] && [ -w "$1" ] ) || ( ! [ -a "$1" ] && [ -w "$(dirname "$1")" ] ) ); then mv -f "${tmp}" "$1"; else echo "Can't write in \"$1\""; rm -f "${tmp}"; fi }
2009-07-27 20:21:15
User: Josay
Functions: cat echo mv rm tty
Tags: redirection
2

A common mistake in Bash is to write command-line where there's command a reading a file and whose result is redirected to that file.

It can be easily avoided because of :

1) warnings "-bash: file.txt: cannot overwrite existing file"

2) options (often "-i") that let the command directly modify the file

but I like to have that small function that does the trick by waiting for the first command to end before trying to write into the file.

Lots of things could probably done in a better way, if you know one...

while true ; do IFS="" read i; echo "$i"; sleep .01; done < <(tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]")
2009-07-22 03:59:07
Functions: dd echo grep read sleep tr true
Tags: color
2

Same as above but slooooow it down

ran=$(head /dev/urandom | md5sum); MAC=00:07:${ran:0:2}:${ran:3:2}:${ran:5:2}:${ran:7:2}; sudo ifconfig wlan0 down hw ether $MAC; sudo ifconfig wlan0 up; echo ifconfig wlan0:0
2009-07-16 16:21:44
User: workingsmart
Functions: echo head ifconfig sudo
7

Next time you are leaching off of someone else's wifi use this command before you start your bittorrent ...for legitimate files only of course.

It creates a hexidecimal string using md5sum from the first few lines of /dev/urandom and splices it into the proper MAC address format. Then it changes your MAC and resets your wireless (wlan0:0).

diff <(echo "$a") <(echo "$b")
2009-07-15 07:26:23
User: olorin
Functions: diff echo
25

You got some results in two variables within your shell script and would like to find the differences? Changes in process lists, reworked file contents, ... . No need to write to temporary files. You can use all the diff parameters you'll need. Maybe anything like $ grep "^>"

is helpful afterwards.

for i in `rpm -qva | sort ` ; do ; echo "===== $i =====" ; rpm -qvl $i ; done > /tmp/pkgdetails
2009-07-14 20:34:55
User: tkunz
Functions: echo rpm sort
0

This will create the file /tmp/pkgdetails, which will contain a listing of all the files installed on your RPM-based system (RedHat, Fedora, CentOS, etc). Useful should the RPM system/database become corrupted to find which package installed which files.

dpkg -S `which nm` | cut -d':' -f1 | (read PACKAGE; echo "[${PACKAGE}]"; dpkg -s "${PACKAGE}"; dpkg -L "${PACKAGE}") | less
2009-07-14 20:21:48
User: mohan43u
Functions: cut echo read
Tags: which dpkg
2

In Debian based distros, this command will list 'binutils' package details which contains 'nm' command. You can replace 'nm' to any other command.

infile=$1 for i in $(cat $infile) do echo $i | tr "," "\n" | sort -n | tr "\n" "," | sed "s/,$//" echo done
2009-07-12 21:23:37
User: iframe
Functions: cat echo sed sort tr
Tags: cat bash sort sed tr
0

Save the script as: sort_file

Usage: sort_file < sort_me.csv > out_file.csv

This script was originally posted by Admiral Beotch in LinuxQuestions.org on the Linux-Software forum.

I modified this script to make it more portable.

tempo=33; slope=10; maxfreq=888; function sinus { echo "s($1/$slope)*$maxfreq"|bc -l|tr -d '-'; }; for((i=1;;i++)); do beep -l$tempo -f`sinus $i`; done
2009-07-10 21:24:11
User: 5z474n
Functions: bc echo tr
1

Infinitely plays beeps with sinusoidally changing sound frequency. Ideal for alarm on an event.

echo -e "\e[31m"; while $t; do for i in `seq 1 30`;do r="$[($RANDOM % 2)]";h="$[($RANDOM % 4)]";if [ $h -eq 1 ]; then v="\e[1m $r";else v="\e[2m $r";fi;v2="$v2 $v";done;echo -e $v2;v2="";done;
2009-07-10 04:20:43
User: Patola
Functions: echo
Tags: color
3

It's the same command as submitted, but first with a command to make all characters green. It's the only way it looked "matrix-like" on my gnome-terminal.

export IFS=$(echo -e "\n")
2009-07-09 15:25:37
User: darkpand
Functions: echo export
8

When you use a "for" construct, it cycles on every word. If you want to cycle on a line-by-line basis (and, well, you can't use xargs -n1 :D), you can set the IFS variable to .

echo alias grep=\'grep --color=auto\' >> ~/.bashrc ; . ~/.bashrc
2009-07-05 07:44:13
User: 0x2142
Functions: alias echo
Tags: color grep
7

This will create a permanent alias to colorize the search pattern in your grep output

echo 2+3 |bc
2009-07-04 22:03:41
User: Kaio
Functions: echo
0

Handy use of bc in the command line. No need to get 'into' the bc to perform calculations

for file in `find /var/log/ -type f -size +5000k`; do echo " " > $file; done
2009-07-03 16:26:36
User: jemmille
Functions: echo file
0

Empties all files in /var/log over 5000k. Useful if /var goes crazy or if you just haven't cleaned up in a while.

function duf { du -sk "$@" | sort -n | while read size fname; do for unit in k M G T P E Z Y; do if [ $size -lt 1024 ]; then echo -e "${size}${unit}\t${fname}"; break; fi; size=$((size/1024)); done; done; }
? () { echo "$*" | bc -l; }
2009-06-28 20:15:30
User: fizz
Functions: bc echo
56

defines a handy function for quick calculations from cli.

once defined:

? 10*2+3
S=`pidof skype`;grep heap /proc/$S/maps|cut -f1 -d' '|awk -F- '{print "0x" $1 " 0x" $2}'|xargs echo "du me t ">l;gdb -batch -p $S -x l>/dev/null 2>&1;strings t|grep \(smirk|head -n1
2009-06-26 20:03:17
User: alvieboy
Functions: awk cut echo grep head xargs
0

Skype has an internal regex which depicts the emoticons it supports. However you cannot simply search the binary file for it. This small 181 character line will do just that, provided skype is running. And of course, only works in linux.

echo "Hello World." | tee -a hello.txt
2009-06-26 00:55:59
Functions: echo tee
Tags: tee
1

When plumbers use pipes, they sometimes need a T-joint. The Unix equivalent to this is 'tee'. The -a flag tells 'tee' to append to the file, rather than clobbering it.

Tested on bash and tcsh.

echo "aplay path/to/song" |at [time]
2009-06-25 19:33:37
User: brausen
Functions: at echo
Tags: alarm
8

Set an alarm to starts in specific time.

echo "-------------" >> nicinfo.txt; echo "computer name x" >> nicinfo.txt; ifconfig | grep status >> nicinfo.txt; ifconfig | grep inet >> nicinfo.txt; ifconfig | grep ether >> nicinfo.txt; hostinfo | grep type >> nicinfo.txt;
echo $numbers | sed "s/\( \|$\)/\n/g" | sort -nu | tr "\n" " " | sed -e "s/^ *//" -e "s/ $//"
2009-06-24 15:12:17
User: chickenzilla
Functions: echo sed sort tr
-1

You can replace "sort -nu" with "sort -u" for a word list sorted or "sort -R" for a random-sorted line

(edit: corrected)

echo "${STRING}" | tr '[A-Z]' '[a-z]' | awk '{print toupper(substr($0,1,1))substr($0,2);}'
2009-06-23 21:11:34
User: mohan43u
Functions: awk echo tr
Tags: awk tr
0

Helpful when we want to do mass file renaming(especially mp3s).

echo {0..1}{0..1}{0..1}{0..1}
2009-06-23 17:30:20
User: dennisw
Functions: echo
17

If you should happen to find yourself needing some binary numbers, this is a quickie way of doing it. If you need more digits, just add more "{0..1}" sequences for each digit you need. You can assign them to an array, too, and access them by their decimal equivalent for a quickie binary to decimal conversion (for larger values it's probably better to use another method). Note: this works in bash, ksh and zsh. For zsh, though, you'll need to issue a setopt KSH_ARRAYS to make the array zero-based.

binary=({0..1}{0..1}{0..1}{0..1})

echo ${binary[9]}
:r! echo %
2009-06-19 12:17:28
User: miccaman
Functions: echo
Tags: vim
1

insert filename

Normal mode: "%p

Insert mode: %

a=`printf "%*s" 16`;b=${a//?/{0..1\}}; echo `eval "echo $b"`