Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to setup some kind of script to go through a set of folders, compressing each one, and only leaving the compressed copy of each. How can I do that?

share|improve this question
The title doesn't pretty much say it. What do want to do exactly? Do you want a script that performs these actions? What type of archive do you want? What order do you want to execute? – André Stannek Jul 22 '12 at 14:50
To be exact I want to 1. compress a folder, 2. After it compresses that folder, delete that folder (Where the backup compressed file remains) 3. Pick the next folder on the list and repeat. I Suppose I do want a script. – TardisGuy Jul 22 '12 at 15:15
How does the list with folders look? – André Stannek Jul 22 '12 at 15:16
up vote 3 down vote accepted

This script takes a list of folders as arguments. Save it as e.g. and call it like this:

./ Folder1 Folder2

You have to make the script executable before this:

chmod u+x

Here comes the script:

for folder in "$@"
    tar czf $folder.tgz $folder
    rm -R $folder

Tell me if this is not what you need. If you are not sure what it's doing make a backup of your data before you test it ;-)

share|improve this answer
It did work. ty much. – TardisGuy Jul 27 '12 at 12:02
You're welcome! :-) – André Stannek Jul 27 '12 at 12:32

Your Answer


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.