SSHを使用してUbuntuシステム全体で単語を検索する

SSHを使用してUbuntuシステム全体で単語を検索する

SSHを使用してUbuntuコンピュータ全体のすべてのファイルから特定の単語を検索するにはどうすればよいですか?

メモ:SSHセッションを実行するためにWindowsプログラムPuTTYを使用していますが、一度接続すると通常のbashセッションがあります。

次のようなこの回答UL.SEでは、次のbashコマンドを使用しました。

find / -xdev -type f -print0 | xargs -0 grep -H "search for this text"

ただし、このコマンドはアクセスが拒否されたり一致したりしなくても出力を返します。

私はキャプチャフラグチャレンジを実行しています。 SSHを介してLinuxシステムに接続する必要があり、システム全体のすべてのファイルで「フラグ」文字列を検索したいと思います。 bashとそのコマンドに関する私の知識は非常に限られています。

ベストアンサー1

2> /dev/nullエラーを隠すには、コマンドに追加してください。これをstderrリダイレクトと呼びます。したがって、特定のコマンドの場合は次のようになります。

find / -xdev -type f -print0 | xargs -0 grep -H "search for this text" 2> /dev/null

しかし、これは非常に長いです。また、することができます:

grep -rw "flag"

~からgrep(1):

       -r, --recursive
              Read all files under each directory, recursively, 
              following symbolic links only if they are on the 
              command line. Note that if no file operand is given, 
              grep searches the working directory.  This is 
              equivalent to the -d recurse option.

       -w, --word-regexp
              Select only those lines containing matches that form 
              whole words. The test is that the matching substring 
              must either be at the beginning of the line or preceded 
              by a non-word constituent character.  Similarly, it 
              must be either at the end of the line or followed by a 
              non-word constituent character.  Word-constituent 
              characters are letters, digits, and the underscore. 
              This option has no effect if -x is also specified.

現在のディレクトリのすべてのファイルで「flag」という単語を探します。したがって、マシン全体を検索するには、次のコマンドを実行します/。大文字と小文字を区別したくない場合は、を追加してください-i。これのもう一つの利点は、flagged同様の単語が見つからないことです。

これらすべてのメッセージをgrep -rw "flag" 2> /dev/nullフィルタリングするにはPermission denied

おすすめ記事