0

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 folder1.671.G.tar.gz folder2.2.7T.tar.gz 

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

Thanks

1
  • 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. Commented Nov 15, 2019 at 17:40

1 Answer 1

0
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 done 
  • Tar & zip all folders in the current directory
  • Get the size of the tar file
  • Rename the tar file to include the size
3
  • 1
    see why for folder in $(ls... is wrong: mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29 Commented Nov 15, 2019 at 17:36
  • better use find . -maxdepth 1 -mindepth 1 -type d | sed 's#^\./##' | while IFS= read -r folder ; do... and quote all occurrences of ${folder} as "$folder" or "${folder}" or e.g. "${folder}.tar.gz" etc. Commented Nov 15, 2019 at 17:46
  • Why not parse ls? Commented Nov 15, 2019 at 18:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.