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

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 a file that is a certain type. "-type l" searches for symbolic links
  • $ 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值