0% found this document useful (0 votes)
196 views

Bash - Delete Files Older Than 10 Days Using Shell Script in Unix - Stack Overflow

This Stack Overflow post discusses how to delete files older than 10 days using a shell script in Unix. The top answer provides a find command to search a directory for files modified over 10 days ago and delete them. Another response adds logging functionality to the script to track deletion times. The discussion provides examples of find commands and considerations for ensuring the correct file paths are used when deleting files in scripts.

Uploaded by

Martín Lehoczky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views

Bash - Delete Files Older Than 10 Days Using Shell Script in Unix - Stack Overflow

This Stack Overflow post discusses how to delete files older than 10 days using a shell script in Unix. The top answer provides a find command to search a directory for files modified over 10 days ago and delete them. Another response adds logging functionality to the script to track deletion times. The discussion provides examples of find commands and considerations for ensuring the correct file paths are used when deleting files in scripts.

Uploaded by

Martín Lehoczky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2017­5­22 bash ­ Delete files older than 10 days using shell script in Unix ­ Stack Overflow

x Dismiss

Únete a la comunidad de Stack Overflow

Stack  Overflow  es  una  comunidad  de  7.2  millones


de  programadores  como  tú  que  se  ayudan
mutuamente. 
Únete  a  ellos,  sólo  te  llevará  un  minuto: 

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.

bash   shell   unix   find

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.

    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  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

And take care that  ./my_dir  exists to avoid bad surprises !

edited Oct 10 '16 at 18:01 answered Nov 21 '12 at 8:54
Gilles Quenot
73.5k 14 126 130

    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  ?

http://stackoverflow.com/questions/13489398/delete­files­older­than­10­days­using­shell­script­in­unix 1/2
2017­5­22 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 

    yeah it is a creartion date of the script –  Steve88  Nov 21 '12 at 9:11

1   it helps thx a lot –  Steve88  Nov 21 '12 at 9:45

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

    To the  anonymous editor: Which version if  find  has a  ‐rm‐rf  option? –  glglgl  Sep 19 '14 at 7:10

http://stackoverflow.com/questions/13489398/delete­files­older­than­10­days­using­shell­script­in­unix 2/2

You might also like