计算提供的文件夹(包括子目录及其文件)中的文件和目录总数

时间:2014-01-23 22:27:48

标签: shell sh

我想计算所提供文件夹中的所有文件和目录,包括子目录中的文件和目录。我写了一个脚本,它将准确计算文件和目录的数量,但它不处理子目录的任何想法??? 我想这样做而不使用 FIND 命令

#!/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

3 个答案:

答案 0 :(得分:1)

使用find:

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"    

答案 1 :(得分:0)

find "$1" -type f | wc -l将为您提供文件find "$1" -type d | wc -l目录

我的快速和肮脏的shellcript将会读取

#!/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 dirs,74377 non-dirs
  • 我的剧本:6032 dirs,71564 non-dirs
  • @ thatotherguy的剧本:6794 dirs,76862 non-dirs

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

答案 2 :(得分:0)

以下是一些不带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匹配的行数。