1758318 Members
2493 Online
108868 Solutions
New Discussion юеВ

finding files

 
Nagaraj Kris
Occasional Advisor

finding files

I have a situation where i have to find files in a directory /home/test which is created 1 hour before excluding filenames starts with abc* and if found i have to notify through email.
This should be checked for every hour

8 REPLIES 8
Tim Nelson
Honored Contributor

Re: finding files

Something basic.



ls /mydir/abc* > /dev/null
if [[ $? -eq 0 ]]
then
echo "abc files found - `date`"|mailx -m -s "File Alert" somebody@there.com
fi

OldSchool
Honored Contributor

Re: finding files

hmmm....

the 1 hour requirement means you're going to have to use "touch" to set a dummy file's time to 1 hour before current time, then use "find -newer" to find the files. Once you've got the list you can exclude what you want.

"man touch", "man find" and search HP forum with "find newer" for more info
James R. Ferguson
Acclaimed Contributor

Re: finding files

Hi:

If you are looking for files modifed in the last hour, use Perl:

# perl -MFile::Find -le 'find(sub{print if -f $_ && -M _ < (1/24) && m/^abc/},"/home/test")'

This finds files in the specified directory that are 1/24 of a day (one hour) or less in age. This age is the *modification* age of the file. There is no such thing as a "creation" timestamp in Unix.

If you would like to use pure shell:

# touch -amt 01101530 /tmp/myref
# find /home/myref -type f -newer /tmp/myref -name "abc*"

Of course, the "problem" in the shell solution is that you have to perform a touch of a reference file to create the one-hour delta everytime you want to search! The Perl solution obviates that.

Regards!

...JRF...
VK2COT
Honored Contributor

Re: finding files

Hello,

You got nice advice about Perl and
standard grep options.

As always, Unix tools offer such a wide variety of solutions. That is the
power of having freedom to chose the
one that suites you :)

If I understand your request correctly,
you want to EXCLUDE filenames that start
with "abc" string.

If you use GNU grep (much more
powerful that standard grep),
it is very easy. An example (line is
wrapped for readability purpose):

find /home/test -type f ! -name 'abc*' \
-cmin -60 -exec ls -als {} \; 2>/dev/null | \
mailx -s "SUMMARY: New abc files" username

Explanation about "-amin", "-cmin" and
"-mmin" flag arguments:

+n for greater than n
-n for less than n
n for exactly n

So...

"-cmin 60" means status changed EXACTLY
60 minutes ago

"-cmin -60" means status changed LESS THAN
60 minutes ago

"-cmin 60" means status changed MORE THAN
60 minutes ago

Cheers,

VK2COT
VK2COT - Dusan Baljevic
VK2COT
Honored Contributor

Re: finding files

Hello,

You got nice advice about Perl and
standard find options.

As always, Unix tools offer such a wide variety of solutions. That is the
power of having freedom to chose the
one that suites you :)

If I understand your request correctly,
you want to EXCLUDE filenames that start
with "abc" string.

If you use GNU find (much more
powerful that standard grep),
it is very easy. An example (line is
wrapped for readability purpose):

find /home/test -type f ! -name 'abc*' \
-cmin -60 -exec ls -als {} \; 2>/dev/null | \
mailx -s "SUMMARY: New abc files" username

Explanation about "-amin", "-cmin" and
"-mmin" flag arguments:

+n for greater than n
-n for less than n
n for exactly n

So...

"-cmin 60" means status changed EXACTLY
60 minutes ago

"-cmin -60" means status changed LESS THAN
60 minutes ago

"-cmin 60" means status changed MORE THAN
60 minutes ago

Cheers,

VK2COT
VK2COT - Dusan Baljevic
James R. Ferguson
Acclaimed Contributor

Re: finding files

Hi (again):

Oops, you want to EXCLUDE files beginning with the string "abc". Hence in either the Perl or the shell, we negate (!) the name matching:

# perl -MFile::Find -le 'find(sub{print if -f $_ && -M _ < (1/24) && $_ !~m/^abc/},"/home/test")'

# find /home/test -type f -newer /tmp/myref ! -name "abc*"

Regards!

...JRF...
VK2COT
Honored Contributor

Re: finding files

Hello,

Like James, I also made a typing error :)
I must be getting old and absent minded...

This is correct explanation:

"-cmin +60" means status changed MORE THAN
60 minutes ago

VK2COT
VK2COT - Dusan Baljevic
Nagaraj Kris
Occasional Advisor

Re: finding files

I Thank you all very much.
I will check it.