Locating files using the find command(linux find命令)

待我称王封你为后i 2022-06-02 10:12 338阅读 0赞

Locating files using the find command

The find command is a powerful *nix utility that allows the user to find files located in the file system via criteria such as the file name, when file was last accessed, when the file status was last changed, the file’s permissions, owner, group, size, or even number of inodes.

Examples

Find a file “foo.bar” that exists somewhere in the filesystem







  • $ find / -name foo.bar -print

    If the file is found the path to the file will be printed as output. On most platforms the -print is optional, however, on some Unix systems nothing will be output without it. Without specifications find searches recursively through all directories.

Find a file without searching network or mounted filesystems







  • $ find / -name foo.bar -print -xdev

    This is useful if you have mounted network drives or filesystems that you do not want searched. This can increase search speed greatly if the mounted filesystem is large or over a slow network. “-mount” does the same thing as “-xdev” for compatibility with other versions of find.

Find a file without showing “Permission Denied” messages







  • $ find / -name foo.bar -print 2>/dev/null

    When find tries to search a directory or file that you do not have permission to read the message “Permission Denied” will be output to the screen. The 2>/dev/null option sends these messages to /dev/null so that the found files are easily viewed.

Find a file, who’s name ends with .bar, within the current directory and only search 2 directories deep







  • $ find . -name *.bar -maxdepth 2 -print

Search directories “./dir1” and “./dir2” for a file “foo.bar”







  • $ find ./dir1 ./dir2 -name foo.bar -print

Search for files that are owned by the user “joebob”







  • $ find /some/directory -user joebob -print

    The files output will belong to the user “joebob”. Similar criteria are -uid to search for a user by their numerical id, -group to search by a group name, and -gid to search by a group id number.







  • $ find /some/directory -type l -print

    Several types of files can be searched for:

    • b block (buffered) special
      c character (unbuffered) special
      d directory
      p named pipe (FIFO)
      f regular file
      l symbolic link
      s socket
      D door (Solaris)

Search for directories that contain the phrase “foo” but do not end in “.bar”







  • $ find . -name ‘foo‘ ! -name ‘*.bar’ -type d -print

    The “!” allows you to exclude results that contain the phrases following it.

The power of find

find becomes extremely useful when combined with other commands. One such combination would be using find and grep together.







  • $ find ~/documents -type f -name ‘*.txt’ \
    -exec grep -s DOGS {} \; -print

    This sequence uses find to look in /users/home/directory/documents for a file (-type f) with a name ending in .txt. It sends the files it finds to the grep command via the -exec option and grep searches the file found for any occurrences of the word “DOG”. If the file is found it will be output to the screen and if the word “DOG” is found, within one of the found files, the line that “DOG” occurs in will also be output to the screen.

The ordering of find‘s options are important for getting the expected results as well as for performance reason.
Visit POWER TOOLS:A Very Valuable Find, by Jerry Peek, for creative ways to use find and important tips about constructing the command’s options.

转自:http://www.hypexr.org/linux\_find\_help.php

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux 查找文件 命令 locate find

    locate命令,快速查找 locate命令不会对你实际的整个硬盘进行查找,而是在文件的数据库里查找记录。 对于刚创建不久的文件,因为它们还没被收录进文件数据库,因此