Script or command to sum all directories and show full size

Asked

Viewed 941 times

5

I am developing a script where from a text list where will have the name of directories, the script take 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, let’s say I have the following directories:

/home/conta1
/home/conta2
/home/conta3
/home/conta4
/home/conta5
/home/conta6

Because of a filter, I would create a text file with the name lista.txt and in it I will put the name of the directories I wish to show the individual size after the sum of all:

cat lista.txt
conta2
conta4

Let’s say I wish the size of the directories above, I tried to make a for, but it didn’t work out:

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

The result comes out like this, showing each account and the total below.

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

In order to appear the way I wish, 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 up the number of directories, play in a variable each name and then run like this:

du -shc /home/$1 /home/$2 /home/$3 

2 answers

2

du -hcs $(sed 's!^!/home/!' lista.txt)

2

Do so:

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

Explanation:

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

After finishing the loop, the command is executed du, passing as parameter the variable folders.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.