Bash: Delete Based on File Date Stamp

Bash: delete based on file date stamp

I think the following should do what you want:

touch -t 201007010000 dummyfile
find /path/to/files -type f ! -newer dummyfile -delete

The first line creates a file which was last modified on the 1st July 2010. The second line finds all files in /path/to/file which has a date not newer than the dummyfile, and then deletes them.

If you want to double check it is working correctly, then drop the -delete argument and it should just list the files which would be deleted.

Delete the files in current folder in the Linux based on the timestamp

You can try to do the above but extract the paths to the files as well. The below will delete all your files that are found by the find command through time stamp provided

      rm -rf `find -maxdepth 1 -type f -exec ls -l {} + | grep '00:00' | awk '{ print $9 }'`

How can I find and delete files based on date in a linux shell script without find?

Since you have time in the filename then use that to time the deletion heres some code that does that :

This script gets the current time in seconds since epoch and then calculates the timestamp 7 days ago. Then for each file parses the filename and converts the date embeded in each filename to a timestamp then compares timestamps to determine which files to delete. Using timestamps gets rid of all hassles with working with dates directly (leap year, different days in months, etc )

The actual remove is commented out so you can test the code.

#funciton to get timestamp X days prior to input timestamp
# arg1 = number of days past input timestamp
# arg2 = timestamp ( e.g. 1324505111 ) seconds past epoch
getTimestampDaysInPast () {
daysinpast=$1
seconds=$2
while [ $daysinpast -gt 0 ] ; do
daysinpast=`expr $daysinpast - 1`
seconds=`expr $seconds - 86400`
done
# make midnight
mod=`expr $seconds % 86400`
seconds=`expr $seconds - $mod`
echo $seconds
}
# get current time in seconds since epoch
getCurrentTime() {
echo `date +"%s"`
}

# parse format and convert time to timestamp
# e.g. 2011-12-23 -> 1324505111
# arg1 = filename with date string in format %Y-%m-%d
getFileTimestamp () {
filename=$1
date=`echo $filename | sed "s/[^0-9\-]*\([0-9\-]*\).*/\1/g"`
ts=`date -d $date | date +"%s"`
echo $ts
}

########################### MAIN ############################
# Expect directory where files are to be deleted to be first
# arg on commandline. If not provided then use current working
# directory

FILEDIR=`pwd`
if [ $# -gt 0 ] ; then
FILEDIR=$1
fi
cd $FILEDIR

now=`getCurrentTime`
mustBeBefore=`getTimestampDaysInPast 7 $now`
SAVEIFS=$IFS
# need this to loop around spaces with filenames
IFS=$(echo -en "\n\b")
# for safety change this glob to something more restrictive
for f in * ; do
filetime=`getFileTimestamp $f`
echo "$filetime lt $mustBeBefore"
if [ $filetime -lt $mustBeBefore ] ; then
# uncomment this when you have tested this on your system
echo "rm -f $f"
fi
done
# only need this if you are going to be doing something else
IFS=$SAVEIFS

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.

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


Related Topics



Leave a reply



Submit