Note # 1
What you actually see in the output of ls -l
is the file's last modification date and not the creation date.
find
has pattern matching(globbing) like -name "*jetty.log"
and it has -mtime
for days modified ... So, for example:
find . -mtime +0 -type f -name "*jetty.log"
To find matching files modified more than 24 hours ago ... 1 for 48 hours and so on.
And it has -mmin
for minutes modified ... So, for example:
find . -mmin +10 -type f -name "*jetty.log"
To find matching files modified more than 10 minutes ago.
And it has the -delete
action to delete matched files ... So, for example:
find . -mtime +9 -type f -name "*jetty.log" -delete
To find matching files modified more than 10 days ago and delete them.
You can restrict the search to the current folder's first level only by adding -maxdepth 1
to prevent matching files in sub-directories.
Note # 2
I noticed that your filenames begin with a date pattern e.g. 2923_05_10...
... If that is consistent and you want to match by those dates, then you can use pattern matching like, for example:
find . -maxdepth 1 -type f -name "2023_05_1[0-4].jetty.log"
To match files with 2023_05_10
to 2023_05_14
in their names.
As these date patterns in the filenames can be easily parsed into actual dates, you can also do that in bash
like, for example:
#!/bin/bash
days=5 # Set the number of days from now for a date to be considered as recent
for f in *.jetty.log
do
f1="${f%%.*}"
f1="${f1//_/-}"
fd="$(date -d "$f1" '+%s')"
td="$(date -d "- $days days" '+%s')"
[ -f "$f" ] && [ "$fd" -lt "$td" ] && echo rm -- "$f"
done
To match and print files with dates in their names older than 5 days ... If the output is what you want, then remove echo
and re run it again to delete those files.
Note # 3
If what you mean is actually the creation/birth date of the file, then you can do that in bash
with for example:
#!/bin/bash
days=5 # Set the number of days from now for a date to be considered recent
for f in *.jetty.log
do
cd="$(stat -c '%w' "$f")"
fcd="$(date -d "$cd" '+%s')"
td="$(date -d "- $days days" '+%s')"
[ -f "$f" ] && [ "$fcd" -lt "$td" ] && echo rm -- "$f"
done
To match and print files with creation date older than 5 days ... If the output is what you want, then remove echo
and re run it again to delete those files.
This one, however, requires a recent kernel as the support for stat
accessing date of birth was absent in older kernels ... Please see this answer for more explanation about this.
Note # 4
Simplify your life with bash
functions to complement missing system-tools functionalities ... For example, this bash
function:
function rm_old {
lfcd=0
for f in *
do
if [ -f "$f" ]
then
fcd="$(stat -c '%w' "$f")"
fcd="$(date -d "$fcd" '+%s')"
if [ "$fcd" -gt "$lfcd" ]
then
todelete+=("${tokeep[@]}")
tokeep=("$f")
lfcd="$fcd"
elif [ "$fcd" -eq "$lfcd" ]
then
tokeep+=("$f")
else
todelete+=("$f")
fi
fi
done
echo "These files will be DELETED:"
printf '\e[4;31m%s\e[0m\n' "${todelete[@]}"
echo "This/These file(s) will be KEPT:"
printf '\e[4;32m%s\e[0m\n' "${tokeep[@]}"
read -p "Confirm Y(Yes)/N(No)" answer
case "$answer" in
Yes|yes|YES|Y|y)
[ "${#todelete[@]}" -gt 0 ] && rm -- "${todelete[@]}" || echo "Exiting ..."
;;
*)
echo "Aborting ..."
;;
esac
unset answer
unset todelete
unset tokeep
unset fcd
unset lfcd
}
Will keep only the last created file/files in the current working directory and delete the rest.