Crontab deleting files command question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Crontab deleting files command question
# 1  
Old 05-03-2013
Crontab deleting files command question

Hello out there,
Our system has a pdf generator that creates pdf files. We dont need them pas 120 days.
So I have this command in my crontab. I currently set it to "0" for testing. But normally have it set to -mtime 120 to remove files out of the folders from PDF out to several other potential folders out. Nothing ever happens though. Crontab runs. Just this command never seems to happen. Is there something missing?
Code:
0 0 * * * find /prod/app/$LOGNAME/data/audit/pdf/*/*/*.pdf -type f -name '*.pdf' -mtime 0 -exec rm {} \;


Last edited by Scott; 05-04-2013 at 10:17 AM.. Reason: Code tags
# 2  
Old 05-03-2013
Quote:
Originally Posted by vsekvsek
Hello out there,
Our system has a pdf generator that creates pdf files. We dont need them pas 120 days.
So I have this command in my crontab. I currently set it to "0" for testing. But normally have it set to -mtime 120 to remove files out of the folders from PDF out to several other potential folders out. Nothing ever happens though. Crontab runs. Just this command never seems to happen. Is there something missing?
0 0 * * * find /prod/app/$LOGNAME/data/audit/pdf/*/*/*.pdf -type f -name '*.pdf' -mtime 0 -exec rm {} \;

Are you saying it is never executed, or it runs but doesn't do a thing?

As a rule of thumb you should always use full paths when using crontab since it doesn't have a profile.

Also, find is recursive so this:
Code:
find /prod/app/$LOGNAME/data/audit/pdf/*/*/*.pdf  <...>

Can be replaced with this:
Code:
find /prod/app/$LOGNAME/data/audit/pdf/ -name "*.pdf" <...>

Lastly, rm is oftentimes alias'ed to "rm -i" so it may be expecting user input. Again try using full paths to avoid this behavior. Try:
Code:
0 0 * * * /usr/bin/find /prod/app/$LOGNAME/data/audit/pdf/ -type f -name "*.pdf" -mtime 0 -exec /bin/rm {} \;


Last edited by verdepollo; 05-03-2013 at 12:43 PM.. Reason: Added [icode]
# 3  
Old 05-03-2013
Are you sure the shell defines $LOGNAME when invoked by cron?
Better set its value directly in the crontab!
rm is certainly not aliased in a script but the rm can become interactive by itself, so better use rm -f

Last edited by MadeInGermany; 05-03-2013 at 11:09 PM..
# 4  
Old 05-04-2013
man find:
Quote:
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
So you might need to specify -mtime +0 to find any files, as 0 alone will just find those files modified exactly NOW.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-04-2013
Better, you can create a script for this command as already discussed here, cron doesn't have profile. So create a script with needed parameters and call the script in cron
# 6  
Old 05-04-2013
Quote:
But normally have it set to -mtime 120 to remove files out of the folders
-mtime 120 finds (and removes) files 120 days old.

-mtime +120 finds files at least 120 days old, and is what you want to use.
# 7  
Old 05-04-2013
Poster @hanson44 has the correct solution.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Crontab question

I need to run an script every 10th and 25th day in every month at 11pm. the script name is /home/ss/automated.sh I tried to execute the script every day and everytime with the below syntax. its not executing it from crontab. * * * * * /home/ss/automated.sh Any idea why it not... (6 Replies)
Discussion started by: ramkumar15
6 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. Shell Programming and Scripting

Deleting all empty files in sub directories with a command

Hello Friends, Im trying to delete empty files in subdirectories with a command. I can find them checking only one directory in each step and then show them with my command like below moreover i could not add removing part: ls -l */* | awk '{if ($5==0) printf "%3s %2d %s... (5 Replies)
Discussion started by: EAGL€
5 Replies

4. Shell Programming and Scripting

output file of the shell script running through crontab is deleting automatical daily.

Dear Friends, I am working on IBM AIX. I have written one script and kept in the crontab as to run daily at 11:38 AM. and the output of the script to be appended to the file generated with the month name. but my file deleting daily and the new file is creating with the output of the shell... (2 Replies)
Discussion started by: innamuri.ravi
2 Replies

5. Shell Programming and Scripting

Deleting files using find command

I want to find the files and delete all the files except the last file. I am using find command , I am sending the find output to a file and getting all the lines except the last one and sending it to the remove command . This is not working. can anyone help me out to do it in the find command... (8 Replies)
Discussion started by: deepaklanka
8 Replies

6. UNIX for Dummies Questions & Answers

Deleting access_log.processed in crontab

Hi, I've worked out that my server was getting clogged with the access_log.processed file. I deleted it using the command > /var/www/vhosts/domain.com/statistics/logs/access_log.processed I also set that up as a crontab job for every Wednesday. What I was wondering is the version using... (4 Replies)
Discussion started by: chickenhouse
4 Replies

7. Shell Programming and Scripting

command for deleting log files based on some condition

Hello, Can anyone pls. provide me with the command for deleting files older then 15 days with a restriction to keep at least 5 files in a directory even if they are older then 15 days. Any help will be highly appreciated. Thanks, Pulkit (4 Replies)
Discussion started by: pulkit
4 Replies

8. UNIX for Dummies Questions & Answers

file deleting question

somehow one of my directories got a number of files whose names start with a dash - e.g. -1129.txt how can I remove them? If I issue rm -1129.txt I get the message of illegal options if I issue rm /-1129.txt I get a message that -1129.txt is not found Lisa HP-UX 11.23 i64 (3 Replies)
Discussion started by: LisaS
3 Replies

9. UNIX for Dummies Questions & Answers

Deleting files with zero length using ls command

Hi, How can I use ls command to Delete all files with zero length in a given path using ls command (I guess awk is required!)? Thanks. (6 Replies)
Discussion started by: GNMIKE
6 Replies

10. UNIX for Dummies Questions & Answers

crontab question

I'm trying to create a crontab entry that will run the who command once per hour Monday-Friday. Any command output should be appended to the file whoison.log in my home directory. This is what I wrote: crontab 0 * * * 1-5 who >> /home/myhome/whoison.log Did I do this right? Thanks (7 Replies)
Discussion started by: Relykk
7 Replies
Login or Register to Ask a Question