Linux 如何限制递归文件列表的深度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4509624/
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-08-04 00:01:29  来源:igfitidea点击:

How to limit depth for recursive file list?

linuxbash

提问by Jon

Is there a way to limit the depth of a recursive file listing in linux?

有没有办法限制 linux 中递归文件列表的深度?

The command I'm using at the moment is:

我目前使用的命令是:

ls -laR > dirlist.txt

But I've got about 200 directories and each of them have 10's of directories. So it's just going to take far too long and hog too many system resources.

但是我有大约 200 个目录,每个目录都有 10 个目录。所以它只会花费太长时间并占用太多系统资源。

All I'm really interested in is the ownership and permissions information for the first level subdirectories:

我真正感兴趣的是第一级子目录的所有权和权限信息:

drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain1.co.uk  
drwxr--r-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain1.co.uk/htdocs  
drwxr--r-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain1.co.uk/cgi-bin  
drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain2.co.uk  
drwxr-xrwx 14 proftp root  1234 Dec 22 13:19 /var/www/vhosts/domain2.co.uk/htdocs  
drwxr-xrwx 14 proftp root  1234 Dec 22 13:19 /var/www/vhosts/domain2.co.uk/cgi-bin  
drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain3.co.uk  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain3.co.uk/htdocs  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain3.co.uk/cgi-bin  
drwxr-xr-x 14 root   root  1234 Dec 22 13:19 /var/www/vhosts/domain4.co.uk  
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain4.co.uk/htdocs
drwxr-xr-- 14 jon    root  1234 Dec 22 13:19 /var/www/vhosts/domain4.co.uk/cgi-bin

EDIT:

编辑:

Final choice of command:

命令的最终选择:

find -maxdepth 2 -type d -ls >dirlist

采纳答案by Alberto Zaccagni

Checkout the -maxdepthflag of find

签出-maxdepth国旗find

find . -maxdepth 1 -type d -exec ls -ld "{}" \;

Here I used 1 as max level depth, -type dmeans find only directories, which then ls -ldlists contents of, in long format.

在这里,我使用 1 作为最大级别深度,-type d表示仅查找目录,然后ls -ld以长格式列出其内容。

回答by Sameer

tree -L 2 -u -g -p -d

tree -L 2 -u -g -p -d

Prints the directory tree in a pretty format up to depth 2 (-L 2). Print user (-u) and group (-g) and permissions (-p). Print only directories (-d). tree has a lot of other useful options.

以深度为 2 (-L 2) 的漂亮格式打印目录树。打印用户 (-u) 和组 (-g) 以及权限 (-p)。仅打印目录 (-d)。tree 还有很多其他有用的选项。

回答by Volker Siegel

Make use of find's options

使用find的选项

There is actually no exec of /bin/lsneeded;

实际上不需要 exec /bin/ls

Find has an option that does just that:

Find 有一个选项可以做到这一点:

find . -maxdepth 2 -type d -ls

To see only the one level of subdirectories you are interested in, add -mindepthto the same level as -maxdepth:

要仅查看您感兴趣的一级子目录,请添加-mindepth到与以下相同的级别-maxdepth

find . -mindepth 2 -maxdepth 2 -type d -ls



Use output formatting

使用输出格式

When the details that get shown should be different, -printfcan show any detail about a file in custom format; To show the symbolic permissions and the owner name of the file, use -printfwith %Mand %uin the format.

当显示的详细信息应该不同时,-printf可以以自定义格式显示有关文件的任何详细信息;为了显示符号权限和文件的所有者名称,使用-printf%M%uformat

I noticed later you want the full ownership information, which includes the group. Use %gin the format for the symbolic name, or %Gfor the group id (like also %Ufor numeric user id)

我后来注意到您需要完整的所有权信息,其中包括组。%g以符号名称或%G组 ID的格式使用(也可%U用于数字用户 ID)

find . -mindepth 2 -maxdepth 2 -type d -printf '%M %u %g %p\n'

This should give you just the details you need, for just the right files.

这应该只为您提供所需的详细信息,以及正确的文件。

I will give an example that shows actually different values for user and group:

我将给出一个示例,显示用户和组的实际不同值:

$ sudo find /tmp -mindepth 2 -maxdepth 2 -type d -printf '%M %u %g %p\n'
drwx------ www-data  www-data /tmp/user/33
drwx------ octopussy root     /tmp/user/126
drwx------ root      root     /tmp/user/0
drwx------ siegel    root     /tmp/user/1000
drwxrwxrwt root      root     /tmp/systemd-[...].service-HRUQmm/tmp

(Edited for readability: indented, shortened last line)

(为了可读性进行了编辑:缩进,缩短了最后一行)



Notes on performance

性能注意事项

Although the execution time is mostly irrelevant for this kind of command, increase in performance is large enough here to make it worth pointing it out:

尽管执行时间与此类命令几乎无关,但这里的性能提升足够大,值得指出:

Not only do we save creating a new process for each name - a hugetask - the information does not even need to be read, as findalready knows it.

我们不仅节省了为每个名称创建一个新进程——一项艰巨的任务——甚至不需要读取信息,因为find它已经知道了。

回答by recolic

All I'm really interested in is the ownership and permissions information for the first level subdirectories.

我真正感兴趣的是第一级子目录的所有权和权限信息。

I found a easy solution while playing my fish, which fits your need perfectly.

我在玩我的鱼时找到了一个简单的解决方案,它非常适合您的需求。

ll `ls`

or

或者

ls -l $(ls)