所以我有一個包含檔案和子目錄的目錄。我想遞回地獲取所有檔案,然后以長格式列出它們,按修改日期排序。這是我想出的。
find . -type f | xargs -d "\n" | ls -lt
但是,這僅列出當前目錄中的檔案,而不列出子目錄中的檔案。鑒于以下列印出所有檔案,我不明白為什么。
find . -type f | xargs -d "\n" | cat
任何幫助表示贊賞。
uj5u.com熱心網友回復:
xargs只有ls當它ls 作為引數傳遞時才能開始。當您通過管道從xargsinto 開始時ls,只有一個副本ls被啟動 - 由父外殼程式 - 并且它沒有提供任何檔案名find | xargs作為引數 - 而是它們在其標準輸入上,但從ls不讀取其標準輸入,所以它甚至不知道他們在那里。
因此,您需要洗掉|字符:
# Does what you specified in the common case, but buggy; don't use this
# (filenames can contain newlines!)
# ...also, xargs -d is GNU-only
find . -type f | xargs -d '\n' ls -lt
...或更好:
# uses NUL separators, which cannot exist inside filenames
# also, while a non-POSIX extension, this is supported in both GNU and BSD xargs
find . -type f -print0 | xargs -0 ls -lt
……或者,甚至比這更好:
# no need for xargs at all here; find -exec can do the same thing
# -exec ... {} is POSIX-mandated functionality since 2008
find . -type f -exec ls -lt {}
此答案中的大部分內容也包含在Using Find的Actions、Complex Actions和Actions in Bulk部分中,非常值得一讀。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/357968.html
