![](/img/trans.png)
[英](unix) How to write script to count the number of files in a directory using loop
[英]Count number of files using unix
我們如何在大型機環境中使用unix計算服務器目錄中存在的文件數。 我已經嘗試了以下命令,但是由於出現底部錯誤而中止:
cd“ / Deepak / dir”
ls -1 | wc -l
ls -l . | egrep -c '^-'
ls -cf
嘗試第一個命令后,我收到的錯誤消息是:不能ls:“ / Deepak / dir / |” 未找到。
我不知道為什么在這里顯示管道。 而且我認為這就是原因,錯誤是指出找不到目錄為“ / Deepak / dir / |” 和“ / Deepak / dir /”不同。
怎么樣:
ls -1 | wc -l
這似乎為我工作。
ls | wc -l也可以正常工作
如果您只想在目錄中不計文件而不計(子)目錄,請使用以下命令代替ls -1
find yourdirname -maxdepth 1 -type f | wc -l
-maxdepth
限制僅find
到yourdirname,並且-type f
僅在搜索時才考慮文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.