2014-01-16 67 views
1

這是一個相對簡單的命令,所以如果存在重複並且有人可以引用我,我很抱歉,我會刪除/關閉此問題。殼牌符號:查找。 -type f -exec file'{}';

Man頁面,找到

find . -type f -exec file '{}' \; 

    Runs 'file' on every file in or below the current directory. Notice that the braces are enclosed in single quote marks to protect them from interpretation 
    as shell script punctuation. The semicolon is similarly protected by the use of a backslash, though ';' could have been used in that case also. 

我不明白的符號\;。那是什麼?

回答

5

在find命令中,動作-exec後跟一個命令和該命令的參數。因爲可以有任何數量的參數,所以需要一些方法來知道它何時結束。分號告訴find它已經到達命令參數的末尾。

對於自己的設備,大多數貝殼都會吃分號。我們希望將該分號傳遞給find命令。因此,我們用反斜槓來避開它。這告訴shell將分號當作find命令的參數之一。

更多:爲什麼不這樣做呢,有人可能會問,假設exec命令的參數只是行尾?爲什麼我們需要結束exec命令的參數呢?原因是find具有高級功能。舉個例子,考慮:

find . -name '*.pdf' -exec echo Yes, we have a pdf: {} \; -o -exec echo No, not a pdf: {} \; 
+0

爲什麼'find'需要知道它何時結束? -exec命令的參數是一個有限的列表,所以它不知道何時結束? – imagineerThat

+1

@imagineerThis,好問題。查看更新的答案。 – John1024