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
How to limit depth for recursive file list?
提问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 -maxdepth
flag of find
签出-maxdepth
国旗find
find . -maxdepth 1 -type d -exec ls -ld "{}" \;
Here I used 1 as max level depth, -type d
means find only directories, which then ls -ld
lists 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/ls
needed;
实际上不需要 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 -mindepth
to 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, -printf
can show any detail about a file in custom format;
To show the symbolic permissions and the owner name of the file, use -printf
with %M
and %u
in the format
.
当显示的详细信息应该不同时,-printf
可以以自定义格式显示有关文件的任何详细信息;为了显示符号权限和文件的所有者名称,使用-printf
与%M
和%u
的format
。
I noticed later you want the full ownership information, which includes
the group. Use %g
in the format for the symbolic name, or %G
for the group id (like also %U
for 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 find
already 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)