简体   繁体   English

Bash计算目录,子目录和文件

[英]Bash counting directories, subdirectories and files

I have had a look at all the other answers on stack overflow and have spent all day on this. 我查看了堆栈溢出时的所有其他答案,并且整日都花在了上面。

The first thing I am trying to do is count all the directories/subdirectories in a the specified directory, which ideally should be pretty simple. 我要做的第一件事是计算指定目录中的所有目录/子目录,理想情况下应该非常简单。 To begin with I ask the user to enter the directory name. 首先,我要求用户输入目录名称。

#Choosing directory
echo "Please type in directory cd: [directory name]"
read dirname
cd $dirname
echo "Entering directory" 

This is working fine. 一切正常。 Next there are two methods I have tried for finding the number of folders. 接下来,我尝试了两种方法来查找文件夹数。

The directory I am testing has 6 directories/subdirectories in total within it and 3 files. 我正在测试的目录中总共有6个目录/子目录和3个文件。

The first method is using 'find' and 'wc' on its own, however this outputs a string " 7" with spaces in front, I do not want to count the directory that we have selected. 第一种方法是单独使用'find'和'wc',但是这会输出一个字符串“ 7”,前面带有空格,我不想计算我们选择的目录。 I also can't subtract one from this value if I set it as a variable as it is a string with spaces at the front. 如果将其设置为变量,我也无法从该值中减去一个,因为它是一个在前面带有空格的字符串。

direnum=`find . -type d | wc -l`
echo "$direnum"

(result = 7)(require result = 6) (结果= 7)(需要结果= 6)

The second method is using a 'for loop' along with 'find' and 'wc'. 第二种方法是使用“ for循环”以及“ find”和“ wc”。 This method works fine, but is extremely slow when there is many files. 此方法可以正常工作,但是在有许多文件时非常慢。

i=0
for d in `find . -type d` 
do
   i=`expr $i + 1`
done

#dont include the directory currently in
i=`expr $i - 1`

#output results
echo "Directories: $i"

(result = 6)(require result = 6) (结果= 6)(需要结果= 6)

I also tried both methods with the files, however it does not output the correct number. 我也用文件尝试了这两种方法,但是它没有输出正确的数字。 I have 3 files within the directory, the first method outputs 5 and the second method outputs the result 8 not sure how this works. 我在目录中有3个文件,第一种方法输出5 ,第二种方法输出结果8,不确定如何工作。

First method for file count 文件计数的第一种方法

#Check number of files in directory
filenum=`find . -type f | wc -l`
echo "$filenum"

Again results in a string " 5" with lots of spaces in front. 再次导致字符串“ 5”,前面有很多空格。

(result = 5)(require result = 3) (结果= 5)(需要结果= 3)

Second method for file count 文件计数的第二种方法

#Check number of files in directory
j=0
for f in `find . -type f` 
do
    j=`expr $j + 1`
done

(result = 8)(require result = 3) (结果= 8)(要求结果= 3)

If someone could put me on the right track it would be appreciated, I am not expecting a full solution, ideally I would like to figure it out on my own but I am probably doing something wrong. 如果有人可以让我走上正确的道路,将不胜感激,我不希望有完整的解决方案,理想情况下,我想独自解决问题,但我可能做错了事。

使用最小深度

find . -mindepth 1 -type d | wc -l

Further to my comment: 除了我的评论:

declare -i direnum
direnum=$(find . -type d|wc -l)
(( direnum-- ))
echo "$direnum"

By the way, notice that I am using $( ... ) rather than back-ticks, Back-ticks are considered bad practice and obsolete. 顺便说一句,请注意,我使用的是$( ... )而不是反引号,反引号被认为是不好的做法且已过时。

The command "tree" counts files and folder from a relative position 命令“ tree”从相对位置计数文件和文件夹

tree /path

will give you the information. 将为您提供信息。 Do you need to use that info in other scripts? 您是否需要在其他脚本中使用该信息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM