如何只查找给定目录中的文件,并使用bash忽略子目录

时间:2011-10-10 15:56:25

标签: bash unix

我查看了其他类似的问题,但没有找到一个可以让我掌握这个概念并根据我有限的时间使其适用于我的情况的问题。我只是运行find命令来查找某些文件,但是子目录中的某些文件具有我想忽略的相同名称。谢谢你的帮助。以下是我正在使用的命令:

我感兴趣的文件/模式:     / dev / abc-scanner,/ dev / abc-cash ....

命令:

find /dev/ -name 'abc-*'

返回了什么:

/dev/abc-scanner
/dev/abc-cash
...
...
...
/dev/.udev/names/abc-scanner
/dev/.udev/names/abc-cash

我想忽略后面的文件:/dev/.udev /...

5 个答案:

答案 0 :(得分:148)

如果您只想将查找限制在第一级,您可以这样做:

 find /dev -maxdepth 1 -name 'abc-*'

...或者如果你特别想要排除.udev目录,你可以这样做:

 find /dev -name '.udev' -prune -o -name 'abc-*' -print

答案 1 :(得分:8)

是否有任何特殊原因需要使用find?您只需使用ls查找与目录中的模式匹配的文件。

ls /dev/abc-*

如果确实需要使用find,则可以使用-maxdepth 1开关仅应用于指定的目录。

答案 2 :(得分:2)

这可能会做你想要的:

find /dev \( ! -name /dev -prune \) -type f -print

答案 3 :(得分:0)

我遇到了一个更普遍的问题-我想在匹配模式的目录中找到文件,但不在其子目录中找到文件。

我的解决方案(假设我们正在寻找直接位于cpp目录中的所有arch文件):

find . -path "*/arch/*/*" -prune -o -path "*/arch/*.cpp" -print

我无法使用maxdepth,因为它首先限制了搜索,并且不知道我想排除的子目录的名称。

答案 4 :(得分:-1)

    server {
    server_name ~.*;

    location / {
        root /usr/src/app;

        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    # optionally disable falling back to PHP script for the asset directories;
    # nginx will return a 404 error when files are not found instead of passing the
    # request to Symfony (improves performance but Symfony's 404 page is not displayed)
    # location /bundles {
    #     try_files $uri =404;
    # }

    location ~ ^/index\.php(/|$) {
         client_max_body_size 50m;

         fastcgi_pass php:9000;
         fastcgi_buffers 16 16k;
         fastcgi_buffer_size 32k;
         include fastcgi_params;
         fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

不适用于我。它什么也没返回。如果我只是做“。”它为我提供了我正在处理的目录下目录中的所有文件。

find /dev -maxdepth 1 -name 'abc-*'

以“。”不返回任何内容。相反,我会获得目录中所有“大”文件的列表以及存储旧文件的rootfiles /目录。

继续。这行得通。

find /dev -maxdepth 1 -name "*.root" -type 'f' -size +100k -ls

显然find ./ -maxdepth 1 -name "*.root" -type 'f' -size +100k -ls 564751 71 -rw-r--r-- 1 snyder bfactory 115739 May 21 12:39 ./R24eTightPiPi771052-55.root 565197 105 -rw-r--r-- 1 snyder bfactory 150719 May 21 14:27 ./R24eTightPiPi771106-2.root 565023 94 -rw-r--r-- 1 snyder bfactory 134180 May 21 12:59 ./R24eTightPiPi77999-109.root 719678 82 -rw-r--r-- 1 snyder bfactory 121149 May 21 12:42 ./R24eTightPiPi771098-10.root 564029 140 -rw-r--r-- 1 snyder bfactory 170181 May 21 14:14 ./combo77v.root 表示感兴趣的目录。但是需要/dev,而不仅仅是./。即使在我弄清楚.的含义或多或少之后,对/的需求也不明显。

我没有回复,因为我没有“声誉”。

相关问题