compress directories with .tar extension

hi guys..

Since am a bit new to shell scripting, can anyone help me with this problem please.. i've been struggling with it since 2 days. :frowning:

I have a directory lets say myFolder and within it I have sub directories let say myFolder1.tar, myFolder2, myFolder3, etc. I need to write a shell script to check the extension of the sub directories and if they are not compress with the .tar extension compress them. At the end I should have myFolder1.tar, myFolder2.tar and myFolder3.tar.

tar is no compression utility - it is an archiving tool. Some tars have the option -z for compression but they use gzip/gunzip to do this.

An detailed example for your case:

## What we have at start
root@somehost:/data/tmp/testfeld> ll myFolder
insgesamt 24
drwxr-xr-x 6 root root  4096 2009-03-20 10:40 .
drwxr-xr-x 4 isau users 4096 2009-03-20 10:37 ..
drwxr-xr-x 2 root root  4096 2009-03-20 10:38 myFolder1
drwxr-xr-x 2 root root  4096 2009-03-20 10:40 myFolder142
drwxr-xr-x 2 root root  4096 2009-03-20 10:38 myFolder2
-rw-r--r-- 1 root root     0 2009-03-20 10:39 myFolder3.tar
drwxr-xr-x 2 root root  4096 2009-03-20 10:38 myFolder4

## Find directories in myFolder since an already tar'ed directory is a file now
root@somehost:/data/tmp/testfeld/myFolder> find . -type d -name "myFolder*" -print
./myFolder2
./myFolder142
./myFolder4
./myFolder1

## Now tar all we have found
root@somehost:/data/tmp/testfeld/myFolder> find . -type d -name "myFolder*" -exec tar cvf {}.tar {} \;
./myFolder2/
./myFolder142/
./myFolder4/
./myFolder1/

## Let's check what has happened
root@somehost:/data/tmp/testfeld/myFolder> ll
insgesamt 72
drwxr-xr-x 6 root root   4096 2009-03-20 10:44 .
drwxr-xr-x 4 isau users  4096 2009-03-20 10:37 ..
drwxr-xr-x 2 root root   4096 2009-03-20 10:38 myFolder1
drwxr-xr-x 2 root root   4096 2009-03-20 10:40 myFolder142
-rw-r--r-- 1 root root  10240 2009-03-20 10:44 myFolder142.tar
-rw-r--r-- 1 root root  10240 2009-03-20 10:44 myFolder1.tar
drwxr-xr-x 2 root root   4096 2009-03-20 10:38 myFolder2
-rw-r--r-- 1 root root  10240 2009-03-20 10:44 myFolder2.tar
-rw-r--r-- 1 root root      0 2009-03-20 10:39 myFolder3.tar
drwxr-xr-x 2 root root   4096 2009-03-20 10:38 myFolder4
-rw-r--r-- 1 root root  10240 2009-03-20 10:44 myFolder4.tar

Thanks for your quick reply... But when i try to execute this command
find . -type d -name "my*" -exec tar cvf {}.tar \;

I get the following error :frowning:

tar: Cowardly refusing to create an empty archive

Hello there,

The following KornShell script does the job

#!/bin/ksh

print -n "Enter the directory path: "
read DIRECTORY

if [[ ! -d $DIRECTORY ]]
then
    print -u2 "$DIRECTORY : no such directory"
    exit 2
fi

for ITEM in $(ls $DIRECTORY)
do
    if [[ -d $ITEM ]]
    then
        if [[ ! $ITEM = *.tar ]]
        then
            tar -cvf $ITEM.tar ./$ITEM
        fi
    fi
done

Regards,
:slight_smile:

Thanks for your reply.. but i foget to specify that i need the bash shell script. :slight_smile: I'm not so familiar with kornshell script.

If the nave the bash script please post it
Thanks

#!/bin/bash

DIRECTORY="."
echo -n "Enter the directory path: "
read DIRECTORY

if [[ ! -d $DIRECTORY ]]
then
    echo "$DIRECTORY : no such directory"
    exit 2
fi

for ITEM in $(ls $DIRECTORY)
do
    if [[ -d $ITEM ]]
    then
        if [[ ! $ITEM = *.tar ]]
        then
            tar -cvf $ITEM.tar ./$ITEM
        fi
    fi
done

When i run your script am getting {}.tar in my main directory myfolder. :confused:
I want to get myfolder.tar myfolder2.tar myfolder3.tar

Look at what I posted again and use copy & paste maybe to transfer the lines.

This is not a line from my example. tar is right of course.
Here my line again:

find . -type d -name "myFolder*" -exec tar cvf {}.tar {} \;

See the difference to yours?

I don't know very well Bash, I know Kornshell, However I run this second version (bash version) on my linux system (ubuntu) and it worked pretty well.

OK am using debian. Thanks for your help :b:

Its working fine now. I made a mistake while copying your command. :wink: I have one last question. Is it possible to remove the original directory after archiving it??

thanks for your help.. :slight_smile:

Oops I've just read the manual page for tar using the --remove-files options like below should do the job(remomving the original files after archiving them) right?!

find . -type d -name "myFolder*" -exec tar --remove-files cvf {}.tar {} \;

Why don't you try it out? :slight_smile: You can create test files with "touch" and directories with "mkdir".