Tested on Bash shell.

Sometimes we might require some looping process in our daily system admin job. Example below is just one of many usage that you can do with the for loop on shell prompt.

Lets say that we are facing some storage problem due to filesystem full, and we started looking high and low for the folders that taking up all the bytes. This is where the for loop comes handy.

admin# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda6 1510032 207340 1225984 15% /
/dev/hda1 46636 9267 34961 21% /boot
/dev/hda8 21268020 8504668 11682984 43% /data
/dev/hda5 3020140 2181588 685136 77% /home
/dev/hda3 4538156 843072 3464552 20% /usr
/dev/hda2 7052496 6411360 641136 100% /home2

Above example shows that /home2 are 100% full, so obviously we just need to search the /home2 filesystem. We can start by changing directory to /home2

admin# cd /home2

and then check what are the folders inside /home2

admin# ls -al
total 66772
drwx-----x 14 rosley users 4096 Feb 21 14:37 ./
drwxr-xr-x 82 root root 4096 Feb 9 07:51 ../
drw-r--r-- 1 user1 users 0 Jun 28 2001 user1/
drw------- 1 user2 users 2285 Jun 28 2001 user2/
drw------- 1 user3 users 2664 Feb 14 15:07 user3/

We do know that command du is to calculate the storage used by certain folder, and by supplying the option -k it will return the value in KiloBytes (instead of blocks), and to make sure we got the real total usage of the folder including its sub-folders, we need to use the -s option. so the complete command to get the total usage of the folder is

admin# du -ks user1
12094 user1

The output will be the amount in KiloBytes, in this case user1 folder used up 12,094Kb or 12.094Mb (MegaBytes).

We can run the du -ks command to every each of the folders until we found the source of the problem or we can use the easier way…

admin# ls -1

By using ls -1 we can actually list just the name of the files/folders, and this is actually provides us with a good listing of just the names instead of all the other non-relavent information.

admin# env | grep SHELL
admin# for i in `ls -1`
> do
> du -ks $i
> done
12094 user1
233004 user2
6166262 user3

Be aware that the single quote for ls -1 is not the normal single quote that normally appear on the same keyboard button with the double quote (), This is the other single quote that (normally) sharing with keyboard button tilde (~) which is ` and not . The normal single quote means you want to treat it as normal text/string/character, this single quote that we use instruct the system to execute whatever command we put inside it. So in this case to execute ls -1. The for loop uses the variable i to hold the output of the command ls -1, which is the lists of names in /home2.

So there will be 3 loops as the output of ls -1 is 3 names (user1,user2,user3), and on every name it will execute the command du -ks $i where $i will be replaced by user1 and then user2 and then user3 (or until end of the names). As a result, we have found the folder that used up our storage.

You can repeat the same process when you want to search further in user3 folders later on.

Tags: , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>