I have created a function that looks for a folder in a particular directory, checks the date it was last modified and if its old then compress it.
This works fine for files using gzip. However for folders I had to use tar. This is my function:
the equivalent funtion for compressing old files uses
which works fine...
the problem with the tar one is I get an error message saying "Cannot write to a directory"
If I replace "{}" with for eg " Archive1 " then it works fine, however what I want it to do is find the folder and compress it, not create a new separate archive
for example if I have the following directories
Folder1
--Nov2010
--Dec2010
I want the function to compress those two folders to
Folder1
--Nov2010.tar.gz
--Dec2010.tar.gz
(Nov2010 and Dec2010 are located inside Folder1 hence the "--")
It's simply a matter of parameters, gzip takes a filename and zips it up replacing the original file with the zipped file and adding the .gz exception.
The tar command on the other hand needs to be told what to save the archive as. The paramter supplied after -f is the archive name.
Last edited by Skrynesaver; 10-15-2011 at 06:17 AM..
Reason: corrected markup
This User Gave Thanks to Skrynesaver For This Post:
Your problem is that you are trying to create a file (archive) with the same name as directory that you are compressing. Remember though, that directories in UNIX are just files, and you cannot create two files with the same name. So basically, you have a name conflict there...
Workaround would be to name archives with some extension like:
and then delete the dirs and rename the archives. The original dirs can be removed in the same tar command with --remove-files switch, if your version of tar supports it, like:
Then you just need to rename the .zipped archives.
=== EDIT: ===
After rereading your post, i realize all you need is to set the extension. If you invoke your function as
compressOldFolder Folder1
in your example; the find command should be:
Because doing
will try to compress the Folder1 and name the archive Nov2010 (but a file named Nov2010 already exists -- it's the orig dir)
You also want the -z option of tar to create gzipped archive
Last edited by mirni; 10-14-2011 at 08:01 AM..
Reason: -z option missing
I understand what the problem is
However both ways create an archive named "{}.tar.gz" or "{}.zipped"
also -z is not recognized as a perameter ( I use AIX )
I want it to keep the same name so I can have for example
Nov2010 and Nov2010.zipped
the problem is how can I get the function to use the foldername for creating the archive?
it seems to work fine, though it now causes a new problem
-print (or even just find) returns the folders as well as the parent path
for examble if I have Oct2010 and Nov2010 in Folder1 the find will return
as a result I will then have 3 compressed files
now I can easily remove the .tar.gz
but if I try to remove the original folders Oct2010 and Nov2010 and keep only the gz files by modifing my code as below
that will aslo remove the parent folder.
I know it is relativly easy to do an ls and grep the folder names then remove them, but I want to keep everything inside the function and keep it simple avoiding complexity. Is there a way to prevent find from returning the current path? (could not find something in man pages)
I have a tar file hello.tar which is 95 GB.
hello.tar has many files and folders including some tar files as well.
I wish to create a new tar ball which should maintain only the folder structure of hello.tar and the tar ball within the hello.tar
So basically the idea is to untar... (2 Replies)
I would like to confirm my file.tar is been tar-ed correctly before I remove them. But I have very limited disc space to untar it.
Can I just do the listing instead of actual extract it? Can I say confirm folder integrity if the listing is sucessful without problem?
tar tvf file1.tar
... (1 Reply)
How do I create individual tars of a all the directories in a directory? I have a directory called 'patients', each patient has a directory in the patients directory. I want to create tars such that each patient has their own tar file.
Thanks! (5 Replies)
Hello guys,
I am sure this has been asked before, but honestly, I cant find post talking about it.
Here is what I need:
- A tar file will be generated manually by user
- This tar file is then used within a bash shell script
My source folder structure is like this:
... (2 Replies)
Hi All-
I want to extract a particular folder from .tar format files.
For example:
File Name: backup.tar
The backup.tar contains the below folders & files.
1) /root_folder/Folder1/Folder1-1/*
2) /root_folder/Folder1/Folder1-2/*
3) /root_folder/Folder2/Folder2-1/*
4)... (5 Replies)
Hi all I'm new to your forum but not new to shells. I'm having a little trouble though as it's been quite some time since I scripted. Here's what I'm trying to do:
I'm trying to search a directory named '/var/root/Applications' for another directory 'fooBar'. The "Applications" directory... (9 Replies)
Hi all,
*I use Uwin and Cygwin emulator.
I´m trying to search for all text files in the current folder (C/Files) and its sub folders using
find -depth -name "*.txt"
The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Hi all,
4 files are returned when i issue 'find . -mtime -1 -type f -ls'.
./ora_475244.aud
./ora_671958.aud
./ora_934052.aud
./ora_934050.aud
However, when I issued the below command:
tar -cvf test.tar `find . -mtime -1 -type f`, the tar file only contains the 1st file -... (2 Replies)
Hi,
I want to extract myfile.war to a folder which is in the same folder with war file.I did this as normal:
jar -xvf myfile.war
But it exploded all the content of file to the same level folder instead of that I was expecting to create a folder called myfile.
This works with tar:
... (0 Replies)