3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

findで更新日がN日以上過去のファイルを探すには-mtimeオプションを使う

Last updated at Posted at 2017-09-13

findコマンドでファイルの最終更新日時が指定日数以上過去のファイルを探す方法です。

更新日が1日以上過去のファイルを探す

find . -mtime +0

0が1日以上という意味です。

更新日が2日以上過去のファイルを探す

find . -mtime +1

更新日が3日以上過去のファイルを探す

find . -mtime +2

テストコード

次はテストコードです。挙動をお手元のmacOS環境で確認できます。ちなみに、「名前付きブロック記法」で書いています。

main.sh
# !/bin/bash

set -eu

: "テスト用フォルダ作成" && {
	cd ~/Desktop
	mkdir -p test
	cd test
}

: "テスト用ファイル作成" && {
	find . -delete
	for i in {0..3}
	do
		date=$(date "-v-${i}d" +'%Y%m%d%H%M')
		touch -t $date $date.log 
	done
}

: "テスト" && {
	echo 今日の日付 … $(date +'%Y-%m-%d %H:%M')
	echo 更新日が1日以上過去のファイルを探す: && 
		find . -mtime +0
	echo 更新日が2日以上過去のファイルを探す: && 
		find . -mtime +1
	echo 更新日が3日以上過去のファイルを探す: && 
		find . -mtime +2
}
実行結果
今日の日付 … 2017-09-13 15:14
更新日が1日以上過去のファイルを探す:
./201709101514.log
./201709111514.log
./201709121514.log
更新日が2日以上過去のファイルを探す:
./201709101514.log
./201709111514.log
更新日が3日以上過去のファイルを探す:
./201709101514.log
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?