Вы находитесь на странице: 1из 2

sign up

log in

tour

help

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

stack overflow careers

Take the 2-minute tour

Delete files older than 10 days using shell script in Unix [duplicate]

This question already has an answer here:

Shell script delete folders older than n days 4 answers


I'm new in shell scripts, can any one 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, thats why i need the current date.
Thx for the answers and sorry for my eng:)
bash

shell

unix

find

edited Feb 7 '13 at 23:33


StardustOne
48.5k

77

asked Nov 21 '12 at 8:46


Steve88

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

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 before

And take care that

./my_dir

exists to avoid bad surprises !


edited Dec 22 '14 at 17:02

answered Nov 21 '12 at 8:54


StardustOne
48.5k

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

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

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
150

If you can afford working via the file data, you can do
find -mmin +14400 -delete

answered Nov 21 '12 at 8:50


glglgl
43.9k

47

93

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

Вам также может понравиться