bash 查找目录中不是目录本身的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1300620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:25:29  来源:igfitidea点击:

Find all files in a directory that are not directories themselves

bashunixksh

提问by Alex

I am looking for a way to list all the files in a directory excluding directories themselves, and the files in those sub-directories.

我正在寻找一种方法来列出目录中的所有文件,不包括目录本身,以及这些子目录中的文件。

So if I have:

所以如果我有:

./test.log
./test2.log
./directory
./directory/file2

I want a command that returns: ./test.log ./test2.log and nothing else.

我想要一个返回的命令:./test.log ./test2.log 而没有别的。

回答by John Kugelman

If you want test.log, test2.log, and file2then:

如果你想要test.log, test2.log,file2然后:

find . -type f

If you do not want file2then:

如果你不想,file2那么:

find . -maxdepth 1 -type f

回答by anton_rh

If you need symlinks, pipes, device files and other specific elements of file system to be listed too, you should use:

如果您还需要列出符号链接、管道、设备文件和文件系统的其他特定元素,您应该使用:

find -maxdepth 1 -not -type d

This will list everything except directories.

这将列出除目录之外的所有内容。

回答by dfa

using find is simple as:

使用 find 很简单:

find . -maxdepth 1 -type f

回答by Eric M

find /some/directory -type f

回答by Rob Jones

$ find . -type f -print

Each file will be on its own line. You must be in the directory you want to search.

每个文件都在它自己的行上。您必须在要搜索的目录中。

回答by amrox

find . -type f

回答by Sachin Chourasiya

One more option

多一个选择

ls -ltr | grep ^d