[英]How can I list the files in a directory that have zero size/length in the Linux terminal?
I am new to using the Linux terminal, so I'm just starting to learn about the commands I can use.我是使用 Linux 终端的新手,所以我才刚刚开始了解我可以使用的命令。 I have figured out how to list the files in a directory using the Linux terminal, and how to list them according to file size.
我已经弄清楚如何使用 Linux 终端列出目录中的文件,以及如何根据文件大小列出它们。 I was wondering if there's a way to list only the files of a specific file size.
我想知道是否有办法只列出特定文件大小的文件。 Right now, I'm trying to list files with zero size, like those that you might create using the
touch
command.现在,我正在尝试列出大小为零的文件,例如您可能使用
touch
命令创建的文件。 I looked through the flags I could use when I use ls, but I couldn't find exactly what I was looking for.我查看了我在使用 ls 时可以使用的标志,但我找不到我正在寻找的确切内容。 Here's what I have right now:
这是我现在拥有的:
ls -lsh /mydirectory
The "mydirectory" part is just a placeholder. “mydirectory”部分只是一个占位符。 Is there anything I can add that will only list files that have zero size?
有什么我可以添加的只会列出大小为零的文件吗?
There's a few ways you can go about this;有几种方法可以 go if you want to stick with
ls -l
you could use eg awk
in a pipeline to do the filtering.如果你想坚持使用
ls -l
你可以在管道中使用例如awk
来进行过滤。
ls -lsh /mydirectory | awk '$5 == 0'
Here, $5
is the fifth field in ls
's output, the size.这里,
$5
是ls
的 output 中的第五个字段,即大小。
Another approach would be to use a different tool, find
.另一种方法是使用不同的工具
find
。
find /mydirectory -maxdepth 1 -size 0 -ls
This will also list hidden files, analogous to an ls -la
.这也将列出隐藏文件,类似于
ls -la
。 The -maxdepth 1
is there so it doesn't traverse the directory tree if you have nested directories. -maxdepth 1
在那里,因此如果您有嵌套目录,它不会遍历目录树。
A simple script can do this.一个简单的脚本可以做到这一点。
for file_name in *
do
if [[ !( -s $file_name ) ]]
then
echo $file_name
fi
done
explanation:解释:
for is a loop. for 是一个循环。 * gives list of all files in a current directory.
* 给出当前目录中所有文件的列表。 -s file_name becomes true if the file has size greater than 0.
-s file_name 如果文件的大小大于 0,则为真。
! ! to negate that
否定那个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.