Bash 用户是否可以指定脚本将搜索多少文件夹级别?
我正在构建一个小的bash脚本项目。我的代码有两个参数,一个搜索路径和一个数字,告诉我需要分析多少文件夹级别。 例如,如果用户给数字2 my programm,它必须搜索其中的所有目录以及其中的所有目录Bash 用户是否可以指定脚本将搜索多少文件夹级别?,bash,find,awk,sort,Bash,Find,Awk,Sort,我正在构建一个小的bash脚本项目。我的代码有两个参数,一个搜索路径和一个数字,告诉我需要分析多少文件夹级别。 例如,如果用户给数字2 my programm,它必须搜索其中的所有目录以及其中的所有目录 #!/bin/bash function check_dir { echo Checking dir : $1 for f in `ls $1` do if [ -d $1/$f ] then dirs_num=$
#!/bin/bash
function check_dir {
echo Checking dir : $1
for f in `ls $1`
do
if [ -d $1/$f ]
then
dirs_num=$(($dirs_num+1))
check_dir $1/$f
else
files_num=$(($files_num+1))
size=`stat -c%s $1/$f`
echo $1/$f - $size
fi
done
}
files_num=0
dirs_num=0
depth=$2
check_dir $1
echo "Found $files_num files and $dirs_num dirs."
这是到目前为止我的代码,我在执行时给出了路径,但它给了我找到的任何文件夹的结果。那么我如何停止这个循环呢?
谢谢。使用
引用手册页
用一点GNU和,你可以用一种安全的方式做你想做的事情
#!/bin/bash
awk '
BEGIN{ RS="\0" } # Non-portable, requires GNU awk
$1 != dir{
print "Checking dir : " $1
dcnt++
dir = $1
}
{
print $1$2 " - " $3
fcnt++
}
END{
print "Found " fcnt " files and " dcnt " dirs."
}' <(find "$1" -maxdepth "$2" -type f -printf "%h\t%f\t%s\0" | LC_ALL=C sort -z )
#/bin/bash
awk'
BEGIN{RS=“\0”}#不可移植,需要GNU awk
$1 != 迪尔{
打印“检查目录:$1”
dcnt++
dir=$1
}
{
打印$1$2“-”$3
fcnt++
}
结束{
打印找到的“fcnt”文件和“dcnt”目录
}“谢谢,我会检查它@∑ωτηρηςⅧωννιΔηης检查答案,以找到一种更安全的方法来做你想做的事
#!/bin/bash
awk '
BEGIN{ RS="\0" } # Non-portable, requires GNU awk
$1 != dir{
print "Checking dir : " $1
dcnt++
dir = $1
}
{
print $1$2 " - " $3
fcnt++
}
END{
print "Found " fcnt " files and " dcnt " dirs."
}' <(find "$1" -maxdepth "$2" -type f -printf "%h\t%f\t%s\0" | LC_ALL=C sort -z )