在Bash中计算目录中的空文件和非空文件
不幸的是,我没有任何代码作为起点,因为我不知道从哪里开始。我需要创建一个名为dircheck的bash脚本,该脚本允许用户使用目录路径(在Bash中计算目录中的空文件和非空文件,bash,Bash,不幸的是,我没有任何代码作为起点,因为我不知道从哪里开始。我需要创建一个名为dircheck的bash脚本,该脚本允许用户使用目录路径(e.g../dircheck~Documents/unit/backup)运行程序,并让脚本向终端输出以下内容: 输入的目录名称有: x个空文件, 包含数据的x个文件, x个空目录, x非空目录 如您所见,我需要输出目录中有多少个文件是空的,有多少个文件有数据,有多少个目录是空的,有多少个目录有数据 任何帮助都将不胜感激 正如评论中所建议的,GNU find可以
e.g../dircheck~Documents/unit/backup
)运行程序,并让脚本向终端输出以下内容:
输入的目录名称有:x个空文件,
包含数据的x个文件,
x个空目录,
x非空目录 如您所见,我需要输出目录中有多少个文件是空的,有多少个文件有数据,有多少个目录是空的,有多少个目录有数据
任何帮助都将不胜感激 正如评论中所建议的,GNU find可以相当容易地做到这一点
#/bin/bash-
echo“$1有:
查找“$1”-第1部分-类型f,d\
\(-empty-o-printf'non-'\)-printf'empty'\
\(-type f-printf'文件'-o-printf'目录'\)\
-printf'\n'| sort | uniq-c
您可以仅通过向find调用添加-maxdepth 1
将分析限制在第一级条目。无论如何,其输出将如下所示:
$。/foo./bionic
/仿生有:
7个空文件
175个非空目录
2163个非空文件
正如评论中所建议的,GNU find可以相当轻松地完成这项工作
#/bin/bash-
echo“$1有:
查找“$1”-第1部分-类型f,d\
\(-empty-o-printf'non-'\)-printf'empty'\
\(-type f-printf'文件'-o-printf'目录'\)\
-printf'\n'| sort | uniq-c
您可以仅通过向find调用添加-maxdepth 1
将分析限制在第一级条目。无论如何,其输出将如下所示:
$。/foo./bionic
/仿生有:
7个空文件
175个非空目录
2163个非空文件
我建议这是另一种使用dircheck.sh
的方法,通过bash函数递归检查给定目录,只使用bash内部语法(shell glob)-而不调用外部命令:
#!/bin/bash
#
# check recursively a directory:
#
function checkDirr() {
for x in $1/*
do
#
# if it's a sub-directory:
#
if [ -d $x ]; then
#echo "$x is a directory."
ndirs=$((ndirs + 1))
#
# glob all entries in an array:
#
entries_array=($x/*)
#
# check entries array count:
#
if [ ${#entries_array[*]} -eq 0 ]; then
#echo "empty dir"
nemptydirs=$((nemptydirs + 1))
else
#
# call recursively myself to check non-empty sub-directory:
#
checkDirr $x
fi
#
# otherwise it's a file:
#
else
#echo "$x is a file."
nfiles=$((nfiles + 1))
#
# check empty file:
#
if [ ! -s $x ]; then
nemptyfiles=$((nemptyfiles + 1))
fi
fi
done
}
#
# main():
#
#
# initialize globals:
#
ndirs=0
nfiles=0
nemptydirs=0
nemptyfiles=0
#
# use given directory path or the current directory .:
#
dir0=${1:-.}
#
# set shell nullglob option to avoid dir/* string when dir is empty:
#
shopt -s nullglob
#
# now check recursively the directory:
#
checkDirr $dir0
#
# unset shell nullglob option:
#
shopt -u nullglob
#
# send statistics:
#
echo $dir0 has:
echo
echo $nemptyfiles empty files
echo $((nfiles - nemptyfiles)) files with data
echo $nemptydirs empty directories
echo $((ndirs - nemptydirs)) non-empty directories
这就给了我们一个例子
$ dircheck.sh so
因此:
2个空文件
87个包含数据的文件
3个空目录
11个非空目录
我建议这是另一种使用
dircheck.sh
的方法,通过bash函数递归检查给定目录,只使用bash内部语法(shell glob)-而不调用外部命令:
#!/bin/bash
#
# check recursively a directory:
#
function checkDirr() {
for x in $1/*
do
#
# if it's a sub-directory:
#
if [ -d $x ]; then
#echo "$x is a directory."
ndirs=$((ndirs + 1))
#
# glob all entries in an array:
#
entries_array=($x/*)
#
# check entries array count:
#
if [ ${#entries_array[*]} -eq 0 ]; then
#echo "empty dir"
nemptydirs=$((nemptydirs + 1))
else
#
# call recursively myself to check non-empty sub-directory:
#
checkDirr $x
fi
#
# otherwise it's a file:
#
else
#echo "$x is a file."
nfiles=$((nfiles + 1))
#
# check empty file:
#
if [ ! -s $x ]; then
nemptyfiles=$((nemptyfiles + 1))
fi
fi
done
}
#
# main():
#
#
# initialize globals:
#
ndirs=0
nfiles=0
nemptydirs=0
nemptyfiles=0
#
# use given directory path or the current directory .:
#
dir0=${1:-.}
#
# set shell nullglob option to avoid dir/* string when dir is empty:
#
shopt -s nullglob
#
# now check recursively the directory:
#
checkDirr $dir0
#
# unset shell nullglob option:
#
shopt -u nullglob
#
# send statistics:
#
echo $dir0 has:
echo
echo $nemptyfiles empty files
echo $((nfiles - nemptyfiles)) files with data
echo $nemptydirs empty directories
echo $((ndirs - nemptydirs)) non-empty directories
这就给了我们一个例子
$ dircheck.sh so
因此:
2个空文件
87个包含数据的文件
3个空目录
11个非空目录
查找以下主题:处理命令行参数(位置参数);在文件上循环(
用于f in*
,用于循环和通配符);变量和算术;测试文件和目录(内置的test
命令)。你会知道下一步该做什么的。你应该查一下表。您应该能够组合-type
、-empty
和-maxdepth
标志来实现所需的功能;在文件上循环(用于f in*
,用于循环和通配符);变量和算术;测试文件和目录(内置的test
命令)。你会知道下一步该做什么的。你应该查一下表。您应该能够组合-type
、-empty
和-maxdepth
标志来实现您想要的功能。您好@oguz ismail感谢您的帮助!有没有不使用find命令的方法呢?不知道,可能有一些相当酷的find
命令。嗨@oguz ismail谢谢你的帮助!有没有不使用find命令的方法呢?不知道,可能有一些相当酷的find
命令。