Linuxで先月修正されたファイルを見つける方法は?

Linuxで先月修正されたファイルを見つける方法は?

あるディレクトリで先月に変更されたファイルを検索して別のディレクトリにコピーする必要があります。

/usr/bin/find $SOURCE_DIR -maxdepth 1 -type f -name 'files*.pdf' -mtime -31 -exec cp -p {} $DEST_DIR`date +"%B_%Y"` \;

先月に変更されたファイルが必要です。例:1月、2月に変更されたファイル...提案がありますか?

ベストアンサー1

@SmileDeveloperは正しい方向に向かっていますが、非常にトリッキーであることが判明しました。

$ cd "$(mktemp --directory)"
$ touch --date="$(date --date="$(date --rfc-3339=date | cut --characters=-7)-01 - 1 day" --rfc-3339=second)" last-day-of-last-month
$ touch --date="$(date --date="$(date --rfc-3339=date | cut --characters=-7)-01 - 1 second" --rfc-3339=second)" end-of-last-month
$ touch --date="$(date --rfc-3339=date | cut --characters=-7)-01" start-of-current-month
$ touch --date="$(date --date="$(date --rfc-3339=date | cut --characters=-7)-01 + 1 month - 1 second" --rfc-3339=second)" end-of-current-month
$ touch --date="$(date --date="$(date --rfc-3339=date | cut --characters=-7)-01 + 1 month" --rfc-3339=second)" start-of-next-month
$ stat --printf '%y\t%n\n' ./* | sort --key=1
2020-06-30 00:00:00.000000000 +1200 ./last-day-of-last-month
2020-06-30 23:59:59.000000000 +1200 ./end-of-last-month
2020-07-01 00:00:00.000000000 +1200 ./start-of-current-month
2020-07-31 23:59:59.000000000 +1200 ./end-of-current-month
2020-08-01 00:00:00.000000000 +1200 ./start-of-next-month
$ find . -mindepth 1 -newermt "$(date --rfc-3339=date | cut --characters=-7)-01 - 1 second" -not -newermt "$(date --rfc-3339=date | cut --characters=-7)-01 + 1 month - 1 second"
./end-of-current-month
./start-of-current-month

デフォルトでは、lastコマンドは前月の最後の秒(除外)から今月の最後の秒(含む)までの範囲を提供します。findほとんどのプログラミング言語とは異なり、包括的な開始日時と排他的な終了日時を選択する方法がないようです。

時間帯について心配する必要はないことを願っています!

おすすめ記事