Script or command to add all directories and show the total size

3

My question is as follows, I am developing a script where from a text list where you will have the name of the directories, the script pick up the name of each directory, show the size of each directory and then the total. Example:

I need to add the total size of some accounts on the server, so let's say I have the following directories:

/ home / account1 / home / account2 / home / account3 / home / account4 / home / account5 / home / account6

For the sake of a filter, irie create a text file with the name list.txt and in it I will put the name of the directories I want to show the individual size after the sum of all:

cat lista.txt
conta2
conta4

Let's say I want the size of the directories above, I tried to make a for but it did not work:

for i in $(cat lista.txt); do du -hcs /home/$i;done

The result of it came out thus, showing each account and the total below.

684K    /home/conta2
684K    total
732K    /home/conta4
732K    total
1,1M    /home/conta5
1,1M    total

To appear the way I want, you have to run the command like this: du -shc /home/conta1 /home/conta2 /home/conta3

Is there any way to make the script take the name of each directory, add the number of directories, play in a variable each name and then execute like this:

du -shc /home/$1 /home/$2 /home/$3 
    
asked by anonymous 31.07.2015 / 01:07

2 answers

1

Do this:

for i in $(cat lista.txt); do pastas="$i $pastas";done; du -hcs $pastas

Explanation:

In the loop, you create a variable called folders which is the concatenation (with space) of directories contained in list.txt

After the loop is terminated, the du command is run by passing the variable folders as a parameter.

    
31.07.2015 / 01:39
0
du -hcs $(sed 's!^!/home/!' lista.txt)
    
08.09.2015 / 17:03