我正在尝试获取一个文件列表,我可以通过管道将其发送到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
似乎无法在操作中组合。
有很多问答;A关于排除特定的子目录,但一般不排除特定子目录中的任何子目录。
使用-maxdepth 1
,我可以让它在一个目录中工作,但问题是这是一个更大命令的排除部分,一旦我运行了完整命令,它就不起作用了。我可能需要排除特定的子目录以及其他几个特定子目录中的任何子目录。
假设您专门查找文件(即不是目录(:
find /var/www/html -type f -not -path "/var/www/html/folder/*/*"
这是因为:
直接位于/var/www/html/folder
下的- 文件不是目录,因此它们与
-path
子句不匹配 - 直接位于
/var/www/html/folder
下的目录与-type f
不匹配 /var/www/html/folder
子目录下的文件的路径中必须有额外的/
,因此它们与-path
表达式匹配
仅使用find:
find /var/www/html -type f -not -path '/var/www/html/folder/*/*'
原始答案:
一个破解可能是find:输出上的grep -v
find /var/www/html/ -type f | grep -v "/var/www/html/folder/.*/" | wc -l