Unix中find用法

To use the find command, at the Unix prompt, enter:

find . -name "pattern" -print

Replace "pattern" with a filename or matching expression, such as "*.txt" . (Leave the double quotes in.)

Options

The general form of the command is:

find (starting directory) (matching criteria and actions)

The find command will begin looking in the starting directory you specify and proceed to search through all accessible subdirectories. You may specify more than one starting directory for searching.

You have several options for matching criteria:

-atime nFile was accessed n days ago
-mtime nFile was modified n days ago
-size nFile is n blocks big (a block is 512 bytes)
-type cSpecifies file type: f=plain text, d=directory
-fstype typSpecifies file system type: 4.2 or nfs
-name namThe filename is nam
-user usrThe file's owner is usr
-group grpThe file's group owner is grp
-perm pThe file's access mode is p (where p is an integer)

You can use  +  (plus) and  -  (minus) modifiers with the atimemtime, and size criteria to increase their usefulness, for example:

-mtime +7Matches files modified more than seven days ago
-atime -2Matches files accessed less than two days ago
-size +100Matches files larger than 100 blocks (50KB)

By default, multiple options are joined by "and". You may specify "or" with the  -o  flag and the use of grouped parentheses. To match all files modified more than 7 days ago and accessed more than 30 days ago, use:

\( -mtime +7 -atime +30 \)

To match all files modified more than 7 days ago or accessed more than 30 days ago, use:

\( -mtime +7 -o -atime +30 \)

You may specify "not" with an exclamation point. To match all files ending in .txt except the file notme.txt, use:

\! -name notme.txt -name \*.txt

You can specify the following actions for the list of files that the find command locates:

-printDisplay pathnames of matching files.
-exec cmdExecute command cmd on a file.
-ok cmdPrompt before executing the command cmd on a file.
-mount(System V) Restrict to file system of starting directory.
-xdev(BSD) Restrict to file system of starting directory.
-prune(BSD) Don't descend into subdirectories.

Executed commands must end with  \;  (a backslash and semi-colon) and may use  {}  (curly braces) as a placeholder for each file that the find command locates. For example, for a long listing of each file found, use:

-exec ls -l {} \;

Matching criteria and actions may appear in any order and are evaluated from left to right.

Full examples

  • To find and report all C language source code files starting at the current directory, enter: find . -name \*.c -print
  • To report all files starting in the directories /mydir1 and /mydir2 larger than 2,000 blocks (about 1,000KB) and that have not been accessed in over 30 days, enter: find /mydir1 /mydir2 -size +2000 -atime +30 -print
  • To remove (with prompting) all files starting in the /mydir directory that have not been accessed in over 100 days, enter: find /mydir -atime +100 -ok rm {} \;
  • To show a long listing starting in /mydir of files not modified in over 20 days or not accessed in over 40 days, enter: find /mydir \(-mtime +20 -o -atime +40\) -exec ls -l {} \;
  • To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter: find /prog -type f -size +1000 -print -name core -exec rm {} \;Note: On some systems, the name of the starting directory must end with a  /  (slash), or the find command will return nothing. Thus, the starting directory in the previous example would be designated as /prog/, with a trailing slash. On other systems, a trailing slash does not affect the command. A trailing slash is never needed when searching in / (the root directory), . (the current directory), or .. (the parent directory).

For more information, consult the Unix manual page by entering at the Unix prompt:

man find
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值