Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a problem. I need to compress a root folder, but it has a lot of ZIP/BZIP/TAR files in its subfolders. Can someone show me a script that will search and unzip each subfolder's zip file and then 7-zip the root folder with maximum compression?

share|improve this question

3 Answers 3

It should be noted that 7z does not respect permissions properly in that it doesn't store the owner/group of the file, etc. It isn't ideally suitable for backups on Unix, so I have used tar.7z in the script below, which will still give good compression. If you really want to you could just specify .7z, but you will lose the permissions data.

You will need to install atool from the repositories because I use it to simplify extraction of various formats. You will also need p7zip:

sudo apt-get install atool p7zip

Simply enter the following script in your text editor, save it and make it executable. Then simply execute it in the desired folder (but see important notes below to see how it works).

(The find command-line in this script is used from this question here.)

#!/bin/bash

find -mindepth 2 -type f -print -exec mv {} . \;
mkdir my_files
aunpack -e * -X my_files/
apack -O-mx=9 imp_files.tar.7z my_files/
mv imp_files.tar.7z /home/$USER/
exit 0

Important Notes:

  • The files from the desired subfolders are delivered to the directory in which the script is executed, unless you want to add the line cd /home/$USER/"$1" to the top of the script and place it in your ~/bin and then call it with script_name desired_folder.

  • The files are found and then moved to the pwd (present working directory), and then a new directory is created and they are all extracted to that. I have used aunpack and apack from the atools package as they enable multiple archive types to be handled at once. See my answer here for more information; aunpack is great as it allows you to extract all types of archive files, without needing to execute separately tar, 7z, etc.

  • After the files have been extracted to the folder specified, that folder is packed as a tar then a 7zip file with maximal compression (mx=9). Lastly, that resulting file is moved to the base of your home folder.

In the script, I have not deleted the files that are fetched from the subdirectories; you can add that if you wish, and tailor the script to your needs, but I hope this is useful for learning about basic scripting.

share|improve this answer

Try to write your own, it's fun! :-)

Here are a few ideas and starting points:

  • Copy the initial data before you make any experiments! (Seriously, you'll need your backup at some point)
  • Make a new script called recompress.sh.
  • Make it executable with chmod +x recompress.sh.
  • You can use find to get a list of all files that need to be handled. This works recursively. See this Stackoverflow answer details.
  • You'd have to handle the zip files separately with unzip <fileanme> and all the tar files with tar xvf <filename>. Tar has auto detection for archive type. So make two separate find /folder -name ... loops. Or find a tool that can hanlde all archive types :-)
  • Delete all extracted archives (Remember that you've made a backup?)
  • compress it all with p7zip. Or if you want to customize the compression level and other stuff use 7zr directly.

Edit: Actually it might be a lot easier if you start by contructing the commands step by step and examining the errors/output of each.

share|improve this answer

You could write a script or do it the easy way by using Nautilus search.

  • Just search for .zip → highlight all zip files and choose → 'Extract here'
  • (optional) delete all *.zip files
  • redo the step for the other file formats

When done compress the root folder.
quick & dirty :)

PS: I love the terminal, but sometimes GUI is just faster.
PPS: Use tar.xz to compress the root folder.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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