[英]Count files and directories using shell script
I'm learning bash scripting and have written a script to count the files and directories in the directory that is supplied as argument. 我正在学习bash脚本,并编写了一个脚本来计算作为参数提供的目录中的文件和目录。 I have it working one way which seems odd to me and am wondering if there is a simpler way of doing it.
我让它工作的方式对我来说很奇怪,我想知道是否有更简单的方法。
I have commented out the code that will work, but left it in as a comparison. 我已经注释掉了可行的代码,但将其作为比较。 I am trying to get the
for
-loop working, instead using if
statements inside it to detect if an item in the given location is a file or a directory. 我试图让
for
-loop工作,而不是在其中使用if
语句来检测给定位置中的项是文件还是目录。
Edit : I just found out that the commented code counts all files and directories in the subdirectories of the given location as well! 编辑 :我刚刚发现注释的代码也计算了给定位置的子目录中的所有文件和目录! Is there any way to prevent this and just count the files and directories of the given location?
有没有办法防止这种情况,只计算给定位置的文件和目录?
#!/bin/bash
LOCATION=$1
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ]
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
#DIRS=$(find $LOCATION -type d)
#FILES=$(find $LOCATION -type f)
#for d in $DIRS
#do
# DIRCOUNT=$[$DIRCOUNT+1]
#done
#for f in $FILES
#do
# FILECOUNT=$[$FILECOUNT+1]
#done
for item in $LOCATION
do
if [ -f "$item" ]
then
FILECOUNT=$[$FILECOUNT+1]
elif [ -d "$item" ]
then
DIRCOUNT=$[$DIRCOUNT+1]
fi
done
echo "File count: " $FILECOUNT
echo "Directory count: " $DIRCOUNT
For some reason the output of the for
-loop, no matter where I point the location to, always returns: 由于某种原因,
for
-loop的输出,无论我指向哪个位置,总是返回:
File count: 0 , Directory count: 1
Use find
as shown below. 使用
find
如下所示。 This solution will count filenames with spaces, newlines and dotfiles correctly. 此解决方案将正确计算带有空格,换行符和dotfiles的文件名。
FILECOUNT="$(find . -type f -maxdepth 1 -printf x | wc -c)"
DIRCOUNT="$(find . -type d -maxdepth 1 -printf x | wc -c)"
Note that the DIRCOUNT
includes the current directory ( .
). 请注意,
DIRCOUNT
包括当前目录( .
)。 If you do not want this, subtract 1. 如果你不想要这个,减去1。
((DIRCOUNT--)) # to exclude the current directory
To just solve the problem you can use: 要解决问题,您可以使用:
FILECOUNT=$(find $LOCATION -type f | wc -l)
DIRCOUNT=$(find $LOCATION -type d | wc -l)
find
will look for all files ( -type f
) or directories ( -type d
) recursively under $LOCATION
; find
将在$LOCATION
下递归查找所有文件( -type f
)或目录( -type d
); wc -l
will count the number of lines written to stdout in each case. wc -l
将计算在每种情况下写入stdout的行数。
However if you want to learn, the bash script may be a better way. 但是,如果您想学习,bash脚本可能是更好的方法。 Some comments:
一些评论:
$LOCATION
only (not recursively under their subdirectories etc), you can use for item in $LOCATION/*
, where the *
will expand to the list of files/directories in the $LOCATION
directory. $LOCATION
查找文件/目录(不能在子目录下递归等),你可以for item in $LOCATION/*
使用for item in $LOCATION/*
,其中*
将扩展到$LOCATION
目录中的文件/目录列表。 The missing *
is why your original script returns 0/1 (becasue the $LOCATION
directory itself is the only item counted). *
是原始脚本返回0/1的原因(因为$LOCATION
目录本身是唯一计算的项目)。 $LOCATION
is actually a directory with [ -d $LOCATION ]
. $LOCATION
实际上是[ -d $LOCATION ]
的目录。 $(( ... ))
, for example FILECOUNT=$(( FILECOUNT + 1 ))
. $(( ... ))
,例如FILECOUNT=$(( FILECOUNT + 1 ))
。 find
with a loop. find
与循环结合使用。 Example: 例:
find $LOCATION | while read item; do
# use $item here...
done
You're not iterating over the list of files inside the given directory; 您没有迭代给定目录中的文件列表; add
/*
after $LOCATION
. $LOCATION
后添加/*
。 Your script should look like: 您的脚本应如下所示:
...
for item in $LOCATION/*
do
...
As pointed by dogbane, just adding /*
will count only files that does not begin with .
正如dogbane指出的那样,只需添加
/*
将只计算不以的文件.
; ; for doing so, you shall do the following:
为此,您应该执行以下操作:
...
for item in $LOCATION/* $LOCATION/.*
do
...
... am wondering if there is a simpler way of doing it.
If you say so ;) 如果你这样说;)
Alternatively, reduce your script to 或者,将脚本缩小为
find /path/to/directory | wc -l
For current directory, do: 对于当前目录,请执行:
find . | wc -l
Use 使用
find $LOCATION -maxdepth 1 -type f | wc -l
For the count of files, and 对于文件的数量,和
find $LOCATION -maxdepth 1 -type d | wc -l
For counting directories 用于计算目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.