Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

loop in shell script

Hello  Expert,

I want to configure loop in shell script...
#!/bin/bash
du -h --max-depth=1 /root/Desktop/inux/test1 | sort -n -r > /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/test2 | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/test3 | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/test4 | sort -n -r >> /root/Desktop/userlist.csv

I've four folder in under linux..Now I'm trying to do run du command in one line..which take all folder disk usage size under linux directory....

if anybody know  about it....please reply on the SAME...
0
xeroxzerox
Asked:
xeroxzerox
  • 7
  • 5
  • 3
  • +1
3 Solutions
 
ozoCommented:
du -h --max-depth=1 /root/Desktop/inux/test[1-4] | sort -n -r > /root/Desktop/userlist.csv
0
 
xeroxzeroxAuthor Commented:
Thanks for reply ozo,
But if i've like this what can i do.... (user name will be change)
du -h --max-depth=1 /root/Desktop/inux/xerox1 | sort -n -r > /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/zerox | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/neil | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/prey.ody | sort -n -r >> /root/Desktop/userlist.csv
0
 
ozoCommented:
du -h --max-depth=1 /root/Desktop/inux/{xerox1,zerox,neil,prey.ody} | sort -n -r > /root/Desktop/userlist.csv
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
TintinCommented:
Or if you do want a for loop

for i in xerox1 zerox neil prey.ody
do
   du -h --max-depth=1 /root/Desktop/$i | sort -nr >>/root/Desktop/userlist.csv
done

Open in new window

0
 
xeroxzeroxAuthor Commented:
OZO  your command output look like this
8.0K    /root/Desktop/inux/prey.ody
8.0K    /root/Desktop/inux/xerox/
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/prey.ody/user1
4.0K    /root/Desktop/inux/xerox/user2
4.0K    /root/Desktop/inux/zerox/user3

But when I run that command
du -h --max-depth=1 /root/Desktop/inux/xerox1 | sort -n -r > /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/zerox | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/neil | sort -n -r >> /root/Desktop/userlist.csv
du -h --max-depth=1 /root/Desktop/inux/prey.ody | sort -n -r >> /root/Desktop/userlist.csv

Output is like this:
8.0K    /root/Desktop/inux/prey.ody
4.0K    /root/Desktop/inux/prey.ody/user1
8.0K    /root/Desktop/inux/xerox/
4.0K    /root/Desktop/inux/xerox/user2
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/zerox/user3

can we get output look like this by your command....
0
 
ozoCommented:
You can modify the sort options, but it's probably easier to run Tintin's version
0
 
xeroxzeroxAuthor Commented:
I've ran Tintin loop but it show  only one Directory output..like.this...

8.0K    /root/Desktop/inux/prey.ody
4.0K    /root/Desktop/inux/prey.ody/user1
0
 
ozoCommented:
Did you really run Tintin's loop?  or did you change the >> in  Tintin's loop to >
0
 
xeroxzeroxAuthor Commented:
#!/bin/bash
for i in xerox1 zerox neil prey.ody
do
   du -h --max-depth=1 /root/Desktop/$i | sort -nr >> /root/Desktop/userlist.csv
done
mutt -s "Test email with attachments and content" -a /root/Desktop/userlist.csv  -- xerox.zerox@texas.edu < /root/Desktop/signature.txt

when i run it 2 times...it add output data userlist.csv in 2 times.
when again i've run it then output data in 3 times...It's not over-right data in userlist.csv file...Because we are using >> here. if i use > then it show only last directory details not all...
0
 
ozoCommented:
do the > before the loop
0
 
xeroxzeroxAuthor Commented:
i'm not understand where i shall be put > in it.

i think according to you it look like

du -h --max-depth=1 /root/Desktop/$i | sort -nr > /root/Desktop/userlist.csv
0
 
simon3270Commented:
You need to do the ">" before the loop (to empty out the output file), and ">>" within the loop so that results are appended to the file and don't overwrite it.  You end up with:
#!/bin/bash
> /root/Desktop/userlist.csv
for i in xerox1 zerox neil prey.ody
do
   du -h --max-depth=1 /root/Desktop/$i | sort -nr >> /root/Desktop/userlist.csv
done
mutt -s "Test email with attachments and content" -a /root/Desktop/userlist.csv  -- xerox.zerox@texas.edu < /root/Desktop/signature.txt

Open in new window

0
 
xeroxzeroxAuthor Commented:
Hi,

I've ran simon 3270 command Now output like this:

4.0K    /root/Desktop/inux/prey.ody/user1
8.0K    /root/Desktop/inux/prey.ody
8.0K    /root/Desktop/inux/xerox/
4.0K    /root/Desktop/inux/xerox/user2
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/zerox/user3

Now prey.ody directory in under user1....why it's not showing output like this..

8.0K    /root/Desktop/inux/prey.ody
4.0K    /root/Desktop/inux/prey.ody/user1
8.0K    /root/Desktop/inux/xerox/
4.0K    /root/Desktop/inux/xerox/user2
8.0K    /root/Desktop/inux/zerox/
4.0K    /root/Desktop/inux/zerox/user3

So...issue in sort or another one in script...
0
 
xeroxzeroxAuthor Commented:
thanks all of you
0
 
simon3270Commented:
Not sure why your entries appear in the "wrong" order, but I think you'll have to drop the "-h" parameter on du.  If, say, the prey.ody directory has 1.1G, while prey.ody/user1 has 900M, the "sort -nr" will say that prey.ody/user1 is bigger - it doesn't understand the M or G suffixes.  For example, on my machine:
sar@localhost:~$ du -h --max-depth=1 /home/sar/Desktop | sort -nr
23M	/home/sar/Desktop
4.0K	/home/sar/Desktop/2011_Oct
1.7M	/home/sar/Desktop/iTunes Library.itlp
sar@localhost:~$ du --max-depth=1 /home/sar/Desktop | sort -nr
22884	/home/sar/Desktop
1724	/home/sar/Desktop/iTunes Library.itlp
4	/home/sar/Desktop/2011_Oct
sar@localhost:~$ 

Open in new window

So the 4.0K in the 2011_Oct directory is shown as being bigger than the 1.7M of iTunes library with "-h", but without, they are in the right order.
0
 
simon3270Commented:
Alternatively, use the "-h" (sort in "human numeric") parameter of sort rather than "-n", so that it *does* respond to K/M/G suffixes.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now