名前に含まれる日付に基づいてファイルを検索する

名前に含まれる日付に基づいてファイルを検索する

以下のスクリプトでは、ユーザーに日付範囲を入力し、その範囲を適用してコマンドの結果をフィルタリングするように要求しますfind。このコマンドは、名前に日付を含むログファイルに対して機能します。たとえば、filename-YYYYMMDD.gzそのファイルが識別されると、新しいディレクトリにコピーされます。

これまで私が知っているもの(日付範囲など)は-newermt 20190820 ! -newermt 2019082625日または26日にファイルをコピーしません。

修正する:

#!/bin/bash




#Take input from user

read -p "Enter year (YYYY): " Y
read -p "Enter start month: " SM
read -p "Enter start day: " SD
read -p "Enter end month: " EM
read -p "Enter end day: " ED
read -p "Enter copy destination directory (with absolute path): " new_directory

# pad month and day numbers with zero to make the string 2 character long
SD="$(printf '%02d' $SD)"
SM="$(printf '%02d' $SM)"
ED="$(printf '%02d' $ED)"
EM="$(printf '%02d' $EM)"

# Make sure that the new directory exists
#mkdir -p new_directory

# Place the result of your filtered `find` in an array,
# but, before, make sure you set:
#IFS='\n'  # in case some file name stored in the array contains a space

array=(
        $(find /directory/log/ -name "test.file-*.gz" -execdir bash -c '
            filedate="$(basename ${0#./test.file-} .gz)";
            if [[ $filedate -gt $Y$SM$SD ]] && [[ $filedate -lt $Y$EM$ED ]]; then
                basename $0
            fi' {} \;
         )
      )

# loop over array, to copy selected files to destination directory

for i in "${array[@]}"; do
    # ensure that destination directory has full path
    cp "$i" "$new_directory"
done

find -newermtコマンドは、ファイル名ではなく特定の日付に変更されたファイルを見つけることを知っています。より良い方法をご存知でしたら、本当にありがとうございます。

ベストアンサー1

名前に含まれる日付に基づいてファイルを検索する

意味するなら本物 ファイル名の日付をフィルタリングするには、次のようにします。

#!/bin/bash

read -p "Enter year (YYYY): " Y
read -p "Enter start month number: " SM
read -p "Enter start day number: " SD
read -p "Enter end month number: " EM
read -p "Enter end day number: " ED
read -p "Enter copy destination directory (with absolute path): " new_directory

# Do some rule-based checking here. I.e. input variables above
# should conform to expected formats...

# pad month and day numbers with zero to make the string 2 character long
SD="$(printf '%02d' $SD)"
SM="$(printf '%02d' $SM)"
ED="$(printf '%02d' $ED)"
EM="$(printf '%02d' $EM)"

# Make sure that the new directory exists
mkdir -p "$new_directory"

# Place the result of your filtered `find` in an array,
# but, before, make sure you set:
IFS=$'\n'  # in case some file name stored in the array contains a space
sdate="$Y$SM$SD"
edate="$Y$EM$ED"

array=( 
        $(find /directory/log -name "filename-*.gz" -execdir  bash -c '
            filedate="$(basename ${0#./filename-} .gz)";
            if (("${filedate:-0}" >= "${1:-0}")) && 
               (("${filedate:-0}" <= "${2:-0}")); then
                echo "$0"
            fi' {} "$sdate" "$edate" \;) 
      )

# loop over array, to copy selected files to destination directory
#for i in "${array[@]}"; do
#    # ensure that destination directory has full path
#    cp "$i" "$new_directory"
#done

# ... or much cheaper than a loop, if you only need to copy...
cp "${array[@]}" "$new_directory"

おすすめ記事