1836861 Members
2168 Online
110110 Solutions
New Discussion

Re: use "find" command

 
SOLVED
Go to solution
ust3
Regular Advisor

use "find" command

I would like to use "find" to search file in a directory that elder than 10 days ( find . -mtime +10 -exec mv {} /tmp \; ) , if I would like the files will be gzip before mv , can advise what can I do ? thx
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

Re: use "find" command

I would suggest you write a script that takes N files and just do:
$ find . -mtime +10 -exec magic_script +

And in the script:
#!/usr/bin/ksh
for file in $*; do
mv $file /tmp
base=$(basename $file)
gzip /tmp/$base
done

Note if there are directory levels in your source directory, you would flatten them.
ust3
Regular Advisor

Re: use "find" command

thx reply ,

if it possible to do it with only ONE "find" statement ? thx
Steven Schweda
Honored Contributor
Solution

Re: use "find" command

> if it possible to do it with only ONE
> "find" statement ?

I see only one "find" command in that
suggestion. Or did you mean one "find"
command without the "magic_script"?

"find" is a useful program, but if you wish
to do any complex tasks with it, it's often
much simpler to use a "magic_script" instead
of trying to find a way to put anything even
slightly complex into a "find" command line.
A. Clay Stephenson
Acclaimed Contributor

Re: use "find" command

You need to change your question because UNIX has absolutely no way to know if a file is older than 10 days because UNIX has no notion of a file's creation date. You can determine if a file has been modified, changed (inode modified), or accessed within a given time interval but should any of these time metadata happen to match the time of a file's creation, it is nothing more than coincidence.
If it ain't broke, I can fix that.
ust3
Regular Advisor

Re: use "find" command

thx replies,

Yes , I mean to write one "find" command without the "magic_script" , that mean have two action after -exec , is it OK ?

thx
Dennis Handly
Acclaimed Contributor

Re: use "find" command

>I mean to write one "find" command without the "magic_script", that mean have two action after -exec, is it OK?

As Steve and I have said, it isn't worth it to strain your brain and write unmaintainable code to do that. You can only do one thing in each -exec. Having the script just makes it easier.
And with -exec ... +, there is no need to worry about performance.
Steven Schweda
Honored Contributor

Re: use "find" command

> [...] one "find" command without the
> "magic_script" , that mean have two action
> after -exec , is it OK ?

You can have more than one "-exec" option,
and that can give you more than one action,
but all kinds of non-simple things which you
might wish to do won't work. For example:

dy # find . -name y.c -exec echo {} \; -exec echo /tmp/{} \;
./y.c
/tmp/{}

On a "find" command line, "{}" must be a
"command argument", that is, a token on the
command line, not _part_ of a token on the
command line.

In a shell script (like "magic_script"), you
can use an expression like "/tmp/$1", and
everyone will be happy. Perhaps you can find
a way to do what you want with a complicated
"find" command, but in many (most?) cases,
it will be _much_ easier to use a separate
script to do anything which is not simple.

Why is it so important to do this job the
hard way?