Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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'm looking for a single line command or a script that archives 5 folders into .tar files (no gzip/bzip needed) in a certain directory and deletes the folders after a successful compression. It has to use the original folder name as file name for the archive too.

So far I've used the current command, which only does one directory per time:

tar -c directory > directory.tar && rm -rf directory

Thanks in advance.

share|improve this question
up vote 1 down vote accepted

This will process the first five directories under the directory passed as an argument to the script.

for directory in $1/*
    if [[ -d "$directory" ]]
        tar -cf "${directory}.tar" "$directory" && rm -rf "$directory"
        if (( c++ > 5 ))

I'm curious about the reason for the limit.

share|improve this answer
The limit is simply to avoid folder that shouldn't be from being archived – EarthMind May 14 '10 at 19:14
tar -cf "${directory}.tar" "$directory" && rm -rf "directory" should be tar -cf "${directory}.tar" "$directory" && rm -rf "$directory", right? – EarthMind May 14 '10 at 19:24
@EarthMind: Yes, that was a typo. I've fixed it. Wouldn't it be better to specify which particular directories (by name or other criteria) you want archived or to exclude particular directories (by name, etc.) rather than just saying "archive the first five"? – Dennis Williamson May 14 '10 at 22:08
Yes, that would be a much better way indeed. I just wanted to keep it simple at first. – EarthMind May 21 '10 at 10:41

it's a bit lengthy but:

for i in dir1 dir2 dir3; do tar -cf $i.tar $i; if [ $? -eq 0 ]; then rm -rf $i; fi; done

edit; tar -cf dir.tar dir is a little more concise than tar -c dir > dir.tar

example script:

tar -cf $DIR.tar $DIR/
if [ $? -eq 0 ]; then
  rm -rf $DIR

you can plop that into, chmod 755 and then run it like directory

however, you could add a lot more to it, to get a list of directories from your main directory, and even use a file to keep track of the last time directory was archived, so you can set your maximum, etc.

share|improve this answer
What does the if [ $? -eq 0 ]; check for? – EarthMind May 14 '10 at 18:23
$? is the return value for the tar command, 0 is successful – cpbills May 14 '10 at 18:24
Won't $i contain a slash? If so the script won't create the archives. – EarthMind May 14 '10 at 18:32
$i may contain a slash if you're doing for i in * but you could remove it with sed, but again, we're getting pretty complicated, and this might work better as a multi-line script, for clarity; edit: doing a quick check, trailing slash is not printed with for i in *; do echo $i; done – cpbills May 14 '10 at 18:39
for dir in dir1 dir2 dir3 dir4 dir5 ; do tar -c "$dir" > "$dir".tar && rm -rf "$dir" ; done
share|improve this answer
Is there an alternative without hardcoding the directory names? – EarthMind May 14 '10 at 18:22
if all the directories are in one directory, you can do for i in /directory/* – cpbills May 14 '10 at 18:23
They are in the same directory but I need to set a maximum so that it doesn't archive all the directories. – EarthMind May 14 '10 at 18:31
a maximum? this is one line to fit into a crontab, i presume? as you get more complicated, you might want to think about writing this out to a script file, and then calling that script in the crontab... that'd be much cleaner. – cpbills May 14 '10 at 18:37
A script would be nice too. One that would allow me to execute it this way: directory – EarthMind May 14 '10 at 18:42

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.