简体   繁体   English

使用shell脚本计算文件和目录

[英]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: 一些评论:

  • If you want to look for files/directories in $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目录本身是唯一计算的项目)。
  • You may want to check first that $LOCATION is actually a directory with [ -d $LOCATION ] . 您可能想先检查$LOCATION实际上是[ -d $LOCATION ]的目录。
  • For arithmetic expressions, use $(( ... )) , for example FILECOUNT=$(( FILECOUNT + 1 )) . 对于算术表达式,请使用$(( ... )) ,例如FILECOUNT=$(( FILECOUNT + 1 ))
  • If you want to find all files/directories recursively, you could combine 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.

相关问题 使用 shell 脚本在目录之间移动文件 - Move files between directories using shell script 使用shell脚本查找文件并将其移动到适当的目录 - Find and move files to appropriate directories using shell script 如何使用 linux 中的 bash shell 脚本递归地重命名文件中的所有目录、文件和文本 - How to rename all directories , files and text in the files recursively using bash shell script in linux count使用shell脚本无法移动的文件数 - count number of files failed to move using shell script 如何使用shell脚本创建多个目录 - How to create multiple directories using shell script Shell 脚本来计算文件,然后删除最旧的文件 - Shell script to count files, then remove oldest files 如何使用Linux Shell脚本读取文本文件列并将文件复制到子目录中的另一个路径 - How to read the column of text file using Linux shell script and copy the files to another path with in sub directories 终端无法运行存储在PATH目录中的Shell脚本文件 - Terminal cannot run shell script files stored in PATH directories 使用Shell脚本下载文件 - Download files using Shell script 如何使用根目录中存在的所有目录下特定文件夹中的Shell脚本列出所有文件? - How to list all the files using shell script present in a specific folder under all the directories those are present in the root directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM