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?

Day7:findコマンド(ファイル検索の極意)

0
Posted at

findコマンド(ファイル検索の極意)

🎯 今日のゴール

  • findの基本構文を理解する
  • 名前・サイズ・更新日で検索できる
  • -exec の意味を理解する

📌 findとは?

ディレクトリ階層を再帰的に検索するコマンド。

🔹 基本構文


find 検索開始ディレクトリ 条件

例:


find /home -name file.txt

→ /home配下から file.txt を検索


📌 ① 名前で検索(-name)


find . -name "*.txt"

⚠ ワイルドカードは "" で囲む


📌 ② サイズで検索(-size)


find . -size +10M

→ 10MBより大きいファイル

記号 意味
+ より大きい
- より小さい

📌 ③ 更新日で検索(-mtime)


find . -mtime -7

→ 7日以内に更新


📌 ④ 種類指定(-type)

オプション 意味
-type f ファイル
-type d ディレクトリ

例:


find . -type d


📌 ⑤ -exec(超重要)

検索結果に対してコマンド実行。


find . -name "*.log" -exec rm {} ;

→ .logファイルを削除

🔹 {} の意味

検索結果のファイル名が入る

🔹 ; の意味

コマンド終了

⚠ 試験頻出ポイント


💻 今日の実践


find . -name "*.txt"
find /etc -type f
find . -size +1M


🧠 試験で狙われるポイント

  • findは再帰検索
  • ワイルドカードは "" 必須
  • -type の意味
  • -exec {} ; の構文

🧪 Day7 確認テスト

【選択問題】

Q1

現在ディレクトリから再帰的に検索するコマンドは?

A. grep
B. find
C. search
D. locate


Q2

ファイルのみを検索するオプションは?

A. -f
B. -file
C. -type f
D. -name f


Q3

検索結果に対してコマンドを実行するオプションは?

A. -run
B. -exec
C. -cmd
D. -do


【記述問題】

Q4

「find . -name "*.log" -exec rm {} ;」の意味を説明せよ。



✅ 解答

Q1:B
Q2:C
Q3:B

Q4例:
現在ディレクトリ以下から.logファイルを検索し、
見つかったファイルをrmで削除する。


🔥 明日のテーマ

パーミッション(chmod / chown)

🔥 今日の本質

Linuxは「探せる人」が強い。

find + grep が自然に書けるようになれば、
初級は卒業レベルです。

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?