2008年12月5日星期五

Linux中find命令-path -prune用法详解

Linux中find命令-path -prune用法详解

在Windows中可以在某些路径中查找文件,也可以设定不在某些路径中查找文件,下面用Linux中的find的命令结合其-path -prune参数来看看在Linux中怎么实现此功能。

假如在当前目录下查找文件,且当前目录下有很多文件及目录(多层目录),包括dir0、dir1和dir2 ...等目录及dir00、dir01...dir10、dir11...等子目录。

1. 在当前目录下查找所有txt后缀文件

    find ./ -name *.txt

2.在当前目录下的dir0目录及子目录下查找txt后缀文件

    find ./ -path './dir0*' -name *.txt

3.在当前目录下的dir0目录下的子目录dir00及其子目录下查找txt后缀文件

    find ./ -path '*dir00*' -name *.txt

4.在除dir0及子目录以外的目录下查找txt后缀文件

    find ./ -path './dir0*' -a -prune -o -name *.txt -print

说明:-a 应该是and的缩写,意思是逻辑运算符‘或’(&&); -o应该是or的缩写,意思是逻辑运算符‘与’(||), -not 表示非.

命令行的意思是:如果目录dir0存在(即-a左边为真),则求-prune的值,-prune 返回真,‘与’逻辑表达式为真(即-path './dir0*' -a -prune 为真),find命令将在除这个目录以外的目录下查找txt后缀文件并打印出来;如果目录dir0不存在(即-a左边为假),则不求值-prune ,‘与’逻辑表达式为假,则在当前目录下查找所有txt后缀文件。

5.在除dir0、dir1及子目录以外的目录下查找txt后缀文件

    find ./ \( -path './dir0*' -o -path './dir1*' \) -a -prune -o -name *.txt -print

注意:圆括号()表示表达式的结合。即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。由于命令行不能直接使用圆括号,所以需要用反斜杠'\'进行转意(即'\'转意字符使命令行认识圆括号)。同时注意'\(','\)'两边都需空格。

6.在dir0、dir1及子目录下查找txt后缀文件

    find ./ \( -path './dir0*' -o -path './dir1*' \) -a -name *.txt -print

-----------------------

另附一篇 转载自:http://www.liamdelahunty.com/tips/linux_find_exclude_multiple_directories.php

Linux Find Exclude Multiple Directories

find . -type d \( -name media -o -name images -o -name backups \) -prune -o -print

find . - Find all files in this directory (.)

-type d - directory or folder

-prune - ignore the proceding path of ...

\( -name media -o -name images -o -name backups \) - The -o simply means OR

-o -print - Then if no match print the results, (prune the directories and print the remaining results)

As always in Linux there are many ways to achieve the same result, and the patient may prefer to build up the find command using the path attribute. Please note that you may need to specify the path with a prefix of "./" and no trailing slash.

find . -path './media' -prune -o -path './images' -prune -o -path './backups' -prune -o -print

An important difference here is that the first command will prune ANY directory in the path that matches the name, such as './media' and './public/media', where as the second format will only prune the explict path './media'.


------

http://www.linuxsir.org/main/?q=node/137Linux文件查找命令find,xargs详述

没有评论: