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,211 results
grep -q '\<lm\>' /proc/cpuinfo && echo 64 bits || echo 32 bits
2013-02-09 13:01:36
User: sputnick
Functions: echo grep
1

This command tell you if your hardware is 32 or 64 bits even if you install a 32bits OS on a 64 bits hardware.

If your distro don't support the -q switch, try doing :

grep &>/dev/null '\<lm\>' /proc/cpuinfo && echo 64 bits || echo 32 bits
(echo -e "HTTP/1.1 200 Ok\n\r"; tail -f /var/log/syslog) | nc -l 1234
2013-02-09 06:15:42
User: adimania
Functions: echo tail
4

This one is tried and tested for Ubuntu 12.04. Works great for tailing any file over http.

load=`uptime|awk -F',' '{print $3}'|awk '{print $3}'`; if [[ $(echo "if ($load > 1.0) 1 else 0" | bc) -eq 1 ]]; then notify-send "Load $load";fi
2013-02-06 08:30:24
User: adimania
Functions: awk echo
0

I run this via crontab every one minute on my machine occasionally to see if a process is eating up my system's resources.

FOR %%c in (C:\Windows\*.*) DO (echo file %%c)
2013-01-31 15:19:54
User: jmcclosk
Functions: echo file
0

You can implement a FOR loop to act on one or more files returned from the IN clause. We originally found this in order to GPG decrypt a file using wildcards (where you don't know exactly the entire file name, i.e.: Test_File_??????.txt, where ?????? = the current time in HHMMSS format). Since we won't know the time the file was generated, we need to use wildcards. And as a result of GPG not handling wildcards, this is the perfect solution. Thought I would share this revelation. :-)

echo 'foo' | tee >(wc -c) >(grep o) >(grep f)
2013-01-31 09:54:18
User: totti
Functions: echo grep tee wc
Tags: tee output input
5

Output of a command as input to many

dd if=/dev/zero of=T bs=1024 count=10240;mkfs.ext3 -q T;E=$(echo 'read O;mount -o loop,offset=$O F /mnt;'|base64|tr -d '\n');echo "E=\$(echo $E|base64 -d);eval \$E;exit;">F;cat <(dd if=/dev/zero bs=$(echo 9191-$(stat -c%s F)|bc) count=1) <(cat T;rm T)>>F
2013-01-31 01:38:30
User: rodolfoap
5

This is just a proof of concept: A FILE WHICH CAN AUTOMOUNT ITSELF through a SIMPLY ENCODED script. It takes advantage of the OFFSET option of mount, and uses it as a password (see that 9191? just change it to something similar, around 9k). It works fine, mounts, gets modified, updated, and can be moved by just copying it.

USAGE: SEE SAMPLE OUTPUT

The file is composed of three parts:

a) The legible script (about 242 bytes)

b) A random text fill to reach the OFFSET size (equals PASSWORD minus 242)

c) The actual filesystem

Logically, (a)+(b) = PASSWORD, that means OFFSET, and mount uses that option.

PLEASE NOTE: THIS IS NOT AN ENCRYPTED FILESYSTEM. To improve it, it can be mounted with a better encryption script and used with encfs or cryptfs. The idea was just to test the concept... with one line :)

It applies the original idea of http://www.commandlinefu.com/commands/view/7382/command-for-john-cons for encrypting the file.

The embedded bash script can be grown, of course, and the offset recalculation goes fine. I have my own version with bash --init-file to startup a bashrc with a well-defined environment, aliases, variables.

echo 'echo "cd `pwd`" >> $VIRTUAL_ENV/bin/postactivate' >> $VIRTUAL_ENV/../postmkvirtualenv
echo '#!/bin/bash' > junk.sh ; find . -iname *.pdf -type f -printf \p\s\2\a\s\c\i\i\ \"%p\"\ \ \"%p\.\t\x\u\"\;\ \p\a\r\ \<\"%p\.\t\x\u\"\ \>\"%p\.\t\x\t\"\ \;\ \r\m\ \"%p\.\t\x\u\"\ \\n >>junk.sh; chmod 766 junk.sh; ./junk.sh ; rm junk.sh
2013-01-27 21:29:08
User: p0g0
Functions: chmod echo find rm
0

Linux users wanting to extract text from PDF files in the current directory and its sub-directories can use this command. It requires "bash", "ps2ascii" and "par", and the PARINIT environment variable sanely set (see man par). WARNING: the file "junk.sh" will be created, run, and destroyed in the current directory, so you _must_ have sufficient rights. Edit the command if you need to avoid using the file name "junk.sh"

grep -q ' lm ' /proc/cpuinfo; [ $? -eq 0 ] && echo '64bit supported'
2013-01-24 21:41:56
User: mzet
Functions: echo grep
0

it shows whether your CPU supports 64 bit (x86-64) mode. uname -a only shows whether you have 64 bit (x86-64) or 32bit (i386) OS installed, this one-liner answers question: Can I install 64bit OS on this machine?

su -c 'sync; echo 3 > /proc/sys/vm/drop_caches'
for i in {a..z}; do echo $(cat ~/.bash_history | grep ^$i.* | wc -l) $i; done | sort -n -r
2013-01-23 18:59:13
User: yaMatt
Functions: cat echo grep sort wc
0

Kind of fun if you're that was inclined. I figured most of my commands start with s. sudo, screen, ssh etc. This script tells me what else they start with.

find $folder -name "[1-9]*" -type f -print|while read file; do echo $file $(sed -e '/^$/Q;:a;$!N;s/\n //;ta;s/ /_/g;P;D' $file|awk '/^Received:/&&!r{r=$0}/^From:/&&!f{f=$0}r&&f{printf "%s%s",r,f;exit(0)}');done|sort -k 2|uniq -d -f 1
2013-01-21 22:50:51
User: lpb612
Functions: awk echo find read sed sort uniq
1

# find assumes email files start with a number 1-9

# sed joins the lines starting with " " to the previous line

# gawk print the received and from lines

# sort according to the second field (received+from)

# uniq print the duplicated filename

# a message is viewed as duplicate if it is received at the same time as another message, and from the same person.

The command was intended to be run under cron. If run in a terminal, mutt can be used:

mutt -e "push otD~=xq" -f $folder

alias locate='if [ $((`date +%s`-`eval $(stat -s /var/db/locate.database); echo $st_mtime`)) -gt 3600 ]; then echo "locate: db is too old!">/dev/stderr; sudo /usr/libexec/locate.updatedb; fi; locate -i'
2013-01-21 17:45:50
User: jhyland87
Functions: alias echo locate stat sudo
Tags: locate osx mac
0

MAC OSX doesn't come with a locate command, This will do the same thing as the locate command on a typical Linux OS.

Simply add it to your ~/.bash_profile

largest() { dir=${1:-"./"}; count=${2:-"10"}; echo "Getting top $count largest files in $dir"; du -sx "$dir/"* | sort -nk 1 | tail -n $count | cut -f2 | xargs -I file du -shx file; }
2013-01-21 09:45:21
User: jhyland87
Functions: cut du echo file sort tail xargs
1

You can simply run "largest", and list the top 10 files/directories in ./, or you can pass two parameters, the first being the directory, the 2nd being the limit of files to display.

Best off putting this in your bashrc or bash_profile file

while [[ 1 ]]; do n=( */* ); s=${n[$(($RANDOM%${#n[@]}))]}; echo -e " - $s"; mpg123 -q "$s"; done
2013-01-18 09:20:58
User: XayOn
Functions: echo mpg123
0

Play files in shuffle mode with bash and mpg123.

Why bother using big-as-hell stuff like mplayer? This will play all your music files contained in */* (in my case author/song.format) with bash and mplayer showing a nice output.

mkalias () { echo "alias $1=\"$2\"" >> ~\.bash_aliases }
2013-01-14 13:56:35
User: xr09
Functions: echo
Tags: alias function
1

Is used like this:

mkalias rmcache "rm -rfv app/cache/*"

find . | while read line; do test `stat -c %u $line` -eq 1003 && chown android:android $line && echo $line; done
if date -d 2006-10-10 >> /dev/null 2>&1; then echo 1; else echo 0; fi
2013-01-10 10:35:15
Functions: date echo
0

On CentOS at least, date returns a boolean for the common date string formats, including YYYY-MM-DD. In the sample output, you can see various invalid dates returning 0 whereas a simple regex check would return 1 for the invalid dates.

-d, --date=STRING display time described by STRING, not `now'

The version of date on OS X does not appear to have this same option.

echo "main(i){for(i=0;;i++)putchar(((i*(i>>8|i>>9)&46&i >>8))^(i&i>>13|i>>6));}" | gcc -x c - && ./a.out | aplay
2013-01-09 21:48:23
User: r0nd0n
Functions: echo gcc
0

Something I pulled off 4chan, it plays a tune.

echo -e "You are a jerk\b\b\b\bwonderful person" | pv -qL $[10+(-2 + RANDOM%5)]
2013-01-09 19:18:07
User: joeheyming
Functions: echo
1

The output will show jerk, then wonderful person since echo parses the \b character.

echo "|MENU| |DOWN| |UP/BRIGHT| |ENTER| |AUTO| |OFF|"|osd_cat -p bottom -o -40 -i 1575
2013-01-02 07:45:54
User: hawo
Functions: echo
1

I often need to change the brightness of my monitor. The symbols of the sensor buttons are not illuminated and on a black background .... invisible!

Now i use osd_cat instead of a torch ;-)

echo 'flush_all' | nc localhost 11211 -i1 <<< 'quit'
while sleep 1; do (mpc status;mpc currentsong)|awk 'BEGIN{FS=": "}/^Artist:/{r=r""$2};/^Title:/{r=r" - "$2};/^time:/{r=$2" "r};/^state: play/{f=1}END{if(f==1){print r}}'|echo -ne "\e[s\e[0;0H\e[K\e[0;44m\e[1;33m$(cat -)\e[0m\e[u";done &
2012-12-24 18:03:29
User: ichbins
Functions: awk echo sleep
Tags: bash mpd,
0

Actually this is a shorter version that fits the 255 chars limit of this resource. The full version shows status in the right top corner:

alias mpdd='while sleep 1; do _r=$(awk '\''BEGIN{FS=": "}/^Artist:/{r=r""$2};/^Title:/{r=r" - "$2};/^time:/{r=$2" "r};/^state: play/{f=1}END{if(f==1){print r}}'\'' <(mpc status;mpc currentsong));_l=${#_r};[ $_l -eq 0 ] && continue;[ -z "$_p" ] && _p=$_l;echo -ne "\e[s\e[0;${_p}H\e[K\e[u";_p=$((COLUMNS - _l));echo -ne "\e[s\e[0;${_p}H\e[K\e[0;44m\e[1;33m${_r}\e[0m\e[u";done &'

mpc is defined like this:

function mpc() {

echo "$*" | nc 192.168.1.1 6600

}

find . -type d | while read dir ; do num=`ls -l $dir | grep '^-' | wc -l` ; echo "$num $dir" ; done | sort -rnk1 | head
scrotit(){ echo "Screenshot in $1 seconds...";scrot -d $1 '%Y%m%d%h.png' -e 'curl -sF file1=@- http://ompldr.org/upload < $f | grep -P -o "(?<=File:).*(http://ompldr.org/.*)\<\/a\>";rm $f'| sed -r 's@.*(http://ompldr.org/\w{1,7}).*@\1@';}
2012-12-03 01:21:19
User: dzup
Functions: echo grep rm sed
0

Take a screenshot, give $1 seconds pause to choose what to screenshot, then upload and get URI of post in ompdlr.org