Linux 使用 shell 脚本计算文件和目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13727069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count files and directories using shell script
提问by Alan Smith
I'm learning bash scripting and have written a script to count the files and directories in the directory that is supplied as argument. I have it working one way which seems odd to me and am wondering if there is a simpler way of doing it.
我正在学习 bash 脚本并编写了一个脚本来计算作为参数提供的目录中的文件和目录。我让它以一种对我来说似乎很奇怪的方式工作,我想知道是否有更简单的方法。
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=
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
采纳答案by Rubens
You're not iterating over the list of files inside the given directory; add /*
after $LOCATION
. Your script should look like:
您没有遍历给定目录中的文件列表;/*
之后添加$LOCATION
。您的脚本应如下所示:
...
for item in $LOCATION/*
do
...
As pointed by dogbane, just adding /*
will count only files that does not begin with .
; for doing so, you shall do the following:
正如 dogbane 所指出的,仅添加/*
将只计算不以.
;开头的文件。为此,您应执行以下操作:
...
for item in $LOCATION/* $LOCATION/.*
do
...
回答by Anders Johansson
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
; wc -l
will count the number of lines written to stdout in each case.
find
将在;下递归查找所有文件 ( -type f
) 或目录 ( -type d
) 将计算每种情况下写入 stdout 的行数。$LOCATION
wc -l
However if you want to learn, the bash script may be a better way. Some comments:
但是,如果您想学习,bash 脚本可能是更好的方法。一些评论:
- If you want to look for files/directories in
$LOCATION
only (not recursively under their subdirectories etc), you can usefor item in $LOCATION/*
, where the*
will expand to the list of files/directories in the$LOCATION
directory. The missing*
is why your original script returns 0/1 (becasue the$LOCATION
directory itself is the only item counted). - You may want to check first that
$LOCATION
is actually a directory with[ -d $LOCATION ]
. - For arithmetic expressions, use
$(( ... ))
, for exampleFILECOUNT=$(( FILECOUNT + 1 ))
. - If you want to find all files/directories recursively, you could combine
find
with a loop.
- 如果您只想查找文件/目录
$LOCATION
(而不是在它们的子目录下递归等),您可以使用for item in $LOCATION/*
,其中*
将扩展到目录中的文件/目录列表$LOCATION
。缺少的*
是原始脚本返回 0/1 的原因(因为$LOCATION
目录本身是唯一计算的项目)。 - 您可能想先检查它
$LOCATION
实际上是一个带有[ -d $LOCATION ]
. - 对于算术表达式,请使用
$(( ... ))
,例如FILECOUNT=$(( FILECOUNT + 1 ))
。 - 如果要递归查找所有文件/目录,可以结合
find
循环使用。
Example:
例子:
find $LOCATION | while read item; do
# use $item here...
done
回答by sampson-chen
... am wondering if there is a simpler way of doing it.
... 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
回答by beny23
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
用于计算目录
回答by dogbane
Use find
as shown below. This solution will count filenames with spaces, newlines and dotfiles correctly.
find
如下图使用。此解决方案将正确计算带有空格、换行符和点文件的文件名。
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 (.
). If you do not want this, subtract 1.
请注意,DIRCOUNT
包括当前目录 ( .
)。如果您不想要这个,请减去 1。
((DIRCOUNT--)) # to exclude the current directory