I'm trying to delete files older than 2 days and write the deleted file names to a log file along with creation/modified time and deletion time.
Eg: filename creationTime deletetionTime
test1 11-08-2020T08:38:16+05:30 12-08-2020T10:18:42+05:30
test2 11-08-2020T08:38:16+05:30 12-08-2020T10:18:42+05:30
I have script to delete files older than 2 days and log the filename to file but not getting how to print along with creation/modified date and deletion time.
#!/bin/bash
path="/test"
filename=*.txt
logfile=/tmp/$(date +%d-%m-%Y).log
days=-60
find $path -mmin $days -name "*.wav" -type f -print0 -delete | xargs -0 basename -a >> $logfile
Can someone help me to achieve that above output?
Once you have deleted a file, there is no longer a possibility to
stat
it foratime
/mtime
information.As well, the
-printf
action in GNUfind
has a%f
specifier for printing the file's basename, so there's really no need for the externalxargs
/basename
combo.So you could get the basename and modification time in a
-printf
likeYou could then execute a
date
command just before (or just after) the-delete
to get the deletion time:(remove the
#
to proceed with the deletions only once you are happy that it is doing what you want).For other date/time format options see the relevant man pages - I chose
%T+
for the GNU long form modification time infind
, then chose date format+%F+%H:%M:%S.%N
to mimic that.