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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

Commands using echo from sorted by
Terminal - Commands using echo - 1,287 results
for x in {a..d}; do echo -e "n\np\n\n\n\nt\n8e\nw\n" | fdisk /dev/sd"$x"; done
2015-05-21 12:59:48
User: jaimerosario
Functions: echo fdisk
Tags: fdisk for loop
0

So, I'm using a CentOS VM in VirtualBox, and created four new disks in the SCSI controller.

The VM created the folders:

/dev/sda

/dev/sdb

/dev/sdc

/dev/sdd

Using a 'for loop' all disks are partitioned for LVM.

for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do d=$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' /proc/$p/environ 2>/dev/null); [[ -n $d ]] && break; done; echo $d
2015-05-18 20:01:20
User: geyslan
Functions: awk cat echo
Tags: display xorg
1

It's useful when you cannot access your env (systemd) or the process DISPLAY variable is not set. Perhaps also when you have a multi-head/user configuration.

fileinfo() { RPMQF=$(rpm -qf $1); RPMQL=$(rpm -ql $RPMQF);echo "man page:";whatis $(basename $1); echo "Services:"; echo -e "$RPMQL\n"|grep -P "\.service";echo "Config files:";rpm -qc $RPMQF;echo "Provided by:" $RPMQF; }
2015-05-11 16:46:01
User: nnsense
Functions: basename echo grep rpm whatis
2

Many times I give the same commands in loop to find informations about a file. I use this as an alias to summarize that informations in a single command. Now with variables! :D

echo FileName | perl -nlE'sleep 1 while time-(stat)[10]<10' && echo DONE
2015-05-09 14:58:41
User: pung96
Functions: echo perl
0

perl version of "Wait for file to stop changing"

When "FileName" has not been changed for last 10 seconds, then print "DONE"

"10" in "(stat)[10]" means ctime.

One have other options like atime, mtime and others. http://perldoc.perl.org/functions/stat.html

while [ $(( $(date +%s) - $(stat -c %Y FILENAME) )) -lt 10 ]; do sleep 1; done; echo DONE
2015-05-09 12:30:13
User: flatcap
Functions: date echo sleep stat
3

This loop will finish if a file hasn't changed in the last 10 seconds.

.

It checks the file's modification timestamp against the clock.

If 10 seconds have elapsed without any change to the file, then the loop ends.

.

This script will give a false positive if there's a 10 second delay between updates,

e.g. due to network congestion

.

How does it work?

'date +%s' gives the current time in seconds

'stat -c %Y' gives the file's last modification time in seconds

'$(( ))' is bash's way of doing maths

'[ X -lt 10 ]' tests the result is Less Than 10

otherwise sleep for 1 second and repeat

.

Note: Clever as this script is, inotify is smarter.

echo $(date +%m) past $(date +%H) | espeak
2015-05-09 12:24:13
User: hal8
Functions: date echo
0

s/espeak/say/ on a mac

echo -e $_{1..80}'\b+'
while kill -0 0; do timeout 5 bash -c 'spinner=( Ooooo oOooo ooOoo oooOo ooooO oooOo ooOoo oOooo); while true; do for i in ${spinner[@]}; do for _ in seq 0 ${#i}; do echo -en "\b\b"; done; echo -ne "${i}"; sleep 0.2; done; done'; done
2015-05-07 19:13:08
User: anapsix
Functions: bash echo kill seq sleep
1

alternatively, run the spinner for 5 seconds:

timeout 5 bash -c 'spinner=( Ooooo oOooo ooOoo oooOo ooooO oooOo ooOoo oOooo); while true; do for i in ${spinner[@]}; do for j in seq 0 ${#i}; do echo -en "\b\b"; done; echo -ne "${i}"; sleep 0.2; done; done'

echo -e ''$_{1..80}'\b+'
2015-05-05 22:13:33
User: knoppix5
Functions: echo
3

(here is character '+' repeated 80 times)

Sometimes needed to enhance the title of the script.

clear; while sleep 1; do d=$(date +"%H:%M:%S"); e=$(echo "toilet -t -f mono12 $d");tput setaf 1 cup 0; eval $e; tput setaf 4 cup 8; eval "$e -F flop";tput cup 0; done
function summaryIP() { < $1 awk '{print $1}' | while read ip ; do verifyIP ${ip} && echo ${ip}; done | awk '{ip_array[$1]++} END { for (ip in ip_array) printf("%5d\t%s\n", ip_array[ip], ip)}' | sort -rn; }
2015-05-01 16:45:05
User: mpb
Functions: awk echo read sort
1

Working with lists of IP addresses it is sometimes useful to summarize a count of how many times an IP address appears in the file.

This example, summarizeIP, uses another function "verifyIP" previously defined in commandlinefu.com to ensure only valid IP addresses get counted. The summary list is presented in count order starting with highest count.

echo "DISPLAY=$DISPLAY xmessage call the client" | at 10:00
2015-05-01 14:57:15
User: op4
Functions: at echo
Tags: echo at xmessage
1

This command will create a popup reminder window to assist in remembering tasks

http://i.imgur.com/2n7viiA.png is how it looks when created

test "$(md5sum /local/file | cut -d' ' -f1)" == "$(ssh root@xen -- md5sum /remote/file | cut -d' ' -f1)" && echo "Match" || echo "Differ"
2015-04-24 03:09:33
User: gnif
Functions: echo test
1

This method does not transfer the contents of the entire file, instead it computes a md5 sum of the file on each end so that large files can be compared without transferring them across the net.

fold -sw 20 <(echo "Long Text to be wrapped with \"\n\"") |sed ':a;N;$!ba;s/ *\n/\\n/g'
2015-04-16 21:06:53
User: alecthegeek
Functions: echo fold sed
1

I used this fragment with Imagemagick convert so that I can place long text strings in pictures. The "\n" gets converted to a true newline in the image.

So this fragment uses fold command to wrap the line and then sed to convert newlines (and any trailing spaces on the line) to the text "\n"

find /PATHNAME -type l | while read nullsymlink ; do wrongpath=$(readlink "$nullsymlink") ; right=$(echo "$wrongpath" | sed s'|OLD_STRING|NEW_STRING|') ; ln -fs "$right" "$nullsymlink" ; done
2015-04-14 14:58:41
User: iDudo
Functions: echo find ln read readlink sed
0

After you run this script, you can check status for broken symlink with this command:

find -L . -type l

awk '{print $0+0}' <(echo -2; echo +3;)
2015-04-08 09:19:24
Functions: awk echo
0

The leading plus sign is removed - Minus sign is left intact

a=$(b=$(($LINES/2));f() { for c in $(seq $b); do for i in $(seq $c);do echo x;done|xargs echo;done };paste <(f) <(f|tac|tr 'x' '-') <(f|tac|tr 'x' '-') <(f)|tr '\t' ' ');(cat <<<"$a"|tac;cat <<<"$a")|tr '-' ' '
crontest () { date +'%M %k %d %m *' |awk 'BEGIN {ORS="\t"} {print $1+2,$2,$3,$4,$5,$6}'; echo $1;}
2015-03-12 19:56:56
User: CoolHand
Functions: awk date echo
0

usage = crontest "/path/to/bin"

This version of this function will echo back the entire command so it can be copied/pasted to crontab. Should be able to be automagically appended to crontab with a bit more work. Tested on bash and zsh on linux,freebsd,aix

echo 'export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"' >> .bashrc
while ( true ) ; do if [ $(expr $RANDOM % 2 ) -eq 0 ] ; then echo -ne "\xE2\x95\xB1" ; else echo -ne "\xE2\x95\xB2" ; fi ; done
2015-01-17 12:46:37
User: tobi
Functions: echo expr true
1

Generates labyrinth-like pattern on UTF-8 terminal in bash.

For fun ;)

mkfifo fifo; while true ; do echo "Waiting for new event"; nc -l 8080 < fifo | tee -a proxy.txt /dev/stderr | nc 192.168.0.1 80 > fifo ; done
2015-01-14 09:26:54
User: ynedelchev
Functions: echo mkfifo tee true
3

If you have a client that connects to a server via plain text protocol such as HTTP or FTP, with this command you can monitor the messages that the client sends to the server. Application level text stream will be dumped on the command line as well as saved in a file called proxy.txt.

You have to change 8080 to the local port where you want your client to connect to. Change also 192.168.0.1 to the IP address of the destination server and 80 to the port of the destination server.

Then simply point your client to localhost 8080 (or whatever you changed it to).

The traffic will be redirected to host 192.168.0.1 on port 80 (or whatever you changed them to).

Any requests from the client to the server will be dumped on the console as well as in the file "proxy.txt".

Unfortunately the responses from the server will not be dumped.

find . -type f -exec echo -n "touch -t \`echo " \; -exec echo -n {} \; -exec echo -n " | sed -E 's/.*([[:digit:]]{8})_([[:digit:]]{4})([[:digit:]]{2}).*/\1\2.\3/g'\` " \; -exec echo {} \; | sh
clear;while true;sleep 1;do for((a=1;a<=$(tput cols)/3;a++));do tput cup 0 $a;echo " " $(date);done;sleep 1;for((a;a>=1;a--));do tput cup 0 $a;echo $(date) " ";done;done
$ if check4progs cp foo mv bar rsync; then echo "needed progs avail, lets do funky stuff"; else echo "oh oh better abort now"; fi
2015-01-01 16:16:00
User: Xk2c
Functions: cp echo mv
-6

Thanks to the great grml team for this func!

You really should look at their shell configs for further usefull things!

http://git.grml.org/?p=grml-etc-core.git;a=blob_plain;f=etc/grml/script-functions;h=4d6bcea8f9beae83abd08f44155d299ea54a4a9f;hb=HEAD

# {{{ check for availability of program(s)

# usage example:

# check4progs [-s,-q,--quiet,--silent] arg [arg .... argn]

#

# with option given either of:

# -s,-q,--quiet,--silent

#

# check for available progs but produce no output

check4progs() {

[ -n "${ZSH_VERSION}" ] && emulate -L sh

local RTN=0

local oldifs="${IFS}"

local ARG d found

local VERBOSE=1

case ${1} in

-q | -s | --quiet | --silent)

VERBOSE=0

shift 1

;;

*)

;;

esac

while [ $# -gt 0 ]

do

ARG="$1"

shift

found=0

IFS=:

for d in $PATH

do

if [ -x "${d}/${ARG}" ]

then

found=1

break

fi

done

IFS="${oldifs}"

# check for availability

if [ ${found} -eq 0 ]

then

if [ ${VERBOSE} -eq 1 ]

then

printf "%s: binary not found\n" "${ARG}" >&2

fi

RTN=1

fi

done

# return non zero, if at least one prog is missing!

return $RTN

}

# }}}

echo {1..9} '* 0 #' | tr ' ' '\n' |paste - - -