特定のファイルを検索する

LinuxBeginner
オンラインで実践に進む

はじめに

このプロジェクトでは、/etc ディレクトリ内のコンテンツに "labex" という文字列を含むファイルを検索し、特定の形式で出力を整理する方法を学びます。

👀 プレビュー

/etc/group
/etc/gshadow
/etc/passwd
/etc/security/limits.conf
/etc/shadow
/etc/subgid
/etc/subuid
/etc/supervisor/conf.d/vnc.conf
/etc/supervisor/conf.d/webide.conf
/etc/supervisor/conf.d/ttyd.conf
/etc/supervisor/supervisord.conf
/etc/shiyanlou/sbin/init.sh
/etc/shiyanlou/services/ttyd.conf
/etc/shiyanlou/services/vncserver.conf
/etc/shiyanlou/services/webide.conf
/etc/group-
/etc/gshadow-
/etc/passwd-
/etc/shadow-
/etc/sudoers.d/labex
/etc/labex_source/init.sh
/etc/labex_source/novnc/dist/main.bundle.js
/etc/labex_source/supervisord.conf
/etc/labex_source/tmux.conf
/etc/labex_source/ttyd.conf
/etc/labex_source/vnc.conf
/etc/labex_source/webide.conf

🎯 タスク

このプロジェクトでは、以下のことを学びます。

  • grep コマンドを使用して、ファイル内の特定の文字列を検索する方法
  • 検索結果の重複エントリを削除する方法
  • 出力をクリーンで読みやすい形式に整理する方法

🏆 成果

このプロジェクトを完了すると、以下のことができるようになります。

  • コンテンツに基づいて効率的にファイルを検索する
  • 検索結果を操作して整理する
  • これらのスキルをさまざまなファイル検索と整理タスクに適用する

labex を含むファイルを検索する

このステップでは、/etc ディレクトリ内のコンテンツに "labex" という文字列を含むすべてのファイルを検索し、各ファイルの完全パスを出力する方法を学びます。

  1. ターミナルを開き、/home/labex/project ディレクトリに移動します。
cd /home/labex/project
  1. grep コマンドを使用して、/etc ディレクトリ内のすべてのファイルに対して文字列 "labex" を検索し、各一致ファイルの完全パスを出力します。
sudo grep -rsl "labex" /etc > output

解説:

  • sudo: /etc ディレクトリにアクセスするために必要な、スーパーユーザー (root) 特権でコマンドを実行します。
  • grep: テキストファイル内のパターンを検索するために使用するコマンド。
  • -r: 指定されたパスのすべてのサブディレクトリを再帰的に検索します。
  • -s: エラーメッセージを抑制し、ファイル名のみを出力します。
  • -l: 一致する行ではなく、ファイル名のみを出力します。
  • "labex": ファイルコンテンツ内で検索する文字列。
  • /etc: 検索対象のディレクトリ。
  • > output: 出力を現在のディレクトリ内の output ファイルにリダイレクトします。
  1. output ファイルの内容を確認します。
cat /home/labex/project/output

これにより、/etc ディレクトリ内のコンテンツに "labex" という文字列を含むすべてのファイルの完全パスが表示されます。

/etc/group
/etc/gshadow
/etc/passwd
/etc/security/limits.conf
/etc/shadow
/etc/subgid
/etc/subuid
/etc/supervisor/conf.d/vnc.conf
/etc/supervisor/conf.d/webide.conf
/etc/supervisor/conf.d/ttyd.conf
/etc/supervisor/supervisord.conf
/etc/shiyanlou/sbin/init.sh
/etc/shiyanlou/services/ttyd.conf
/etc/shiyanlou/services/vncserver.conf
/etc/shiyanlou/services/webide.conf
/etc/group-
/etc/gshadow-
/etc/passwd-
/etc/shadow-
/etc/sudoers.d/labex
/etc/labex_source/init.sh
/etc/labex_source/novnc/dist/main.bundle.js
/etc/labex_source/supervisord.conf
/etc/labex_source/tmux.conf
/etc/labex_source/ttyd.conf
/etc/labex_source/vnc.conf
/etc/labex_source/webide.conf

出力を検証する

この最後のステップでは、output ファイルが正しく作成され、予想されるファイルパスが含まれていることを検証します。

  1. /home/labex/project/output ファイルを開き、その内容を確認します。以下のことを確認してください。

    • 各行は一意のファイルパスを表しています。
    • すべてのファイルパスは /etc ディレクトリ内にあり、そのコンテンツに "labex" という文字列を含んでいます。
  2. 出力が正しいように見える場合は、プロジェクトを成功裏に完了したことになります!

おめでとうございます。コンテンツに基づいて特定のファイルを検索し、望ましい形式で出力を整理する方法を学びました。

まとめ

おめでとうございます!このプロジェクトを完了しました。実力を向上させるために、LabEx でさらに多くの実験を行って練習してください。

✨ 解答を確認して練習✨ 解答を確認して練習