I have a list of directories I want to compress on an ubuntu machine.

Is there any tool to automatically calculate the size of the of my folders and copy the size to the name of the compressed file?

I have some thing like that:

$du -h -d 1
671G    ./folder1
2.7T    ./folder2

I would like it to be like this

$ls -1 

The given sizes with the du -h -d 1 are the non-compressed one, so the size calculation needs to happen after the compression.


  • Why do you want the compressed size in the file name? You can easily see the compressed size of the archive with du -h at any time. The uncompressed size (which would result from unpacking the archive) is IMHO a much more useful information.
    – Bodo
    Nov 15, 2019 at 17:40

1 Answer 1

for folder in $(ls -l | grep ^d | awk '{print $NF}'); do
  tar zcf ${folder}.tar.gz ${folder}
  size=$(du -sh ${folder}.tar.gz | awk '{print $1}')
  mv ${folder}.tar.gz ${folder}-${size}.tar.gz
  • Tar & zip all folders in the current directory
  • Get the size of the tar file
  • Rename the tar file to include the size

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.