Linuxでfindコマンドを使用して2つの条件を実行する方法

Linuxでfindコマンドを使用して2つの条件を実行する方法

5日が経過したファイル(ログファイル)を削除しようとしていますが、削除せずに特定のファイルだけをアーカイブする必要がありますが、次のようにしてみました。

find . -type f -mtime +5 ! -name 'test2' -exec rm -f {} \;

しかし、これは成功しませんでした。私が使用するオペレーティングシステムはCentos 7です。

ベストアンサー1

ご覧のとおり、テストのために次の構造を作成しました。

mkdir test && cd test && touch file1 file2 file3 lol test2
touch -t 200805101024 file*

これを得る:

ls -la
total 8
drwxr-xr-x 2 root   root   4096 Oct 13 18:09 .
drwxr-xr-x 4 phphil phphil 4096 Oct 13 18:09 ..
-rw-r--r-- 1 root   root      0 May 10  2008 file1
-rw-r--r-- 1 root   root      0 May 10  2008 file2
-rw-r--r-- 1 root   root      0 May 10  2008 file3
-rw-r--r-- 1 root   root      0 Oct 13 18:09 lol
-rw-r--r-- 1 root   root      0 Oct 13 18:09 test2

正しいコマンドを実行した後:

find . -type f -mtime +5 ! -name 'test2' -exec rm -f {} \;

状況は予想通りです。

ls -la
total 8
drwxr-xr-x 2 root   root   4096 Oct 13 18:09 .
drwxr-xr-x 4 phphil phphil 4096 Oct 13 18:09 ..
-rw-r--r-- 1 root   root      0 Oct 13 18:09 lol
-rw-r--r-- 1 root   root      0 Oct 13 18:09 test2

編集:コマンドまたはコマンドパラメータに関する情報を見つけます。

コマンドおよび/またはパラメータの動作がわからない場合は、次の手順に従います。

man find | less

その後、次のように目的のコマンドのマニュアルを検索できます。

  • タイプ/
  • 私が思う検索語を書いてください-mtime
  • 次に、を入力して次の検索用語に移動するNか、を入力して前の検索用語に移動できます。n

私は次の興味深い情報を見つけました。

   -mtime n
          File's data was last modified n*24 hours ago.  See the comments for -atime to understand how  round‐
          ing affects the interpretation of file modification times.

おすすめ記事