Shell 计算所提供文件夹中的文件和目录总数,包括子目录及其文件

Shell 计算所提供文件夹中的文件和目录总数,包括子目录及其文件,shell,sh,Shell,Sh,我想计算所提供文件夹中的所有文件和目录,包括子目录中的文件和目录。我已经写了一个脚本,它将准确计算文件和目录的数量,但它不处理子目录有什么想法??? 我想在不使用查找命令的情况下执行此操作 #!/bin/bash givendir=$1 cd "$givendir" || exit file=0 directories=0 for d in *; do if [ -d "$d" ]; then directories=$((directories+1)) else fi

我想计算所提供文件夹中的所有文件和目录,包括子目录中的文件和目录。我已经写了一个脚本,它将准确计算文件和目录的数量,但它不处理子目录有什么想法??? 我想在不使用查找命令的情况下执行此操作

#!/bin/bash

givendir=$1
cd "$givendir" || exit

file=0
directories=0

for d in *;
do
 if [ -d "$d" ]; then
    directories=$((directories+1))
 else
    file=$((file+1))
 fi
done

echo "Number of directories :" $directories
echo "Number of file Files :" $file
使用查找:

echo "Number of directories:            $(find "$1" -type d | wc -l)"
echo "Number of files/symlinks/sockets: $(find "$1" ! -type d | wc -l)"
使用纯shell和递归:

#!/bin/bash                                                                                               

countdir() {                                                                                              
  cd "$1"                                                                                                 
  dirs=1                                                                                                  
  files=0                                                                                                 

  for f in *                                                                                              
  do                                                                                                      
    if [[ -d $f ]]                                                                                        
    then                                                                                                  
      read subdirs subfiles <<< "$(countdir "$f")"                                                        
      (( dirs += subdirs, files += subfiles ))                                                            
    else                                                                                                  
      (( files++ ))                                                                                       
    fi                                                                                                    
  done                                                                                                    
  echo "$dirs $files"                                                                                     
}                                                                                                         

shopt -s dotglob nullglob                                                                                 
read dirs files <<< "$(countdir "$1")"                                                                    
echo "There are $dirs dirs and $files files"    
#/bin/bash
countdir(){
cd“$1”
dirs=1
文件=0
f英寸*
做
如果[-d$f]]
然后
读取子文件
find“$1”-type f | wc-l
将为您提供文件,
find“$1”-type d | wc-l
将为您提供目录

我又快又脏的脚本会读

#!/bin/bash

test -d  "$1" || exit
files=0

# Start with 1 to count the starting dir (as find does), else with 0
directories=1

function docount () {
    for d in $1/*; do
        if [ -d "$d" ]; then
                directories=$((directories+1))
            docount "$d";
        else
                files=$((files+1))
        fi
    done
}

docount "$1"
echo "Number of directories :" $directories
echo "Number of file Files :" $files
但请注意:在我的项目构建文件夹中,存在一些差异:

  • 查找:6430目录,74377非目录
  • 我的脚本:6032目录,71564非目录
  • @那个家伙的剧本:6794目录,76862非目录

我认为这与大量链接、隐藏文件等有关,但我懒得去调查:
find
是首选工具。

以下是一些不使用find的单行命令:

目录数量:
ls-Rl./|grep:$“| wc-l

文件数:
ls-Rl./|grep“[0-9]:[0-9]”|wc-l

说明:
ls-Rl
递归列出所有文件和目录,每行一个

grep:$”
只查找最后一个字符为“:”的结果。这些都是目录名

grep“[0-9]:[0-9]”
与时间戳的HH:MM部分匹配。时间戳仅显示在文件上,而不显示在目录上。如果时间戳格式不同,则需要选择不同的grep


wc-l
计算与grep匹配的行数。

哦,还有一件事我不想使用find命令就可以做到。@latude8848:这将是一种什么样的学习?使用正确的工具完成正确的工作是学习IMO的最佳策略。我是这一部分的新手。我已经使用find命令解决了这个问题,而且它是it’这还不够挑战。如果我想详细理解一种编程语言,那么我必须找到解决同样问题的新方法。很棒的脚本工作正常。在我的计算机上运行时,输出类似于find命令。谢谢你的帮助