How to find and delete date based files in linux shell script without searching?

PLEASE NOTE THAT I CAN'T USE "FIND" IN THE TARGET ENVIRONMENT

I need to delete all files older than 7 days in linux shell script. Something like:

FILES=./path/to/dir for f in $FILES do echo "Processing $f file..." # take action on each file. $f store current file name # perhaps stat each file to get the last modified date and then delete files with date older than today -7 days. done 

Is it possible to use 'stat' for this? I tried to use

 find *.gz -mtime +7 -delete 

but found that I cannot use find on the target system (there is no permission for the cron user, and this cannot be changed). The target system is Redhat Enterprise.

File names are formatted as follows:

gzip> / mnt / target03 / rest-of-path / web / backups / DATABASENAME_ date "+%Y-%m-%d" .gz

+6
source share
4 answers

Since you have time in the file name, use it in time to remove this code that does this:

This script gets the current time in seconds from the era, and then calculates the timestamp 7 days ago. Then, for each file, the file name is analyzed and the date embedded in each file name is converted to a time stamp, then the time stamp is compared to determine which files to delete. Using timestamps gets rid of all the troubles when working with dates directly (leap year, different days in months, etc.)

The actual deletion is commented out so you can verify the code.

 #funciton to get timestamp X days prior to input timestamp # arg1 = number of days past input timestamp # arg2 = timestamp ( eg 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 # eg 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 
+3
source

This should work:

 #!/bin/sh DIR="/path/to/your/files" now=$(date +%s) DAYS=30 for file in "$DIR/"* do if [ $(((`stat $file -c '%Y'`) + (86400 * $DAYS))) -lt $now ] then # process / rm / whatever the file... fi done 

A little clarification: stat <file> -c '%Z' gives the file modification time as seconds since the UNIX era for the file, and $(date +%s) gives the current UNIX timestamp. Then it’s simple enough to check if the timestamp of the file is plus seven days in seconds than the current timestamp.

+8
source

If you prefer to rely on a date in the file names, you can use this procedure, which checks if the date is older than another:

 is_older(){ local dtcmp=`date -d "$1" +%Y%m%d`; shift local today=`date -d "$*" +%Y%m%d` return `test $((today - dtcmp)) -gt 0` } 

and then you can scroll through the file names bypassing β€œ-7 days” as the second date:

 for filename in *; do dt_file=`echo $filename | grep -o -E '[12][0-9]{3}(-[0-9]{2}){2}'` if is_older "$dt_file" -7 days; then # rm $filename or whatever fi done 

In the is_older procedure, date -d "-7 days" +%Y%m%d will return the date 7 days before, in a numerical format, ready for comparison.

+2
source
 DIR='' now=$(date +%s) for file in "$DIR/"* do echo $(($(stat "$file" -c '%Z') + $((86400 * 7)))) echo "----------" echo $now 

to do

0
source

Source: https://habr.com/ru/post/904258/


All Articles