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.

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.

UpGuard checks and validates configurations for every major OS, network device, and cloud provider.

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:



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!

Top Tags



Commands tagged for loop from sorted by
Terminal - Commands tagged for loop - 26 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

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

The VM created the folders:





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

for USER in /var/spool/cron/*; do echo "--- crontab for $USER ---"; cat "$USER"; done
2014-12-11 19:48:46
User: tyzbit
Functions: cat echo

This is not exhaustive but after checking /etc/cron* is a good way to see if there are any other jobs any users may have set.

Note: this is a repost from a comment "flatcap" made on http://www.commandlinefu.com/commands/view/3726/print-crontab-entries-for-all-the-users-that-actually-have-a-crontab#comment, for which I am grateful and I take no credit.

function b58encode () { local b58_lookup_table=({1..9} {A..H} {J..N} {P..Z} {a..k} {m..z}); bc<<<"obase=58;ibase=16;${1^^}"|(read -a s; for b58_index in "${s[@]}" ; do printf %s ${b58_lookup_table[ 10#"$b58_index" ]}; done); }

A bitcoin "brainwallet" is a secret passphrase you carry in your brain.

The Bitcoin Brainwallet Private Key Base58 Encoder is the third of three functions needed to calculate a bitcoin PRIVATE key from your "brainwallet" passphrase.

This base58 encoder uses the obase parameter of the amazing bc utility to convert from ASCII-hex to base58. Tech note: bc inserts line continuation backslashes, but the "read s" command automatically strips them out.

I hope that one day base58 will, like base64, be added to the amazing openssl utility.

YYYY=2014; MM=02; for d in $(cal -h $MM $YYYY | grep "^ *[0-9]"); do DD=$(printf "%02d" $d); echo $YYYY$MM$DD; done
2014-02-06 11:31:57
User: fibo
Functions: cal echo grep printf
Tags: cal for loop

Edit YYYY and MM at the beginning of the command with the year and month you want.

Note that `DD=$(printf "%02d" $d)` will pad single digit integers with a leading zero.

Substitute `echo $YYYY$MM$DD` at the end of the line with the command you want to launch, for instance

script.pl --yyyymmdd $YYYY$MM$DD

Also available on GitHub as bash util: https://github.com/fibo/yyyymmdd

for i in rpcbind nfslock lldpad fcoe rpcidmapd; do service $i stop; chkconfig $i off; done
2014-01-23 05:05:22
User: prasad
Functions: chkconfig
Tags: for loop

We will turn off and stop multiple linux services using FOR LOOP.

FOR %I IN (*.mp4) DO \Tools\ffmpeg\bin\ffmpeg.exe -i "%I" "%~nI.mpeg"
for i in {1..4096}; do base64 /dev/urandom | head -c 8192 > dummy$i.rnd ; done

Using the 'time' command, running this with 'tr' took 28 seconds (and change) each time but using base64 only took 8 seconds (and change). If the file doesn't have to be viewable, pulling straight from urandom with head only took 6 seconds (and change)

for i in `seq 1 4096`; do tr -dc A-Za-z0-9 </dev/urandom | head -c8192 > dummy$i.rnd; done
for i in `ip addr show dev eth1 | grep inet | awk '{print $2}' | cut -d/ -f1`; do echo -n $i; echo -en '\t'; host $i | awk '{print $5}'; done
while curl -dsL example.com 2>&1 | grep 503;do sleep 8;done;echo server up
while true; do curl -vsL -o /dev/null example.com 2>&1 | grep 503 > /dev/null || echo "OK: server is up."; sleep 8; done
FOR %%c in (C:\Windows\*.*) DO (echo file %%c)
2013-01-31 15:19:54
User: jmcclosk
Functions: echo file

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. :-)

for ARG in * ; do sudo -u USER 7z x -o"$(echo $ARG|sed 's/\(.*\)\..*/\1/')" "$ARG" ; done
2012-12-31 19:47:24
User: n158
Functions: sudo

Magic line will extract almost all possible archives from current folder in its own folders. Don't forget to change USER name in sudo command. sed is used to create names for folders from archive names w/o extension. You can test sed expression, used in this command:

arg='war.lan.net' ; x=$(echo $arg|sed 's/\(.*\)\..*/\1/') ; echo $x

If some archives can't be extracted, install packages:

apt-get install p7zip-full p7zip-rar

Hope this will save a lot of your time. Enjoy.

i=0; while [ $i -lt 100 ]; do echo "test, ttest, tttest-${i}" >> kk.file; i=`expr $i + 1`; done
2012-09-13 21:46:18
User: kaushalmehra
Functions: echo

while commandt






{commandt is executed and its exit status tested.}

for i in 1 2 3

> do

> echo $i

> done

for i in /var/spool/cron/tabs/*; do echo ${i##*/}; sed 's/^/\t/' $i; echo; done
2012-07-12 08:07:20
User: harpo
Functions: echo sed

This is flatcaps tweaked command to make it work on SLES 11.2

for i in /var/spool/cron/*; do echo ${i##*/}; sed 's/^/\t/' $i; echo; done
testt(){ o=abcdefghLkprsStuwxOGN;echo $@;for((i=0;i<${#o};i++));do c=${o:$i:1};test -$c $1 && help test | sed "/^ *-$c/!d;1q;s/^[^T]*/-$c /;s/ if/ -/";done; }
2012-02-21 16:54:53
User: AskApache
Functions: echo sed test

Applies each file operator using the built-in test.

testt /home/askapache/.sq


-a True - file exists.

-d True - file is a directory.

-e True - file exists.

-r True - file is readable by you.

-s True - file exists and is not empty.

-w True - the file is writable by you.

-x True - the file is executable by you.

-O True - the file is effectively owned by you.

-G True - the file is effectively owned by your group.

-N True - the file has been modified since it was last read.

Full Function:

testt ()


local dp;

until [ -z "${1:-}" ]; do


[[ ! -a "$1" ]] && dp="$PWD/$dp";

command ls -w $((${COLUMNS:-80}-20)) -lA --color=tty -d "$dp";

[[ -d "$dp" ]] && find "$dp" -mount -depth -wholename "$dp" -printf '%.5m %10M %#15s %#9u %-9g %#5U %-5G %Am/%Ad/%AY %Cm/%Cd/%CY %Tm/%Td/%TY [%Y] %p\n' -a -quit 2> /dev/null;

for f in a b c d e f g h L k p r s S t u w x O G N;


test -$f "$dp" && help test | sed "/-$f F/!d" | sed -e 's#^[\t ]*-\([a-zA-Z]\{1\}\) F[A-Z]*[\t ]* True if#-\1 "'$dp'" #g';





sayspeed() { for i in $(seq 1 `echo "$1"|wc -c`); do echo -n "`echo $1 |cut -c ${i}`"; sleep 0.1s; done; echo "";}
2012-02-11 05:51:42
User: kundan
Functions: echo seq sleep wc

change the time that you would like to have as print interval

and just use it to say whatever you want to

genRandomText() { x=({a..z}); for(( i=0; i<$1; i++ )); do printf ${x[$((RANDOM%26))]}; done; printf "\n"; }
2012-01-26 08:19:33
User: uxseven
Functions: printf

Here's my version. It's a bit lengthy but I prefer it since it's all Bash.

genRandomText() { perl -e '$n=shift; print chr(int(rand(26)) + 97) for 1..$n; print "\n"' $1;}
2012-01-21 00:21:20
User: putnamhill
Functions: perl

If you don't have seq, you can use perl.

genRandomText() { a=( a b c d e f g h i j k l m n o p q r s t u v w x y z );f=0;for i in $(seq 1 $(($1-1))); do r=$(($RANDOM%26)); if [ "$f" -eq 1 -a $(($r%$i)) -eq 0 ]; then echo -n " ";f=0;continue; else f=1;fi;echo -n ${a[$r]};done;echo"";}
2012-01-20 21:18:16
User: bbbco
Functions: echo seq

Ever need to get some text that is a specific number of characters long? Use this function to easily generate it! Doesn't look pretty, but sure does work for testing purposes!

domain=google.com; for ns in $(whois $domain | awk -F: '/Name Server/{print $2}'); do echo ">>> Nameservers for $domain from $a <<<"; dig @$ns $domain ns +short; echo; done;
2011-05-08 04:46:34
User: laebshade
Functions: awk dig echo whois

Change the $domain variable to whichever domain you wish to query.

Works with the majority of whois info; for some that won't, you may have to compromise:

domain=google.com; for a in $(whois $domain | grep "Domain servers in listed order:" --after 3 | grep -v "Domain servers in listed order:"); do echo ">>> Nameservers for $domain from $a

Note that this doesn't work as well as the first one; if they have more than 3 nameservers, it won't hit them all.

As the summary states, this can be useful for making sure the whois nameservers for a domain match the nameserver records (NS records) from the nameservers themselves.

for i in {21..79};do echo -e "\x$i";done | tr " " "\n" | shuf | tr -d "\n"
for _a in {A..Z} {a..z};do _z=\${!${_a}*};for _i in `eval echo "${_z}"`;do echo -e "$_i: ${!_i}";done;done|cat -Tsv

This uses some tricks I found while reading the bash man page to enumerate and display all the current environment variables, including those not listed by the 'env' command which according to the bash docs are more for internal use by BASH. The main trick is the way bash will list all environment variable names when performing expansion on ${!A*}. Then the eval builtin makes it work in a loop.

I created a function for this and use it instead of env. (by aliasing env).

This is the function that given any parameters lists the variables that start with it. So 'aae B' would list all env variables starting wit B. And 'aae {A..Z} {a..z}' would list all variables starting with any letter of the alphabet. And 'aae TERM' would list all variables starting with TERM.

aae(){ local __a __i __z;for __a in "$@";do __z=\${!${__a}*};for __i in `eval echo "${__z}"`;do echo -e "$__i: ${!__i}";done;done; }

And my printenv replacement is:

alias env='aae {A..Z} {a..z} "_"|sort|cat -v 2>&1 | sed "s/\\^\\[/\\\\033/g"'

From: http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

imageshack() { for files in *; do curl -H Expect: -F fileupload="@$files" -F xml=yes -# "http://www.imageshack.us/index.php" | grep image_link | sed -e 's/<image_link>/[IMG]/g' -e 's/<\/image_link>/[\/IMG]/g'; done; }
2010-10-01 06:50:04
Functions: grep sed
Tags: curl for loop

Each file in the current folder is uploaded to imageshack.us

If the folder contains other filetypes


for files in *


for files in *.jpg

(to upload ONLY .jpg files)

Additionally you can try (results may vary):

for files in *.jpg *.png

The output URL is encased with BB image tags for use in a forum.