我想编写一个小的 shell 脚本,显示文件树中从给定目录开始的所有文件,这些文件具有给定的用户和大于给定大小的大小。因此,我的脚本将以起始目录、用户名和大小作为参数。这是我目前所拥有的:
#!/bin/bash
owner="valdsilviufarcas"
size=0
function display_owner_and_size()
{
owner=`stat --format "%s" $1`
size=`stat --format "%U" $1`
}
function search()
{
for elem in $1/*
do
display_owner_and_size "$elem"
if [[ $owner == $2 && $size > $3 ]]
then
echo $elem
fi
if test -d "$elem"
then
search "$elem" $2 $3
fi
done
}
search $1 $2 $3
但是,我一直收到此错误:Cannot stat: (bla-bla-bla) : No such file or directory
。为什么 stat 函数不起作用?
答案1
您只需使用以下find
命令:
find ${dir} -user ${user} -type f -size +${size}c -printf "%u\t%s\t%h/%f\n"
将 Bash 变量替换${dir}
为要递归检查的目录、${user}
应仅显示的用户名或 UID 以及${size}
以字节为单位的最小文件大小(不包括!如果使用该值100
,则显示的最小文件可以有 101 个字节)。
此命令将仅打印常规文件,它会忽略输出中的目录和特殊文件(如符号链接或设备)。
答案2
有两个不同的问题。
第一个是你没有引用你的变量:这最终导致stat
失败:
search $1 $2 $3
应该search "$1" "$2" "$3"
;for elem in $1/*
应该for elem in "$1"/*
;owner=`stat --format "%s" $1`
应该owner=`stat --format "%s" "$1"`
;size=`stat --format "%U" $1`
应该size=`stat --format "%U" "$1"`
;search "$elem" $2 $3
应该search "$elem" "$2" "$3"
。
第二种情况是,在递归过程中到达一个空目录时, 的扩展失败,并在不存在的上调用for element in $1/*
两个命令:stat
/path/to/empty/directory/*
% bash script.sh /home/user user 0
stat: cannot stat ‘/home/user/Documenti/Musica/*’: No such file or directory
stat: cannot stat ‘/home/user/Documenti/Musica/*’: No such file or directory
stat: cannot stat ‘/home/user/Documenti/My Games/*’: No such file or directory
stat: cannot stat ‘/home/user/Documenti/My Games/*’: No such file or directory
stat: cannot stat ‘/home/user/Documenti/Video/*’: No such file or directory
stat: cannot stat ‘/home/user/Documenti/Video/*’: No such file or directory
stat: cannot stat ‘/home/user/home/user/*’: No such file or directory
stat: cannot stat ‘/home/user/home/user/*’: No such file or directory
stat: cannot stat ‘/home/user/Modelli/*’: No such file or directory
stat: cannot stat ‘/home/user/Modelli/*’: No such file or directory
stat: cannot stat ‘/home/user/Musica/*’: No such file or directory
stat: cannot stat ‘/home/user/Musica/*’: No such file or directory
stat: cannot stat ‘/home/user/Pubblici/*’: No such file or directory
stat: cannot stat ‘/home/user/Pubblici/*’: No such file or directory
stat: cannot stat ‘/home/user/Video/*’: No such file or directory
stat: cannot stat ‘/home/user/Video/*’: No such file or directory
%
解决这个问题的一种方法是在调用之前检查文件/目录是否存在display_owner_and_size
,这样如果我们试图统计/遍历不存在的目录,就跳过当前迭代(因为目录是空的,所以没有理由获取stat
其内容,也没有理由继续遍历树的那个分支):
[ -e "$elem" ] && display_owner_and_size "$elem" || continue
除此之外,其他错误还有:
owner=`stat --format "%s" "$1"`
應該是owner=`stat --format "%U" "$1"`
而且size=`stat --format "%U" "$1"`
應該是size=`stat --format "%s" "$1"`
;- 不能使用
>
进行数值比较。使用-gt
:if [[ $owner == $2 && $size -gt $3 ]]
。
因此修改后的脚本如下:
#!/bin/bash
owner="valdsilviufarcas"
size=0
function display_owner_and_size()
{
owner=`stat --format "%U" "$1"`
size=`stat --format "%s" "$1"`
}
function search()
{
for elem in "$1"/*
do
[ -e "$elem" ] && display_owner_and_size "$elem" || continue
if [[ $owner == $2 && $size -gt $3 ]]
then
echo $elem
fi
if test -d "$elem"
then
search "$elem" "$2" "$3"
fi
done
}
search "$1" "$2" "$3"