简体   繁体   English

以bash列出大于100K的文件

[英]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 . 我正在尝试编写自己的脚本,因此不应使用命令findls 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. 另外,我不能使用findls因为我必须逐个显示文件,询问用户是否要查看下一个文件。 This is possible using the command find or ls or only can be done writing my own function? 这可以使用命令findls来完成,或者只能通过编写我自己的函数来完成?

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 (或awksed等)来仅保留文件大小。

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.

相关问题 如何使用grep命令来显示大于100k且小于140k的条目在文件的最后一个文件中 - How to use grep command to diplay entries that are greater than 100k and less than 140k In the last feild of a file 如何通过ftp连接获取比Linux命令行(bash)中指定的文本字母数字大的文件列表? - How to get list of files alphanumerically greater than text specified in Linux command line (bash) via ftp connection? Linux如何在文件名中列出大于特定时间戳的文件? - Linux How to List Files greater than particular timestamp in file name? 如何在Linux上将RaspberryPi的I2C时钟配置为100K - How to configure the RaspberryPi's I2C clock to 100K on Linux Bash - 查找超过3行的文件列表 - Bash — find a list of files with more than 3 lines 在bash中处理二进制数据文件,查找大于某个数字的元素 - Processing binary data files in bash, finding elements which are greater than some number bash shell字段需要大于# - bash shell field needs to be greater than # 获取创建日期大于某个日期 linux 的文件列表 - Get list of files whose creation date is greater than some date linux 如何使用sed查找大于100的替换数字? - how to find the replace the digit which is greater than 100 using sed? 多个 -a 大于/小于 break bash 脚本 - Multiple -a with greater than / less than break bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM