【发布时间】:2023-04-02 01:20:01
【问题描述】:
我正在尝试获取可以通过管道传输到wc -l 的文件列表以获取所有文件的字数(不直接使用wc,因此我可以在使用命令之前过滤文件列表)。
我的目录结构是这样的:
- folder
- file.php
- file2.html
- file3.php
- folder1
- folder2a
- folder3b
- folder4
- file.php
- file2.php
我想排除我的find 中的某些目录,主要是库和其他我没有制作的东西。我可以像这样手动完成:
find /var/www/html/ -type f -not -path "/var/www/html/folder/folder1" -not -path "/var/www/html/folder/folder2a" 等
但是,必须明确指定所有文件夹很烦人,而且列表也可能随时更改。我尝试使用/* 和/** 进行模式匹配,但这也不起作用。 有没有办法让我的 find 命令中的这些“非”之一可以排除特定目录的所有子目录,但不排除该目录本身?(包括它的文件,但不包括它的任何子目录)?
这是一个直观的猜测:
find /var/www/html -not -path '/var/www/html/someotherbadfolder' -type f \( ! -path "/var/www/html/folder" -maxdepth 1 \)
但即使是find 也会抱怨:
find: warning: you have specified the -maxdepth option after a non-option argument -not, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
所以看来maxdepth 无法在一个操作中合并。
有很多关于排除特定子目录的问答,但通常不是特定子目录中的任何子目录。
我能够使用-maxdepth 1 使其在单个目录中工作,但问题是这是一个较大命令的排除部分,并且在我运行完整命令后就不起作用了。潜在地,我可能需要排除特定子目录以及其他几个特定子目录中的任何子目录。
【问题讨论】:
-
find /var/www/html -type d -path '/var/www/html/folder/*' -prune -o -type f -print