How to Delete Files Older Than Specific Date in Linux

How do you delete files older than specific date in Linux?

You can touch your timestamp as a file and use that as a reference point:

e.g. for 01-Jan-2014:

touch -t 201401010000 /tmp/2014-Jan-01-0000

find /path -type f ! -newer /tmp/2014-Jan-01-0000 | xargs rm -rf

this works because find has a -newer switch that we're using.

From man find:

-newer file
File was modified more recently than file. If file is a symbolic
link and the -H option or the -L option is in effect, the modification time of the
file it points to is always used.

How to delete files older than a certain date in file name in Ubuntu

I used the following based on the answer to this question :

weekago=`date -d -1week +%Y%m%d` 

OLDERTHAN='./bkup_'$weekago'.log'

for fname in ./bkup_*.log; do test "$fname" "<" "$OLDERTHAN" && rm
"$fname"; done

Works a treat.

Removing files older than X days using a date format in the filename

With bash and a regex:

for i in app-*.log*; do
[[ "$i" =~ -([0-9]{4}-[0-9]{2}-[0-9]{2}) ]] \
&& [[ "${BASH_REMATCH[1]}" < "2020-12-20" ]] \
&& echo rm -v "$i"
done

${BASH_REMATCH[1]} contains 2020-12-17, e.g.

As one line:

for i in app-*.log*; do [[ "$i" =~ -([0-9]{4}-[0-9]{2}-[0-9]{2}) ]] && [[ "${BASH_REMATCH[1]}" < "2020-12-20" ]] && echo rm -v "$i"; done

Output:


rm -v app-2020-12-17.log.2
rm -v app-2020-12-18.log.1
rm -v app-2020-12-18.log.2
rm -v app-2020-12-18.log.31
rm -v app-2020-12-18.log.32
rm -v app-2020-12-18.log.33
rm -v app-2020-12-18.log.3.gz

BASH - Delete files older than 3 months?

If you want exact number of days for 3 months then you can use:

days=$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))

and use it as:

find /tmp/*.log -mtime +$days -type f -delete

Or directly in find:

find /tmp/*.log -type f \
-mtime "+$(( ( $(date '+%s') - $(date -d '3 months ago' '+%s') ) / 86400 ))" -delete

Delete files older than the epoch date of a file in a list

Ok, Thank you triplee, I think this will work:

IFS=$'\n'
while read i
do printf "%s " "$i"
stat --format=%Z $i
done < <(find /data/owncloud/*/files -type f) > /root/script/newpurge/filelistwithchangeddate

filetodelete=$(expr `date +'%s'` - 2592000)

awk -v epoch="$filetodelete" '$NF<epoch' /root/script/newpurge/filelistwithchangeddate > oldf
iles

awk '{$NF=""}1' /root/script/newpurge/oldfiles > marktodelete
sed -i "s/^/'/g" /root/script/newpurge/marktodelete
sed -i "s/[ ]\+$/'/g" /root/script/newpurge/marktodelete

for i in $(cat /root/script/newpurge/marktodelete)
do
rm -f $i
done

find and delete file or folder older than x days

You can make use of this piece of code

find /tmp/* -mtime +7 -exec rm {} \;

Explanation

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.

  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +7, it will find files older than 7 days.

  • The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.

Source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

For deleting folders, after emptying inside of them you can rmdirinstad of rm in the piece of code, also if you only want to see directories you can add

-type d

to piece of code such as below:

find /tmp/*/* -mtime +7 -type d -exec rmdir {} \;

Delete all files older than 30 days, based on file name as date

I am by no means a systems administrator, but you could consider a simple shell script along the lines of:

# Generate the date in the proper format
discriminant=$(date -d "30 days ago" "+%Y_%m_%d")

# Find files based on the filename pattern and test against the date.
find . -type f -maxdepth 1 -name "*_*_*.txt" -printf "%P\n" |
while IFS= read -r FILE; do
if [ "${discriminant}" ">" "${FILE%.*}" ]; then
echo "${FILE}";
fi
done

Note that this is will probably be considered a "layman" solution by a professional. Maybe this is handled better by awk, which I am unfortunately not accustomed to using.



Related Topics



Leave a reply



Submit