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,208 results
echo -e "${PATH//://\n}" >/tmp/allpath; grep -Fh -f /tmp/allpath /var/lib/dpkg/info/*.list|grep -vxh -f /tmp/allpath >/tmp/installedinpath ; find ${PATH//:/ } |grep -Fxv -f /tmp/installedinpath
2009-09-09 05:33:14
User: kamathln
Functions: echo find grep
Tags: Debian dpkg PATH
0

OS: Debian based (or those that use dpkg)

Equivalent to doing a dpkg -S on each file in $PATH, but way faster.

May report files generated though postinstall scripts and such. For example . It will report /usr/bin/vim .. which is not not a file installed directly by dpkg, but a link generated by alternatives hooks

echo src::${PATH} | awk 'BEGIN{pwd=ENVIRON["PWD"];RS=":";FS="\n"}!$1{$1=pwd}$1!~/^\//{$1=pwd"/"$1}{print $1}'
2009-09-09 04:03:46
User: arcege
Functions: awk echo
Tags: awk echo PATH
-2

Removes trailing newline; colon becomes record separator and newline becomes field separator, only the first field is ever printed. Replaces empty entries with $PWD. Also prepend relative directories (like ".") with the current directory ($PWD). Can change PWD with env(1) to get tricky in (non-Bourne) scripts.

echo $PATH | tr \: \\n
2009-09-09 02:10:04
User: crk
Functions: echo tr
Tags: bash echo tr PATH
12

This version uses Pipes, but is easier for the common user to grasp... instead of using sed or some other more complicated method, it uses the tr command

prev=0;next=1;echo $prev;while(true);do echo $next;sum=$(($prev+$next));prev=$next;next=$sum;sleep 1;done
while [ -n "`pgrep wget`" ]; do sleep 2 ;done; [ -e "/tmp/nosleep"] || echo mem >/sys/power/state
2009-09-06 05:51:20
User: kamathln
Functions: echo sleep
1

[Note: This command needs to be run as root].

If you are downloading something large at night, you can start wget as a normal user and issue the above command as root. When the download is done, the computer will automatically go to sleep. If at any time you feel the computer should not go to sleep automatically(like if you find the download still continuing in the morning), just create an empty file called nosleep in /tmp directory.

echo capitalize | { dd bs=1 count=1 conv=ucase 2> /dev/null; cat ;}
echo "SECRET MESSAGE" | gpg -e --armor -s | sendmail USER@DOMAIN.COM
2009-09-04 20:47:12
User: flip387
Functions: echo gpg sendmail
3

A very simple command to send a signed and encrypted message from the command line using GPG Keys

du -a --max-depth=1 | sort -n | cut -d/ -f2 | sed '$d' | while read i; do if [ -f $i ]; then du -h "$i"; else echo "$(du -h --max-depth=0 "$i")/"; fi; done
2009-09-03 20:43:43
User: nickwe
Functions: cut du echo read sed sort
3

Based on the MrMerry one, just add some visuals to differentiate files and directories

find . -maxdepth 1 -type d|xargs du -a --max-depth=0|sort -rn|cut -d/ -f2|sed '1d'|while read i;do echo "$(du -h --max-depth=0 "$i")/";done;find . -maxdepth 1 -type f|xargs du -a|sort -rn|cut -d/ -f2|sed '$d'|while read i;do du -h "$i";done
2009-09-03 20:33:21
User: nickwe
Functions: cut du echo find read sed sort xargs
2

Based on the MrMerry one, just add some visuals and sort directory and files

echo $((3.0/5.0))
echo sortmeplease|sed 's/./&\n/g'|sort|tr -d '\n'
echo sortmeplease | awk '{l=split($1,a,"");asort(a);while(x<=l){printf "%s",a[x];x++ }print "";}'
2009-09-03 10:22:39
User: foob4r
Functions: awk echo
-1

using awk

missed the last char thanks @Josay

echo {1..199}" sheep," | espeak -v english -s 80
2009-09-03 10:08:12
User: MrMerry
Functions: echo
6

Can change language and speed, see espeak man page for options. (Install espeak in your linux distro via yum or apt-get)

For insomniacs you may need to enclose in a while true; do ...; done loop ;)

echo sortmeplease | grep -o . | sort | tr -d '\n'; echo
2009-09-03 00:52:49
User: MrMerry
Functions: echo grep sort tr
1

Sorts a character string, using common shell commands.

echo "5 k 3 5 / p" | dc
2009-09-03 00:21:54
User: xamaco
Functions: echo
1

using bc is for sissies. dc is much better :-D

Polish notation will rule the world...

for dnsREC in $(curl -s http://www.iana.org/assignments/dns-parameters |grep -Eo ^[A-Z\.]+\ |sed 's/TYPE//'); do echo -n "$dnsREC " && dig +short $dnsREC IANA.ORG; done
xmms2 pause && echo "xmms2 play" | at now +5min
2009-08-30 04:35:10
User: Vrekk
Functions: at echo
2

you can also run "xmms2 pause & at now +5min

echo "${PATH//:/$'\n'}"
for i in 192.168.1.{61..71};do ping -c 1 $i &> /dev/null && echo $i;fi;done
2009-08-26 06:04:24
User: AlecSchueler
Functions: echo ping
Tags: Network ssh bash
-2

If you need to ssh into a computer on the local network but you're unsure of the ip to use, then ping them and see if you get a response. If you do, print out the address you got it from. Adjust the range to suit your network.

echo "xmms2 play" | at 6:00
2009-08-26 04:20:24
User: Vrekk
Functions: at echo
1

Nice little alarm clock to wake you up on time (hopefully).

You can also do 'echo "vlc path/to/song" | at 6:00

echo "0t${currentEpoch}=Y" | /usr/bin/adb
2009-08-25 12:17:01
User: verboEse
Functions: echo
0

this works on Solaris, so not better than the "only-GNU"-tool :-(

I think, there is no one-liner for this, that will work on all *nix-es

echo "aBcDeFgH123" | tr a-z A-Z
echo 1234567890 | awk '{ print strftime("%c", $0); }'
2009-08-25 09:37:54
User: alvinx
Functions: awk echo
8

- convert unixtime to human-readable with awk

- useful to read logfiles with unix-timestamps, f.e. squid-log:

sudo tail -f /var/log/squid3/access.log | awk '{ print strftime("%c ", $1) $0; }

echo `date +%m/%d/%y%X |awk '{print $1;}' `" => "` cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2, $3;}'` >> datetmp.log
2009-08-24 21:26:29
User: ninadsp
Functions: awk cat echo
7

Uses the data in the /proc system, provided by the acpid, to find out the CPU temperature. Can be run on systems without lm-sensors installed as well.

y=http://www.youtube.com;for i in $(curl -s $f|grep -o "url='$y/watch?v=[^']*'");do d=$(echo $i|sed "s|url\='$y/watch?v=\(.*\)&.*'|\1|");wget -O $d.flv "$y/get_video.php?video_id=$d&t=$(curl -s "$y/watch?v=$d"|sed -n 's/.* "t": "\([^"]*\)",.*/\1/p')";done
2009-08-22 21:31:29
User: matthewbauer
Functions: echo grep sed
3

This will download a Youtube playlist and mostly anything http://code.google.com/apis/youtube/2.0/reference.html#Video_Feeds

The files will be saved by $id.flv