[英]Bash Scripting - Display number of files and folders in a directory
我是bash腳本的新手,我在stackoverflow上找到了此示例 。 現在,看來這是我需要的,但是我不確定一些事情。
我如何正確修改它以顯示下載目錄中的文件和文件夾數量
“ $ @”是什么意思?
到目前為止,我的代碼:
cleanPath="/home/someuser/Downloads/*"
if [ -d $cleanPath]; then
find "$cleanPath" -type f | ls -l $cleanPath | wc -l | echo "Number of files is $@"
find "$cleanPath" -type d | ls -l $cleanPath | wc -l | echo "Number of directorieis $@"
fi
您快到這里了,但是您會遇到一些語法錯誤(if語句括號內需要空格)和其他一些錯誤。 如果由於外殼擴展*而沒有正確引用cleanPath之類的"$cleanPath"
則cleanPath="/home/someuser/Downloads/*"
可能會引起問題,因此您實際上會獲得Downloads中所有文件和目錄的列表(嘗試echo $cleanPath
,您將看到)。 另外,我不明白為什么將find的輸出傳遞給ls,ls甚至不會使用輸入,它只會列出所有文件和目錄。
嘗試這個:
cleanPath="/home/someuser/Downloads"
if [ -d "$cleanPath" ]; then
echo "No of files is ""$(find "$cleanPath" -mindepth 1 -type f | wc -l)"
echo "No of directories is ""$(find "$cleanPath" -mindepth 1 -type d | wc -l)"
fi
請注意,這是遞歸的-默認為find
行為。 您沒有明確說明這是否是您想要的。 對於非遞歸列表:
cleanPath="/home/someuser/Downloads"
if [ -d "$cleanPath" ]; then
echo "No of files is ""$(find "$cleanPath" -mindepth 1 -maxdepth 1 -type f | wc -l)"
echo "No of directories is ""$(find "$cleanPath" -mindepth 1 -maxdepth 1 -type d | wc -l)"
fi
您也可以使用$@
作為傳遞給腳本的所有位置參數的數組表示形式。
https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#index-_0024_0040
注意:-mindepth -maxdepth不符合POSIX,並且不適用於帶有換行符的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.