[英]Count number of files in directory
I don't know why this command count also directories as files: 我不知道为什么此命令还将目录也视为文件:
nb_fichier_src=($(find "$file" -type f | wc -l))
In $file
I have 51 files and 1 folder, but the output of my instruction is 52!!! 在
$file
我有51个文件和1个文件夹,但指令的输出为52! I want to count only the number of files in my folder. 我只想计算文件夹中的文件数。 Any ideas of what is going wrong?
有什么问题的想法吗?
To take care of files with spaces and new lines in filenames better to use find
command like this: 为了更好地处理文件名中带有空格和换行符的文件,最好使用以下
find
命令:
nb_fichier_src=$(find . -maxdepth 1 -type f -exec echo . \; | wc -l)
Take note of -exec echo .
注意
-exec echo .
which prints a single dot for each file found in current directory. 它为当前目录中找到的每个文件打印一个点。
You could test
over a glob loop of the directory. 您可以
test
目录的全局循环。
num_files=0
for file in directory/*; do
[ -f "$file" ] && ((num_files++))
done
You should use: 您应该使用:
ls -l | grep -v ^d | wc -l
Note: 注意:
为了避免递归搜索,请使用maxdepth选项:
nb_fichier_src=($(find "$file" -maxdepth 1 -type f | wc -l))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.