[英]UNIX: Use a single find command to search files larger than 4 MiB, then pipe the output to a sort command
我目前有一個問題,我想在下面回答。 以下是我想出的,但似乎不起作用:
find /usr/bin -type f -size +4194304c | sort -n
我在上面的正確軌道上嗎?
問題:使用單個 find 命令在 /usr/bin 中搜索所有大於 4 MiB 的文件,並以長格式打印列表。 將此輸出傳送到排序命令,該命令將從最大到最小對列表進行排序
我會擺弄 for -printf 命令行開關,像這樣:
find YOUR_CONDITION_HERE -printf '%s %p\\n' | sort -n
find YOUR_CONDITION_HERE -printf '%s %p\\n' | sort -n
: %s 代表字節大小,%p 代表文件名。 您可以稍后修剪尺寸,例如使用cut
,例如:
find -type f -size +4194304c -printf '%s %p\\n' | sort -n | cut -f 2 -d ' '
find -type f -size +4194304c -printf '%s %p\\n' | sort -n | cut -f 2 -d ' '
但是考慮到您需要長列表格式,我猜您會在 printf 的參數中添加更多字段。
相關主題: https : //superuser.com/questions/294161/unix-linux-find-and-sort-by-date-modified
你是在正確的軌道上,但find
命令只會輸出文件的名稱,而不是它的大小。 這就是sort
會按字母順序對它們進行排序的原因。
要按大小排序,您可以輸出文件列表,然后使用xargs
將其傳遞給ls
,如下所示:
find /usr/bin -type f -size +4194304c | xargs ls -S
如果您希望ls
在單個列上輸出文件列表,則可以將-S
替換為-S1
。 該命令將變為:
find /usr/bin -type f -size +4194304c | xargs ls -S1
為了使您的命令對所有文件名具有抵抗力,我建議使用-print0
(它將用空字符分隔路徑,這是唯一一個不能出現在 Linux 文件名中的字符)。 該命令將變為:
find /usr/bin -type f -size +4194304c -print0 | xargs -0 ls -S1
你也可以試試
find /usr/bin -type f -size +4194304c -ls | sort -n -k7
如果你想反轉結果然后嘗試
find /usr/bin -type f -size +4194304c -ls | sort -r -n -k7
或者另一種選擇
find /usr/bin -type f -size +4194304c -exec ls -lSd {} +
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.