Operating System - HP-UX
1833017 Members
2111 Online
110048 Solutions
New Discussion

Re: new to cron, scheduled find & delete

 
SOLVED
Go to solution
Rob Bailey_3
Occasional Advisor

new to cron, scheduled find & delete

hey, well i'm running free webhosting and i need to set up a cron job that will find all of a certain file type and delete them. can someone please post me the code. thanks.
20 REPLIES 20
Michael Tully
Honored Contributor
Solution

Re: new to cron, scheduled find & delete


e.g. find /filesystem -name -exec rm {} \;

you can use wilcards as well
find /filesystem/directory -name "*filename*" -exec rm {} \;

So in cron to run everyday at 15:44

44 15 * * * /usr/bin/find /filesystem/directory -name "*filename*" -exec rm {} \;

You also set this up in a script as well and point cron to the script
Anyone for a Mutiny ?
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

will it tell me what files were deleted and is what directories?
Con O'Kelly
Honored Contributor

Re: new to cron, scheduled find & delete

Hi

No it doesn't tell you the files that were deleted.
Maybe run another find prior to this and send output to file so you have a record:
find // -name "*" -exec ll {} \; >> /tmp/find_delete.log

Cheers
Con
Geoff Wild
Honored Contributor

Re: new to cron, scheduled find & delete

How about something like this:

find /hostingdir -name *.xls -print | xargs ls {} \; > /tmp/files2del.txt

for i in `cat /tmp/files2del.txt`
do
rm $i
done

Replace *.xls with whatever...

Note - the reason to use xargs instead of exec is right in the man page for find:

Note that output from find was piped to xargs(1) instead of using
the -exec primary. This is because when a large number of files
or directories is to be processed by a single command, the -exec
primary spawns a separate process for each file or directory,
whereas xargs collects file names or directory names into
multiple arguments to a single chmod command, resulting in fewer
processes and greater system efficiency.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

can i run -print and -exec in the same command line?
Geoff Wild
Honored Contributor

Re: new to cron, scheduled find & delete

Yes, you can run -exec in same command line - but why? use xargs instead:

find /hostingdir -name *.xls -print | xargs rm {} \; > /tmp/files2del.txt

I thought you might want 2 scripts - run the find the first night, then next day, check the contents prior to removing....

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Michael Tully
Honored Contributor

Re: new to cron, scheduled find & delete

You can run both yes. The problem with using xargs is that if it doesn't find anything with the find command (xargs is a filter) is that is will remove files in the currect directory.

You can of course run a script to get the contents of the directory and place them in a file before you delete them.
Anyone for a Mutiny ?
Geoff Wild
Honored Contributor

Re: new to cron, scheduled find & delete

Michael - not too sure what you mean by "does't find anything"?

# find / -name *.html -print | xargs ls {} \; >/tmp/files.txt


# head -25 /tmp/files.txt
/home/Inetpub/wwwroot/banner.html
/home/Inetpub/wwwroot/index1.html
/home/Inetpub/wwwroot/index2.html
/home/Inetpub/wwwroot/indexf.html
/home/Inetpub/wwwroot/index.html
/home/Inetpub/wwwroot/mhpo/11thconvagenda.html
/home/Inetpub/wwwroot/mhpo/11thconv.html
/home/Inetpub/wwwroot/mhpo/11thconvspeak.html
/home/Inetpub/wwwroot/mhpo/12thconv.html
/home/Inetpub/wwwroot/mhpo/benefits.html
/home/Inetpub/wwwroot/mhpo/board.html
/home/Inetpub/wwwroot/mhpo/bullnews.html
/home/Inetpub/wwwroot/mhpo/cariboo.html
/home/Inetpub/wwwroot/mhpo/classifieds.html
/home/Inetpub/wwwroot/MIS-NEW1/aboutus.html
/home/Inetpub/wwwroot/MIS-NEW1/index1.html
/home/Inetpub/wwwroot/MIS-NEW1/index.html
/home/Inetpub/wwwroot/MIS-NEW1/products.html
/home/Inetpub/wwwroot/MIS-NEW1/services.html
/home/Inetpub/wwwroot/MIS-NEW2/spry.html
/home/Inetpub/wwwroot/MIS-NEW/help.html
/home/Inetpub/wwwroot/MIS-NEW/index.html
/home/Inetpub/wwwroot/MIS-NEW/links.html
/home/Inetpub/wwwroot/MIS-NEW/news.html
/home/Inetpub/wwwroot/MIS-NEW/Smokey.html

Works for me...

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

note: i am running this through cpanel, anyways so i can do this?

find public_html/ -name *.mp3 -print | exec rm {} \; >> /tmp/find_delete.log

im just trying to get this actually in my memory, so i dont have to ask all this again, sorry
Con O'Kelly
Honored Contributor

Re: new to cron, scheduled find & delete

Hi

Geoff
I think what Michael means is that 'xargs' works if files exist. If there is no files to find it returns everything in the directory
eg find . -name "file_doesnt_exist" -print | xargs ls

Rob
Use 2 find commands:
find public_html/ -name *.mp3 -exec ls -l {} \; >> /tmp/find_delete.log

find public_html/ -name *.mp3 -exec rm {} \;

Cheers
Con
Geoff Wild
Honored Contributor

Re: new to cron, scheduled find & delete

Oh - I see - if it doesn't find anything:

# find /tmp -name *.unknownfile -print | xargs ls {} \; >/tmp/files.txt

# cat /tmp/files.txt

#

Returns nothing - so even it it was rm instead of ls - then it wouldn't remove anything...

As a test (don't do this at home!!):

cd tmp
ls >tmp.files
cat tmp.files
backup.log
files.txt
ftpdunebackup.log
lost+found
makewhatisNUWseo
orbit-root
tmp.files

find /tmp -name *.unknownfile -print | xargs rm {} \; >/tmp/files.txt

rm: cannot remove `{}': No such file or directory
rm: cannot remove `;': No such file or directory
[root@arrakis tmp]# ls
backup.log ftpdunebackup.log makewhatisNUWseo tmp.files
files.txt lost+found orbit-root

Well - it didn't delete anything....

Rgds...Geoff






Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Michael Tully
Honored Contributor

Re: new to cron, scheduled find & delete


find public_html/ -name *.mp3 -print | exec rm {} \; >> /tmp/find_delete.log

should be:
find /public_html -name "*mp3" -print | exec rm {} \; >> /tmp/find_delete.log

You must use double quotes for wildcards.
Anyone for a Mutiny ?
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

can i search for more than one file type? like all media files?
Geoff Wild
Honored Contributor

Re: new to cron, scheduled find & delete

Sure:

find /tmp \( -name '*.wav' -o -name '*.mp3' \) -print | xargs rm {} \; >/tmp/files.txt

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

do i use -o for every different file type?
Sanjay Kumar Suri
Honored Contributor

Re: new to cron, scheduled find & delete

man find (-o stand for Logical OR)

expression -o expression: Logical OR operator. True if either or both of the expressions are true.

sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

so then i use...
Rob Bailey_3
Occasional Advisor

Re: new to cron, scheduled find & delete

ok i just used find public_html/ -name *avi -print but nothing shows up if it has a space in the name of the file.
Sanjay Kumar Suri
Honored Contributor

Re: new to cron, scheduled find & delete

Some corrections:

find /public_html -name "*avi" -print


sks
A rigid mind is very sure, but often wrong. A flexible mind is generally unsure, but often right.
Geoff Wild
Honored Contributor

Re: new to cron, scheduled find & delete

Rob,

Welcome to the forum by the way.

Be sure to read:

http://forums1.itrc.hp.com/service/forums/helptips.do?#overview

And then assign points to those that have helped you.

Thanks...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.