0
0

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 3 years have passed since last update.

findコマンド

Posted at

古い日付のファイルを探して消したい

※「シス環系女子 season2 第10~11話」の備忘録です。

findコマンド

  • -ctime

以下は「/backup/daily」ディレクトリ配下の30日以上前のファイル一覧を表示するコマンド

find /backup/daily -ctime +30

※-ctimeは「+」で指定日より昔を表、「-」で指定日より現在寄の時系列を表す。

以上より、30日以上前のファイルを削除するシェルスクリプトを書くとすると以下のようになる。

# !/bin/bash

remove_files = $(find /backup/daily -ctime +30)
for file in $remove_files
  rm "$file"
done

またrmコマンド(削除コマンド)を伴う処理を実行する場合は、以下のようにパイプラインでlessにつなげて、一度確認してみた方が良い。

$ find /backup/daily -ctime +30 | less
  • -and

以下は、1ヶ月前から2ヶ月前までの間のファイルで、ファイル名に「report」か「error」を含む文字列。

$ find /logs/ -ctime +30 -and -ctime -60 -and \( -name "*report*" -or -name "*error*" \)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?