LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-22-2017, 05:41 AM   #1
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Rep: Reputation: Disabled
cron job to delete regular files


Hi,

I have created the following cron job

37 12 * * * root find /home/mike/tmp -type f -exec rm {} \;

to delete files in /home/mike/tmp directory, however files are not deleted.

[root@rh2 ~]# ls /home/mike/tmp
mike1 mike2 mike3

Any idea?

Many thanks!
 
Old 01-22-2017, 05:52 AM   #2
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
The path is different in cron, so you should use the full path to find. There is also the risk of trouble if you have weird file names such as those with whitespace. So I'd say try -delete instead of -exec

Also, how exactly have you created and stored the cron job? There are two ways, each with a slightly different format.
 
1 members found this post helpful.
Old 01-22-2017, 05:57 AM   #3
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

thanks for your help!

I edited the /etc/crontab. I altered the config as following.

55 12 * * * root /bin/find /home/mike/tmp -type f -exec /bin/rm {} \;

but still it does not work

[root@rh2 tmp]# ls
mike1 mike2 mike3

Thanks!
 
Old 01-22-2017, 05:59 AM   #4
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Quote:
Originally Posted by chtsalid View Post
Hi,

thanks for your help!

I edited the /etc/crontab. I altered the config as following.

55 12 * * * root /bin/find /home/mike/tmp -type f -exec /bin/rm {} \;

but still it does not work

[root@rh2 tmp]# ls
mike1 mike2 mike3

Thanks!
I wouldn't expect it to. Find where find is on your system and use that path instead of /bin:

Code:
which find
Also instead of -exec /bin/rm {} \; try just -delete instead.

It will be easier if you work it out in an interactive shell first before adding it to cron
 
1 members found this post helpful.
Old 01-22-2017, 06:12 AM   #5
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

still not working

[root@rh2 tmp]# which find
/bin/find

11 13 * * * root /bin/find /home/mike/tmp -type f -name mike* -delete

 
Old 01-22-2017, 06:16 AM   #6
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Quote:
Originally Posted by chtsalid;
11 13 * * * root /bin/find /home/mike/tmp -type f -name mike* -delete
Ok. You have the right path. Now you've changed the formula by adding -name. That's great, but you'll need to wrap the name in quotes so that the asterisk is processed by find and not your shell. Without quotes, the shell applies globbing

Code:
/bin/find /home/mike/tmp -type f -name 'mike*' -delete
Also what method are you using to create the cronjob? sudo crontab -e or some other method?
 
1 members found this post helpful.
Old 01-22-2017, 06:20 AM   #7
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
I did, vi /etc/crontab

19 13 * * * root /bin/find /home/mike/tmp -type f -name 'mike*' -delete

but still nothing. Should I put anything else after setting the -delete option?

Thanks!
 
Old 01-22-2017, 06:26 AM   #8
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Does the following work in the shell?

Code:
/bin/find /home/mike/tmp -type f -name 'mike*' -delete
See about getting that working before moving that to cron.
 
1 members found this post helpful.
Old 01-22-2017, 06:29 AM   #9
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Yes, it works, it deleted all files.

Where does the problem lie then?

Thanks!
 
Old 01-22-2017, 06:35 AM   #10
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
If there's a problem it will usually show up in the logs. Which distro are you running, including version?
 
1 members found this post helpful.
Old 01-22-2017, 06:44 AM   #11
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
I am using CentOS 7.
 
Old 01-22-2017, 09:27 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,038

Rep: Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892Reputation: 2892
Look at the contents of /var/log/cron. To see the last 10 lines you can use the tail command i.e.

tail /var/log/cron (as root)

I see nothing obviously wrong with your find command and it works on my CentOS 7 system.
 
1 members found this post helpful.
Old 01-22-2017, 11:00 AM   #13
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Hi,

thank you for your input! I found the mistake. It had nothing to do with all these .

timedatectl local time was wrong. I changed it, and it worked having the following command configured.

[root@rh2 tmp]# crontab -l
56 17 * * * find /home/mike/tmp -type f -exec rm {} \;

[root@rh2 tmp]# tail /var/log/cron
Jan 22 17:55:17 rh2 crontab[17395]: (root) BEGIN EDIT (root)
Jan 22 17:55:24 rh2 crontab[17395]: (root) REPLACE (root)
Jan 22 17:55:24 rh2 crontab[17395]: (root) END EDIT (root)
Jan 22 17:55:31 rh2 crontab[17398]: (root) LIST (root)
Jan 22 17:55:52 rh2 crontab[17415]: (root) LIST (root)
Jan 22 17:56:01 rh2 crontab[17417]: (root) BEGIN EDIT (root)
Jan 22 17:56:01 rh2 crond[15011]: (root) RELOAD (/var/spool/cron/root)
Jan 22 17:56:01 rh2 CROND[17422]: (root) CMD (find /home/mike/tmp -type f -exec rm {} \
Jan 22 17:56:24 rh2 crontab[17417]: (root) REPLACE (root)
Jan 22 17:56:24 rh2 crontab[17417]: (root) END EDIT (root)

Thanks!
 
Old 01-22-2017, 11:33 AM   #14
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,171
Blog Entries: 3

Rep: Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061Reputation: 2061
Great. Be sure to run NTPd to keep the time synchronized, if you have time zone settled.

The -exec rm {} \; part is still problematic. You need to at least quote the {} part. Better would be to try the -delete option.
 
1 members found this post helpful.
Old 01-22-2017, 11:35 AM   #15
chtsalid
Member
 
Registered: Jan 2017
Posts: 67

Original Poster
Rep: Reputation: Disabled
Thank you for your input!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backing up and/or delete logs with a cron job. NaCo Linux - Server 2 08-01-2009 03:16 AM
regular print job using cron or something ? bigjohn Linux - Software 8 03-31-2005 09:14 PM
delete mails thru cron job massoo Linux - General 0 01-12-2005 02:19 AM
cron job to delete files based on attributes alpha21 Linux - General 3 11-09-2004 12:06 PM
howto delete files via ftp from cron job ? cccc Linux - Networking 2 01-31-2004 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration