Bash - Delete Files Older Than 10 Days Using Shell Script in Unix - Stack Overflow
Bash - Delete Files Older Than 10 Days Using Shell Script in Unix - Stack Overflow
x Dismiss
Únete a la comunidad de Stack Overflow
Registrarse
Delete files older than 10 days using shell script in Unix [duplicate]
This question already has an answer here:
Shell script to delete directories older than n days 5 answers
I'm new to shell scripts, can anyone help? I want to delete scripts in a folder from the current date back to 10 days. The scripts
looks like:
2012.11.21.09_33_52.script
2012.11.21.09_33_56.script
2012.11.21.09_33_59.script
The script will run in every 10 day with Crontab, that's why I need the current date.
edited Oct 19 '15 at 20:30 asked Nov 21 '12 at 8:46
Morgan Thrapp Steve88
6,001 2 22 49 532 1 8 23
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.
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 is the common tool for this kind of task :
find ./my_dir ‐mtime +10 ‐type f ‐delete
EXPLANATIONS
./my_dir your directory (replace with your own)
‐mtime +10 older than 10 days
‐type f only files
‐delete no surprise. Remove it to test your find filter before executing the whole
command
edited Oct 10 '16 at 18:01 answered Nov 21 '12 at 8:54
Gilles Quenot
73.5k 14 126 130
http://stackoverflow.com/questions/13489398/deletefilesolderthan10daysusingshellscriptinunix 1/2
2017522 bash Delete files older than 10 days using shell script in Unix Stack Overflow
But a simple test will tell you =) – Gilles Quenot Nov 21 '12 at 9:06
37 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
Just spicing up the shell script to delete older files
#!/bin/bash
timestamp=$(date +%Y%m%d_%H%M%S)
path="/data/backuplog"
filename=log_back_$timestamp.txt
log=$path/$filename
find $path ‐name "*.txt" ‐type f ‐mtime +7 ‐print ‐delete >> $log
echo "Backup:: Script Start ‐‐ $(date +%Y%m%d_%H%M)" >> $log
START_TIME=$(date +%s)
... code for backup ...or any other operation ....
END_TIME=$(date +%s)
ELAPSED_TIME=$(expr $END_TIME ‐ $START_TIME)
echo "Backup :: Script End ‐‐ $(date +%Y%m%d_%H%M)" >> $log
echo "Elapsed Time :: $(date ‐d 00:00:$ELAPSED_TIME +%Hh:%Mm:%Ss) " >> $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 answered Jul 26 '13 at 11:57
MarcoZen
335 4 11
If you can afford working via the file data, you can do
find ‐mmin +14400 ‐delete
answered Nov 21 '12 at 8:50
glglgl
55.4k 6 74 131
http://stackoverflow.com/questions/13489398/deletefilesolderthan10daysusingshellscriptinunix 2/2