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
tput smcup; echo "Doing some things..."; sleep 2; tput rmcup
2009-10-08 16:48:04
User: jgc
Functions: echo sleep tput
8

Very useful for interactive scripts where you would like to return the terminal contents to its original state before the script was run. This would be similar to how vi exits and returns you to your original terminal screen.

Save and clear the terminal contents with:

tput smcup

Execute some commands, then restore the saved terminal contents with:

tput rmcup
for USER in `cut -d ":" -f1 </etc/passwd`; do crontab -u ${USER} -l 1>/dev/null 2>&1; if [ ! ${?} -ne 0 ]; then echo -en "--- crontab for ${USER} ---\n$(crontab -u ${USER} -l)\n"; fi; done
2009-10-07 20:51:01
User: tharant
Functions: crontab echo
3

This is how I list the crontab for all the users on a given system that actually have a crontab.

You could wrap it with a function block and place it in your .profile or .bashrc for quick access.

There's prolly a simpler way to do this. Discuss.

echo "vertical text" | fold -1
sitepass() { echo -n "$@" | md5sum | sha1sum | sha224sum | sha256sum | sha384sum | sha512sum | gzip - | strings -n 1 | tr -d "[:space:]" | tr -s '[:print:]' | tr '!-~' 'P-~!-O' | rev | cut -b 2-11; history -d $(($HISTCMD-1)); }
2009-10-01 20:14:57
User: syssyphus
Tags: Security
14

usage: sitepass MaStErPaSsWoRd example.com

description: An admittedly excessive amount of hashing, but this will give you a pretty secure password, It also eliminates repeated characters and deletes itself from your command history.

tr '!-~' 'P-~!-O' # this bit is rot47, kinda like rot13 but more nerdy

rev # this avoids the first few bytes of gzip payload, and the magic bytes.

echo $(</tmp/foo)
expdp user/password FLASHBACK_SCN=$(echo -e "select current_scn from v\$database;" | sqlplus / as sysdba 2>/dev/null| grep [0-9][0-9][0-9][0-9][0-9][0-9]*)
2009-10-01 08:55:20
User: peshay
Functions: as echo grep
0

Creates a consistent datapumpt export on an Oracle database with the current sequence number, while the system is running and changes happens on the database.

check_dns_no() { for i in $* ; do if `wget -O - -q http://www.norid.no/domenenavnbaser/whois/?query=$i.no | grep "no match" &>/dev/null` ; then echo $i.no "available" ; fi ; sleep 1 ;done }
2009-09-30 21:17:33
User: xeor
Functions: echo grep sleep
Tags: wget dig dns
0

Mostly for Norwegians, but easily adoptable to others. Very handy if you are brainstorming for a new domainname.

Will only display the available ones..

You can usually do this better with dig, but if you dont have dig, or the TLD only have an online service to check with, this will be usefull..

(crontab -l; echo '* * * * * dmesg -c'; ) | crontab -
2009-09-30 18:13:38
User: syssyphus
Functions: crontab dmesg echo
5

this is helpful because dmesg is where i/o errors, etc are logged to... you will also be able to see when the system reboots or someone attaches a thumb drive, etc.

don't forget to set yourself up in /etc/aliases to get roots email.

echo "Starting Daemon"; ( while :; do sleep 15; echo "I am still running =]"; done ) & disown -h -ar $!
s=124890; array=($(echo $s | sed 's/./& /g')); echo ${array[@]}; echo ${!array[@]}
2009-09-29 12:30:04
User: tatsu
Functions: echo sed
0

Convert string to array using echo and sed =)

[ `date --date='next day' +'%B'` == `date +'%B'` ] || echo 'end of month'
PERMA () { echo "$@" >> ~/.bashrc; }
2009-09-28 16:03:24
User: zlemini
Functions: echo
2

Simple function to permanently add an alias to your profile.

Tested on bash and Ksh, bash version above.

Here is the ksh version: PERMA () { print "$@" >> ~/.profile; }

Sample usage:

PERMA alias la='ls -a'

echo -e "\e[32m"; while :; do for i in {1..16}; do r="$(($RANDOM % 2))"; if [[ $(($RANDOM % 5)) == 1 ]]; then if [[ $(($RANDOM % 4)) == 1 ]]; then v+="\e[1m $r "; else v+="\e[2m $r "; fi; else v+=" "; fi; done; echo -e "$v"; v=""; done
2009-09-27 15:30:38
User: dennisw
Functions: echo
Tags: color
19

I like the fact the Patola's version uses only ones and zeros, but I also like the sparse output of the other versions. This one combines both of those features and eliminates some unnecessary cruft.

You can vary the sparseness by changing "$(($RANDOM % 5))" to another number. The number in this term "$(($RANDOM % 4))" controls how frequently the numbers are output bold.

echo "blacklist pcspkr"|sudo tee -a /etc/modprobe.d/blacklist.conf
2009-09-27 11:42:47
User: sliceoflinux
Functions: echo sudo tee
4

This command will disable the beep sound from the PC speaker.

beepwhenup () { echo 'Enter host you want to ping:'; read PHOST; if [[ "$PHOST" == "" ]]; then exit; fi; while true; do ping -c1 -W2 $PHOST 2>&1 >/dev/null; if [[ "$?" == "0" ]]; then for j in $(seq 1 4); do beep; done; ping -c1 $PHOST; break; fi; done; }
2009-09-24 18:11:10
Functions: echo host ping read seq
Tags: ping beep
2

After this, just type:

beepwhenup

You need to install "beep" before this would make the beep sound.

Save it in your .profile if you want to use it later

WARNING: this command won't exit until it is successful. You won't be able to CONTROL+C out of it.

echo $PATH|tr : '\n'|sort|uniq -d
host A: cat /proc/dev/ttyS0 host B: echo hello > /dev/ttyS0
2009-09-24 13:22:23
User: flart
Functions: cat echo host
2

If the connection works you should see a "hello" on host A. If not: check your cabeling etc :-)

mplayer -vo dummy -ao dummy -identify * 2>&1 | grep ID_LENGTH | sed 's/.*=\([0-9]*\)/\1/' | xargs echo | sed 's/ /+/g' | bc | awk 'S=$1; {printf "%dh:%dm:%ds\n",S/(60*60),S%(60*60)/60,S%60}'
2009-09-24 10:33:19
User: Strawp
Functions: awk bc echo grep sed xargs
5

You're behind on your TV catch-up, but how far behind? This command tries to open mplayer against all files in the current dir. If it's a video file it will contain ID_LENGTH, which is summed and output in hours, minutes and seconds.

Someone better at awk could probably reduce this down a lot.

(echo "set terminal png;plot '-' u 1:2 t 'cpu' w linespoints;"; sudo vmstat 2 10 | awk 'NR > 2 {print NR, $13}') | gnuplot > plot.png
for each in `cut -d " " -f 1 inputfile.txt`; do echo "select * from table where id = \"$each\";"; done
2009-09-23 13:29:16
User: hfs
Functions: echo
Tags: echo cut for-each
0

I never can remember the syntax of awk. You can give a different -d option to cut to separate by e.g. commas. Also this allows to do more things with the generated SQL, e.g. to redirect it into different files.

echo 'Enter Picasa album RSS URL:"; read -e feedurl; GET "$feedurl" |sed 's/</\n</g' | grep media:content |sed 's/.*url='"'"'\([^'"'"']*\)'"'"'.*$/\1/' > wgetlist
2009-09-22 10:51:08
User: kamathln
Functions: echo
0

Grab the RSS link to the Picasa album. Feed it to the script when its hungry. When its done writing the shopping list, just use

wget -c -i wgetlist

to get your stuff.

echo "see attached file" | mail -a filename -s "subject" email@address
2009-09-21 11:58:49
User: gnpf
Functions: echo mail
Tags: mail
1

if "mail -a" fail, try "mutt -a" or "nail -a"

sleeper(){ while `ps -p $1 &>/dev/null`; do echo -n "${2:-.}"; sleep ${3:-1}; done; }; export -f sleeper
12

Very useful in shell scripts because you can run a task nicely in the background using job-control and output progress until it completes.

Here's an example of how I use it in backup scripts to run gpg in the background to encrypt an archive file (which I create in this same way). $! is the process ID of the last run command, which is saved here as the variable PI, then sleeper is called with the process id of the gpg task (PI), and sleeper is also specified to output : instead of the default . every 3 seconds instead of the default 1. So a shorter version would be sleeper $!;

The wait is also used here, though it may not be needed on your system.

echo ">>> ENCRYPTING SQL BACKUP" gpg --output archive.tgz.asc --encrypt archive.tgz 1>/dev/null & PI=$!; sleeper $PI ":" 3; wait $PI && rm archive.tgz &>/dev/null

Previously to get around the $! not always being available, I would instead check for the existance of the process ID by checking if the directory /proc/$PID existed, but not everyone uses proc anymore. That version is currently the one at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html but I plan on upgrading to this new version soon.

xmms2 mlib search added \> $(echo $(date +%s) - 604800|bc)
echo $(date +%s) > start-time; URL=http://www.google.com; while true; do echo $(curl -L --w %{speed_download} -o/dev/null -s $URL) >> bps; sleep 10; done &
2009-09-19 21:26:06
User: matthewbauer
Functions: date echo sleep
9

This will log your internet download speed.

You can run

gnuplot -persist <(echo "plot 'bps' with lines")

to get a graph of it.