| Author |
Need some comments on a Unix script to remove file and compress
|
|
| Herbert 2005-12-14, 5:57 pm |
| Hello,
There are 6 files. The files have time stamp like
aafile.NOV-12-09,
bbafile.NOV-12-09,
ccfile.NOV-12-09,
ddafile.NOV-12-09,
eefile.NOV-12-09,
fffile.NOV-12-09
I made a simple script to compress all the files over a month and
delete them over 6 months like below.
#!/bin/ksh
#
IMPORT_FILES= "Aafile.* \
Bbafile.* \
Ccfile.* \
Ddafile.* \
Eefile.* \
Fffile.*"
#
FILE_DIR=/xx/xx
#
#
cd $FILE_DIR
for i in $IMPORT_FILES
do
find ${i} -mtime +30 -type f -print0 | xargs -0 compress -vf
find ${i} -mtime +180 -type f -print0 | xargs -0 rm -vf
done
There are other files in the directory but I want to compress those 6
files over a month (actually many files with the same name but with
different time stamp) and delete them over
6 months.
Any comments will be highly appreciated. Any other way to do this?
Thanks
Herbert
| |
| Doug Freyburger 2005-12-16, 5:57 pm |
| Herbert wrote:
>
> The files have time stamp like
> aafile.NOV-12-09,
....
> I made a simple script to compress all the files over a month and
> delete them over 6 months like below.
>
> #!/bin/ksh
> #
> IMPORT_FILES= "Aafile.* \
> Bbafile.* \
> Ccfile.* \
> Ddafile.* \
> Eefile.* \
> Fffile.*"
Typo note - They are capitalized here but not above.
> #
> FILE_DIR=/xx/xx
> #
> #
> cd $FILE_DIR
>
> for i in $IMPORT_FILES
> do
>
> find ${i} -mtime +30 -type f -print0 | xargs -0 compress -vf
> find ${i} -mtime +180 -type f -print0 | xargs -0 rm -vf
>
> done
>
> There are other files in the directory but I want to compress those 6
> files over a month (actually many files with the same name but with
> different time stamp) and delete them over
> 6 months.
>
> Any comments will be highly appreciated. Any other way to do this?
I would rather use the find's to drive the loops rather than use the
loops to drive the find's:
find . \( -name "aafile*" -o -name "bbfile*" ... \) -mtime +30 -print
while read victim ; do
compress -vf ${victim}
done
and then repeat with the 180.
Just checking since it's been a long time since I switched from
compress
to gzip - Compressing preserves mtime, right?
| |
| Herbert 2005-12-18, 8:49 pm |
| I am new to Unix. What is the big difference? If the number of files
that I'd like to handle in the future change, I have to change find
command (kind of hard coded). I'd like to use just one find command to
do both for +30 and +180. Is it possible to do it?
Many thanks
| |
| Bit Twister 2005-12-18, 8:49 pm |
| On 18 Dec 2005 17:21:31 -0800, Herbert wrote:
> I am new to Unix. What is the big difference? If the number of files
> that I'd like to handle in the future change, I have to change find
> command (kind of hard coded). I'd like to use just one find command to
> do both for +30 and +180. Is it possible to do it?
I would have thought a +30 would pick up a +180.
Then there is the concept of for loops. Short example
for _offset in 30 180
do
find -atime +${_offset} ...
done
| |
| David Nixon 2005-12-19, 7:49 am |
| > Hello,
[snip]
> I made a simple script to compress all the files over a month and
> delete them over 6 months like below.
> #!/bin/ksh
> #
> IMPORT_FILES= "Aafile.* \
> Bbafile.* \
> Ccfile.* \
> Ddafile.* \
> Eefile.* \
> Fffile.*"
> #
> FILE_DIR=/xx/xx
> #
> #
> cd $FILE_DIR
This script doesn't check that if has in fact changed
directory. It could, potentially, commence operations from
the root of your directory tree.
| |
| Doug Freyburger 2005-12-19, 6:02 pm |
| Herbert wrote:
>
> I am new to Unix.
And new to UseNet as well it appears. It is important on UseNet to
quote context. never assume the reader can see the previous post.
The style I use here has been UseNet standard since long before
the Web existed but it isn't the only appropriate quoting style.
> What is the big difference? If the number of files
> that I'd like to handle in the future change, I have to change find
> command (kind of hard coded).
You gave a for loop that looped through all filenames. I gave a
find that used name patterns. The "big" difference is tiny until the
difference becomes "big". Chuckle. The problem is the limited
buffer of command line parsers. If the list of files in your loop
grows to beyond maybe 500, your loop will blow its buffer and
fail while my find expression will continue to work. Before that
many matching files exist I merely showed you an alternate way
to code the same thing.
> I'd like to use just one find command to
> do both for +30 and +180. Is it possible to do it?
Sure but it's more work than it's worth. Look to the -exec switch
in find, how to use parens, how to use -o. Something like
find ... \( -mtime +30 -exec compress {} \; -prune \) -o \( -mtime +180
....
The trick is getting that -prune to work correctly so if it compresses
it does not bother removing.
| |
| Herbert 2005-12-20, 5:57 pm |
| How do I check it? Could you let me know please?
| |
| Dave Hinz 2005-12-20, 5:57 pm |
| On 20 Dec 2005 11:32:25 -0800, Herbert <kang_uni@hotmail.com> wrote:
> How do I check it? Could you let me know please?
Nobody knows what or who you are responding to, because you didn't
include any context. Let me guess: you use google to post.
|
|
|
|