
我有一個find
命令傳遞xargs
到ls -d
.當沒有文件匹配時,我得到:
.
作為我的唯一輸出,因為預設ls -d
顯示:
.
...,當沒有指定(檔案名稱)參數時。有沒有辦法在不傳遞任何參數時ls -d
省略.
,並且不顯示任何輸出?
更新:你只是無法說服ls -d
不顯示.
:
.
ls -d -A\ # Skip implied
--hide=.\ # No, really, skip implied
--ignore=. # I mean it, damn it!
.
:)
答案1
xargs
與 一起使用find
太 1980 年代了...
來電例如:
find . -type d -exec ls -d {} +
並且 ls 指令永遠不會在沒有參數的情況下被呼叫。
find
David Korn 於 1989 年為 SVr4新增了 execplus 功能。
答案2
您標記了作業系統,所以你可能運行 GNU xargs,它有這個選項:
-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run the command.
對於find ... | xargs -r ls -d
,如果不匹配任何內容,則ls
根本不會啟動。find
find
可能仍然.
明確匹配。為了避免這種情況,請新增! -name .
到尋找表達式中。或使用-mindepth 1
(在 GNU find 中)來防止find
匹配命令列上給出的任何路徑。
(至於如何讓ls
自己忽略.
,我不知道。)