在文本文件中打印日期



我正试图编写一个脚本,如果目录的最新文件超过3天,它就会打印出目录的名称。需要注意的是,这是在一个文本文件中完成的,该文件的内容包括命令cat directoryName > file.txt的输出。file.txt的输出示例,不包括目录2和3:

drwxr-xr-x 2 root root 4096 Aug  3 00:00 Directory1
drwxr-xr-x 2 root root 4096 Aug 15 00:00 Directory2
drwxr-xr-x 2 root root 4096 Aug 15 00:00 Directory3
/root/Downloads/syslog/Directory1:
total 12
-rw-r--r-- 1 root root 18 Aug 20 00:00 file1
-rw-r--r-- 1 root root  9 Jun 23 00:00 file2
-rw-r--r-- 1 root root  9 Dec  8  2022 file3

从终端提取最新文件并使用-mtime进行过滤很简单,但如何在文本文件中做到这一点?

请检查它是否能帮助您:

#!/bin/bash
######################################################################
# Script Configuration
######################################################################
readonly INPUT_FILENAME=file.txt
readonly NUMBER_OF_DAYS=3
######################################################################
# Global variables
######################################################################
g_current_directory=""
g_current_time_in_seconds=$(date "+%s")
g_number_of_days_in_seconds=$(( ${NUMBER_OF_DAYS} * 24 * 60 * 60 ))
g_max_age_in_seconds=$(( ${g_current_time_in_seconds} - ${g_number_of_days_in_seconds} ))
######################################################################
# Checks the record type
# Arguments:
#  l_record - A single record from the file to be evaluated
# Returns:
#  0 - The record is a directory information record
#  1 - The record is a directory name
#  2 - The record is a file information record
#  3 - Error detecting the record type
######################################################################
function check_record_type() {
local l_record="$1"
local l_record_type=3
if [[ "${l_record}" =~ ^d......... .* ]]; then
l_record_type=0
elif [[ "${l_record}" =~ ^/.*: ]]; then
l_record_type=1
elif [[ "${l_record}" =~ ^-......... .* ]]; then
l_record_type=2
fi
return ${l_record_type}
}
##### Start processing ###############################################
# Read the file line by line (record by record)
while read record ; do
# Gets the current record type
check_record_type "${record}"
record_type=$?
# We just evaluate the types 1 (directory name) and 2 (file information record)
case ${record_type} in
1) if [[ ! -z "${g_current_directory}" ]] &&
[[ ${recent_file_exists_in_dir} == false ]]; then
echo "${g_current_directory}"
fi
g_current_directory=$( echo "${record}" | tr -d ':' )
recent_file_exists_in_dir=false
;;
2) filename=$( echo "${record}" | awk '{print $9}' )
file_date=$( echo "${record}" | awk '{print $6 " " $7 " " $8}' )
file_date_in_seconds=$( date -d"${file_date}" +"%s" )
# Check if the file is between the boundaries of
# n days ago and the current time. The comparison is evaluated
# in seconds.
if [[ ${file_date_in_seconds} -gt ${g_max_age_in_seconds} ]] &&
[[ ${file_date_in_seconds} -lt ${g_current_time_in_seconds} ]]; then             
# Yes, we have recent files,
# Set the flag to exclude this directory from the listing
recent_file_exists_in_dir=true
fi
;;
*) # Skip records that are different from types 1 and 2.
esac
done < ${INPUT_FILENAME}
if [[ ! -z "${g_current_directory}" ]] &&
[[ ${recent_file_exists_in_dir} == false ]]; then
echo "${g_current_directory}"
fi

最新更新