compress directories with .tar extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compress directories with .tar extension
# 1  
Old 03-20-2009
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. Smilie

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.
# 2  
Old 03-20-2009
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:
Code:
## 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

# 3  
Old 03-20-2009
Quote:
Originally Posted by zaxxon
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:
Code:
## 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 Smilie

tar: Cowardly refusing to create an empty archive
# 4  
Old 03-20-2009
Quote:
Originally Posted by kanexxx
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. Smilie

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.
Hello there,

The following KornShell script does the job

Code:
#!/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,
Smilie
# 5  
Old 03-20-2009
Quote:
Originally Posted by dariyoosh
Hello there,

The following KornShell script does the job

Code:
#!/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,
Smilie
Thanks for your reply.. but i foget to specify that i need the bash shell script. Smilie I'm not so familiar with kornshell script.

If the nave the bash script please post it
Thanks
# 6  
Old 03-20-2009
Quote:
Originally Posted by kanexxx
Thanks for your reply.. but i foget to specify that i need the bash shell script. Smilie I'm not so familiar with kornshell script.

If the nave the bash script please post it
Thanks
Code:
#!/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

# 7  
Old 03-20-2009
Quote:
Originally Posted by dariyoosh
Code:
#!/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. Smilie
I want to get myfolder.tar myfolder2.tar myfolder3.tar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compress Files in Multiple Directories

I would like to compress the files in multiple directories. For some reason, it only compress the first directory (/Sanbox/logs1) but not the rest of the other directories ("/Sanbox/logs2" "/Sanbox/logs3" "/Sanbox/logs4" ). Any help would be appreciated. Here's my code: #!/bin/bash... (1 Reply)
Discussion started by: Loc
1 Replies

2. Shell Programming and Scripting

Compress a tar file to smaller size

I have a tar file with name DTT012_GP_20140207.tar and many more with different names of different sizes ranging from 1GB to 4GB. Now my requirement is to extract/not extract these files and then divide it into various parts of size 500MB and save it with different names and then compress... (5 Replies)
Discussion started by: Shaibal_bp
5 Replies

3. UNIX for Dummies Questions & Answers

How to compress the directories which is older than 7 days?

Hi all, how to compress the directories which is older 7 days. If any one knows please help me this is urgent. Thanks in advance (3 Replies)
Discussion started by: rameshpagadala
3 Replies

4. Shell Programming and Scripting

Want to compress .war extension file

Hi All, I want to compress a .war file. bash-3.00$ ls -l /opt/test -rw-r--r-- 1 test test 15M Aug 22 18:20 old.war bash-3.00$ find . -type f -exec compress {} \; ./irc.war: -- file unchanged While try to compress the above file i am getting "file unchanged error"... (7 Replies)
Discussion started by: natraj005
7 Replies

5. Shell Programming and Scripting

tar and compress in one step

I know there is a way to tar up directory and sub-directories and have it compressed all in one command but but the syntax escapes me. I seem to re-call something like this: tar -cvf /tmp/file.tar - | compress ? Can somebody please provide me with the syntax on how to tar/compress and... (6 Replies)
Discussion started by: BeefStu
6 Replies

6. UNIX for Dummies Questions & Answers

Compress the file using Tar command

Hi, When i am tar the file particular ,csv file format in a folder i am receiving the error Command: tar cf New_data.tar /new/file/mari/getdata/small/*.xml Arguements too long But sometimes i am able to compress other folder but the tar folder contains all the file format and... (10 Replies)
Discussion started by: marivinay
10 Replies

7. UNIX for Dummies Questions & Answers

How to compress files without extension

Could someone please help? I'm trying to compress all the files in a directory without extension. I know for typical files with extension, the command is something like: tar -zcvf file.tar.gz *.doc What is the command for files without extension? Thanks. (4 Replies)
Discussion started by: AChan1118
4 Replies

8. Shell Programming and Scripting

Search, Tar and Compress in one line

Hi all, I am supposed to collect a huge amount of log files from a unix system (HP-UX) onto a local system. The log files are not in one place, but they are scattered all over the Unix server. The unix server has only limited space, so that I can not create a tar file first and then compress it.... (4 Replies)
Discussion started by: bluesky099
4 Replies

9. UNIX for Advanced & Expert Users

tar and compress

I need to compress and tar a couple files in a directory, but I also want the original files unchanged, ie if I compress a1.cpp , then a1.cpp becomes a1.cpp.z, but what I want after running the compress utility is to have both a1.cpp as it is and a1.cpp.z and then tar a1.cpp.z to an... (4 Replies)
Discussion started by: muru
4 Replies

10. UNIX for Dummies Questions & Answers

tar command with compress option...

Hi ! i have to write a script that archivs homes not used since 3 years. First, my script gathers the users that are concerned, using the following command : ll -lt /home/*/.sh_history | egrep '2000|1999|1998|1997' | awk '{print $3}' i obtain a list like this : user_1 user_2 ...... (3 Replies)
Discussion started by: tomapam
3 Replies
Login or Register to Ask a Question