Photon Light Photon Light - 4 months ago 8
Linux Question

Using du to estimate amount of space for files in list file

I've got a directory structure which looks like this:

/here/are/my/files/


My files are named by some convention, let's say:

myfile_0001.txt
myfile_0001.bin
myfile_0001.xls
...(few other extensions)
myfile_0567.txt
myfile_0567.bin
myfile_0567.xls
... (few other extensions)


And so on. Extensions may change, or there may be too many of them for a label
myfile_xxxx
to care about it.

On the other hand, I've got file
list.txt
with entries:

myfile_0001
myfile_1234
myfile_0204
... (label number is irregular).


What I want to do is to estimate amount of space of all file families (that is, myfile_xxxx* - all extensions for a given file label), that are included in
list.txt
(and only for them).

How i was doing it so far:

file_list=$(for entry in $( cat list.txt); do ls /here/are/my/files/$entry* ; done)
du -shc $file_list


(It could be combined in one line without
$file_list
variable, but it looks more readable for me).

Can I do it in one line with only
du
command, with something like

du -c "/here/are/my/files/$(cat list.txt)* | tail -1


(It dosen't work, but I would like to get something in similar form)

Answer

Try

du -hs -c $(for line in `cat list.txt`; do ls $line; done) | tail -1 | awk '{print $1}'

This will print only the filesize in human readable format

EDIT: If you want to provide a static path that is not privided in the list.txt you can do

du -hs -c $(for line in `cat list.txt`; do ls /path/to/files/${line}; done) | tail -1 | awk '{print $1}'

If you want all files with the sufix in the list.txt do

du -hs -c $(for line in `cat list.txt`; do ls /path/to/files/${line}*; done) | tail -1 | awk '{print $1}'

If you dont want human readable format remove "-hs" from the command

Comments