1833431 Members
3319 Online
110052 Solutions
New Discussion

log of deleted files

 
John M_1
Occasional Contributor

log of deleted files

Hi all,

I have a cron job script to delete output reports files from a directory older than 20 using the following command:
>>>
find . -mtime +1 -exec ll {} \;
find . -mtime +1 -exec rm {} \;
<<<
Is there a way that I can log all the files deleted in one single command rather than have two file sweeps.

Thanks very much in advance,
John
11 REPLIES 11
Peter Kloetgen
Esteemed Contributor

Re: log of deleted files

Hi John,

try the following command:

find . -mtime +1 -exec ls -A {} \; | tee log_file | xargs rm

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Ulf Lipski
Advisor

Re: log of deleted files

Hi,

just consider that using xargs is limited by the total count of the argument list. Therefore it doesn't work if there are too many files.

By, Ulf
Optimization is the root of all evil.
John M_1
Occasional Contributor

Re: log of deleted files

Thanks again to all,
how about this command:

find . -mtime +1 -exec ls -A {} \; |tee log_files | awk '{print "rm "$1}' |sh

I guess there will not be restrictions as Ulf pointed out
Heiner E. Lennackers
Respected Contributor

Re: log of deleted files

Hi,

the main use of xargs is to avoid having to much arguments to a command. with the xargs option -n you can specify how many argument you want to give to each execution of the specified command.

But another problem you will face with the xargs solution are blanks in a filename. i would use the following command:

find . -mtime +1 | tee -a /path/to/mylogfile | xargs -i rm "{}"

Heiner
if this makes any sense to you, you have a BIG problem
Systeemingenieurs Infoc
Valued Contributor

Re: log of deleted files

if you're only interested in the filenames, you can do :

find . -mtime +1 -print -exec rm {} \;
A Life ? Cool ! Where can I download one of those from ?
Darrell Allen
Honored Contributor

Re: log of deleted files

Hi John,

For your example:
find . -mtime +1 -exec ll {} \; -exec rm {} \;

You may want to add the "-d" option to "ll" if you have sub-directories. Or you chould use "-type f" option for the find.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Eric Ladner
Trusted Contributor

Re: log of deleted files

There's nothing that says you can't have two exec's in the same find command.

find . -mtime -1 -exec ll {} \; -exec rm {} \;

Just make sure the rm is last or the ll won't work. They are executed in order.

This will retain your previuos behavior as well (i.e. output in the mail from cron, not some other log file)
James R. Ferguson
Acclaimed Contributor

Re: log of deleted files

Hi John:

You can also use the '-t' (trace) option of 'xargs' to echo to stderr the command that will be executed, in this case thereby capturing a log of those files which are removed:

# find . -mtime +1 -type f xargs -L 500 -t -i rm {} 2> /tmp/removedfiles

I've added the '-L' option to 'xargs' to limit the number of arguments processed during each interation. This avoids the argument-too-long problem, particularly on 10.x releases.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: log of deleted files

Hi (again) John:

I'm sorry, I dropped a pipe. The command should read:

# find . -mtime +1 -type f | xargs -L 500 -t -i rm {} 2> /tmp/removedfiles

Regards!

...JRF...


Martin Johnson
Honored Contributor

Re: log of deleted files

find . -mtime +1 -exec rm {} \; -print

will work too. You can alway redirect the output to a file.

HTH
Marty
harry d brown jr
Honored Contributor

Re: log of deleted files

Of course, don't forget to EMAIL that list of deleted files:

uuencode /tmp/logfiles logfiles.txt | xmail -m -s "log of deleted files" yourname@your.com someonelsesname@your.com

live free or die
harry
Live Free or Die