如何从find命令中排除特定目录的子目录? [英] How to exclude subdirectories of a specific directory from find command?

查看:487
本文介绍了如何从find命令中排除特定目录的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个文件列表,该文件列表可以通过管道传递到wc -l以获取所有文件的字数统计(不直接使用wc,因此我可以在使用命令之前过滤文件列表).

I'm trying to get a list of files which I can pipe to wc -l to get a word count of all of them (not using wc directly so I can filter the file list before using the command).

我的目录结构是这样的:

My directory structure is something like this:

- folder
   - file.php
   - file2.html
   - file3.php
   - folder1
   - folder2a
   - folder3b
   - folder4
- file.php
- file2.php

我想在我的find中排除某些目录,主要是库和其他我没有做的东西.我可以像这样手动进行操作:

I'd like to exclude certain directories in my find, largely libraries and other stuff that I didn't make. I can do that manually like so:

find /var/www/html/ -type f -not -path "/var/www/html/folder/folder1" -not -path "/var/www/html/folder/folder2a"

但是,必须显式指定所有文件夹很烦人,列表也可能随时更改.我尝试使用/*/**进行模式匹配,但这也不起作用. 在我的find命令中是否有一种方法可以排除特定目录的所有子目录,但不能排除该目录本身? (包括其文件,但不包括其任何子目录)?

However, it's being annoying to have to explicitly specify all the folders, and the list could change at any point, too. I've tried using /* and /** to pattern match but that doesn't work, either. Is there a way for one of these "not"s in my find command that I can exclude all the subdirectories of a particular directory, but not exclude that directory itself? (include its files, but not any of its subdirectories)?

这是一个直观的猜测:

find /var/www/html -not -path '/var/www/html/someotherbadfolder' -type f \( ! -path "/var/www/html/folder" -maxdepth 1 \)

但是,即使find对此也有抱怨:

But even find complains about this:

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似乎无法合并到一个操作中.

So it seems maxdepth is incapable of being combined in an operation.

有很多关于排除特定子目录的问答,但一般而言,不排除特定子目录中的任何子目录.

There's lots of Q&A about excluding specific subdirectories, but not generically any subdirectories in a particular subdirectory.

我可以使用-maxdepth 1在单个目录中运行它,但是问题是这是一个较大命令的排除部分,并且在我运行完整命令后该命令不起作用.潜在地,我可能需要排除特定的子目录以及其他几个特定的​​子目录中的任何子目录.

I was able to get it to work in a single directory with -maxdepth 1, but the problem is this is an exclusion part of a larger command, and that didn't work once I ran the full command. Potentially, I might need to exclude specific subdirectories as well as any subdirectories in several other specific subdirectories.

推荐答案

只需查找:

find /var/www/html -type f -not -path '/var/www/html/folder/*/*'

原始答案:

在find的输出上可能是grep -v的一个黑客:

One hack could be grep -v on the output of find:

find /var/www/html/ -type f | grep -v "/var/www/html/folder/.*/" | wc -l

这篇关于如何从find命令中排除特定目录的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆