How to know the size of the file from an absolute path. For example, I have created a file test. Inside that file, I have specified the absolute paths of some other files. Now from the given file how to find the size of the absolute path?

du -sh filename gives --> file size
du -sh $filename gives --> current working directory size.
  • $filename is the content of the variable filename, not the absolute path to filename. – muru Jul 16 '19 at 7:33

Try this,

We should pass the content of the test file as an argument to find the size of absolute path mentioned in it.

du -sh `cat test`
|improve this answer|||||
  • works only when the filenames do not contain spaces. – pLumo Jul 16 '19 at 7:34

You can use --files0-from option from du:

summarize disk usage of the NUL-terminated file names specified in file > F; if F is -, then read names from standard input

As I guess, that your files are not NUL-terminated, but newline-terminated, you need to replace \n with \0 first. This will of course break for filenames containing \n.

tr '\n' '\0' < filenames | du -sh --files0-from=-

Alternatively, you could simply use xargs:

xargs -a filenames -d '\n' du -sh
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.