友人から、-rスイッチを使ってディレクトリとサブディレクトリ内のファイルを再帰的に検索できると聞きました。指定されたステートメントのエラーを教えてください。機能しません。
find / -type f -r -name "abc.txt"
答え1
動作しない理由は、findに-r
オプションがないからです。多くのプログラムではフラグが「再帰的」を意味するのは事実ですが-r
、これはすべてに当てはまるわけではなく、 にも当てはまりませんfind
。 の仕事はfind
ファイルとディレクトリを検索することですが、しない再帰的にしたい。
ほとんどのプログラムのオプションは コマンドで確認できますman
。たとえば、 ですman find
。find のマニュアルは膨大なので、オプションを検索してみるとよいでしょう-r
。
$ man find | grep -w -- -r
-- は grep にオプションの読み取りを停止するように指示するだけです。これがない場合は、-r が grep にオプションとして渡されます。また、 を押して/
検索したいものを入力して Enter を押すと、man ページ内で検索できます。
このコマンドは何も返しません。マニュアルを検索する次のコマンドと比較してくださいcp
:
$ man cp | grep -w -- -r
-R, -r, --recursive
は常に再帰的であるためfind
、その逆、つまり、いくつのサブディレクトリに降りるかを選択できるフラグがあります。
-maxdepth levels
Descend at most levels (a non-negative integer) levels of direc‐
tories below the command line arguments. -maxdepth 0
means only apply the tests and actions to the command line
arguments.
-mindepth levels
Do not apply any tests or actions at levels less than levels (a
non-negative integer). -mindepth 1 means process all files
except the command line arguments.
したがって、コマンドについて疑問がある場合は、そのコマンドのman
ページを読んでください。特定のオプションが何を実行するかはわからないからです。たとえば、次のようになります。
$ man sort | grep -w -- -r
-r, --reverse
$ man mount | grep -w -- -r,
-r, --read-only
$ man awk | grep -A 8 -w -- -r
-r
--re-interval
Enable the use of interval expressions in regular expression
matching (see Regular Expressions, below). Interval expressions
were not traditionally available in the AWK language. The POSIX
standard added them, to make awk and egrep consistent with each
other. They are enabled by default, but this option remains for
use with --traditional.
$ man sed | grep -w -- -r
-r, --regexp-extended
$ man xterm | grep -w -- -r
-r This option indicates that reverse video should be simulated by
swapping the foreground and background colors.
分かりました。
答え2
find
すでに再帰的であるため、-r