Documente Academic
Documente Profesional
Documente Cultură
log in
tour
help
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
Delete files older than 10 days using shell script in Unix [duplicate]
The script will run in every 10 day with Crontab, thats why i need the current date.
Thx for the answers and sorry for my eng:)
bash
shell
unix
find
77
91
85
12
marked as duplicate by tripleee, sgibb, keyser, Adam Arold, Undo Sep 5 '13 at 19:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Did you have a look at man date ? Jens Nov 21 '12 at 8:48
3
Do you need to delete based on the filename or the file's modification time? Barmar Nov 21 '12 at 8:49
i need a whole script:) i find an exapmle but i'm not sure that this is good for that: find /home/scripts/ . -name
'*.script' -mtime +10 -exec rm -rf '{}' \; Steve88 Nov 21 '12 at 8:54
3 Answers
find
EXPLANATIONS
./my_dir
-mtime +10
-type f
only files
-delete
./my_dir
77
91
find /home/scripts/*.script -mtime +10 type f -delete will be ok for delete these?
2012.11.21.09_33_52.script 2012.11.21.09_33_56.script 2012.11.21.09_33_59.script Steve88 Nov 21
'12 at 9:00
It depends of the date of the modification, like what ls -l displays. Are the date the same as ls -l ?
But a simple test will tell you =) StardustOne Nov 21 '12 at 9:06
yeah it is a creartion date of the script Steve88 Nov 21 '12 at 9:11
Be VERY careful to supply an absolute path on commands like these! Once, using a command very much
like this in a cron job, I accidentally deleted every file on my production mail server older than 10 days,
which I can tell you was no fun to recover from. DSimon May 28 '14 at 20:00
END_TIME=$(date +%s)
ELAPSED_TIME=$(expr $END_TIME - $START_TIME)
>> $log
The code build on sputnick's answer and adds a few more things.
log files named with a timestamp
log folder specified
find looks for *.txt files only in the log folder
log files older than 7 days are deleted ( assuming this is for a backup log)
notes the start / end time
calculates the elapsed time...
edited Jul 26 '13 at 12:30
If you can afford working via the file data, you can do
find -mmin +14400 -delete
47
93
To the anonymous editor: Which version if find has a -rm-rf option? glglgl Sep 19 '14 at 7:10