简体   繁体   English

如何列出 Linux 终端中大小/长度为零的目录中的文件?

[英]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.这里, $5ls的 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.

相关问题 如何在Linux终端中复制带空格的文件列表 - How can I copy a list of files with spaces in Linux terminal 如何在Linux中列出目录中的文件和文件夹及其总大小? - How to list the files and folders in a directory with its total size in Linux? 如何从终端中的任何目录运行linux可执行文件? - How can i run a linux executable from any directory in terminal? 如何在/ proc中显示文件的大小? 它不应该是零大小 - how can i show the size of files in /proc? it should not be size zero 在Linux终端中,如何删除目录中除一个或两个以外的所有文件 - In Linux terminal, how to delete all files in a directory except one or two Linux:如何列出有关文件或目录的信息(大小,权限,按类型划分的文件数?) - Linux:How to list the information about file or directory(size,permission,number of files by type?) in total 如何将文件从 linux 目录复制到另一个给定长度参数 - How to copy files from a linux directory to another given a length parameter 如何列出Linux目录中的所有文件,其中包含root用户和其他用户使用完整路径使用的基本命令和shell? - How can I list all files in a Linux directory that holds basic commands and shells used by root and other users using a full path? 如何将文件从谷歌云计算引擎(linux 终端)复制到谷歌云 shell? - How can I copy files from a Google Cloud Compute engine(linux terminal) to google cloud shell? 如何使用linux cmd列出linux中所有目录中的所有文件 - How to list all files in all directory in linux using linux cmd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM