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.
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.
zaxxon
March 20, 2009, 5:51am
2
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
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:
## 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
tar: Cowardly refusing to create an empty archive
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.
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
#!/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,
dariyoosh:
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,
Thanks for your reply.. but i foget to specify that i need the bash shell script. 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.
I want to get myfolder.tar myfolder2.tar myfolder3.tar
zaxxon
March 20, 2009, 7:38am
8
Look at what I posted again and use copy & paste maybe to transfer the lines.
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
tar: Cowardly refusing to create an empty archive
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
zaxxon:
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?
Its working fine now. I made a mistake while copying your command. I have one last question. Is it possible to remove the original directory after archiving it??
thanks for your help..
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 {} \;
zaxxon
March 20, 2009, 10:29am
13
Why don't you try it out? You can create test files with "touch" and directories with "mkdir".