linux下查找指令

linux下查找指令

我的朋友告訴我,可以使用 -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。此外,您可以在手冊頁中進行搜索,方法是點擊/然後寫下您要搜索的內容,然後輸入。

該命令什麼也不返回,將其與搜尋以下手冊的命令進行比較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

相關內容