[英]List files greater than 100K in bash
I want to list the files recursively in the HOME
directory. 我想在
HOME
目录中递归列出文件。 I'm trying to write my own script , so I should not use the command find
or ls
. 我正在尝试编写自己的脚本,因此不应使用命令
find
或ls
。 My script is: 我的脚本是:
#!/bin/bash
minSize=102400;
printFiles() {
for x in "$1/"*; do
if [ -d "$x" ]; then
printFiles "$x";
else
size=$(wc -c "$x");
if [[ "$size" -gt "$minSize" ]]; then
echo "$size";
fi
fi
done
}
printFiles "/~";
So, the problem here is that when I run this script, the terminal throws Line 11: division by 0
and /home/gandalf/Videos/*: No such file or directory
. 因此,这里的问题是,当我运行此脚本时,终端抛出
Line 11: division by 0
和/home/gandalf/Videos/*: No such file or directory
。 I have not divided by any number, why I'm getting this error?. 我没有除以任何数字,为什么会出现此错误? And the second one?
还有第二个?
Alternatively, I can't use find
or ls
because I have to display the files one by one asking to the user if he want to see the next file or not. 另外,我不能使用
find
或ls
因为我必须逐个显示文件,询问用户是否要查看下一个文件。 This is possible using the command find
or ls
or only can be done writing my own function? 这可以使用命令
find
或ls
来完成,或者只能通过编写我自己的函数来完成?
Thanks. 谢谢。
size=$(wc -c "$x");
That's the line that is failing. 那是失败的线。 When you run that
wc
command manually you should be able to see why: 当您手动运行该
wc
命令时,您应该能够看到原因:
$ wc -c /tmp/out
5 /tmp/out
The output contains not only the file size but also the file name. 输出不仅包含文件大小,还包含文件名。 So you can't use
$size
with the -gt
comparator on the next line. 因此,不能在下一行将
$size
与-gt
比较器一起使用。 One way to fix that is to change the wc
line to use cut
(or awk
, or sed
, etc) to keep just the file size. 解决该问题的一种方法是更改
wc
行以使用cut
(或awk
或sed
等)来仅保留文件大小。
size=$(wc -c "$x" | cut -f1 -d " ")
A simpler alternative suggested by @mklement0: @ mklement0建议的一种更简单的选择:
size=$(wc -c < "$x")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.