LINUX

LINUX FIND指令

介紹

find指令用來尋找檔案。

語法

find [directories] [expressions] [actions]

常用的expressions

-name pattern       The name of the desired file must match this shell pattern, which may include shell wildcards *,?, and [].

-path pattern       The pathname of the desired file must match this shell pattern, which may include shell wildcards *,?, and [].

-lname pattern      The symbolic link target of the desired file must match this shell pattern, which may include shell wildcards *,?, and [].

-iname pattern      Same as -name, but is case-insensitive.

-ipath pattern      Same as -path, but is case-insensitive.

-ilname pattern     Same as -lname, but is case-insensitive.

-regex regexp       The path (relative to the directory tree being searched) must match the given regular expression.

-type t             Locate only files of type t. This includes plain files (f), directories (d), symbolic links (l), block device (b), character device (c), name pipes (p), and sockets (s).

-atime N            File was last accessed exactly N days ago. Use +N for "greater than N," or -N for "less than N."

-ctime N            File was last changed exactly N days ago. Use +N for "greater than N," or -N for "less than N."

-mtime N            File was last modified exactly N days ago. Use +N for "greater than N," or -N for "less than N."

-amin N             File was last accessed exactly N minutes ago. Use +N for "greater than N," or -N for "less than N."

-cmin N             File was last changed exactly N minutes ago. Use +N for "greater than N," or -N for "less than N."

-mmin N             File was last modified exactly N minutes ago. Use +N for "greater than N," or -N for "less than N."

-anewer other_file  File was accessed more recently than other_file.

-cnewer other_file  File was modified more recently than other_file.

-newer other_file   File was changed more recently than other_file.

-maxdepth N         Consider files at most N levels deep in the directory tree being searched.

-mindepth N         Consider files at least N levels deep in the directory tree being searched.

-follow             Dereference symbolic links.

-depth              Proceed using depth-first search: completely search a directory's contents (recursively) before operating on the directory itself.

-xdev               Limit the search to a single filesystem (i.e., don't cross device boundaries).

-size N[bckw]       Consider files of size N, which can be given in blocks (b), one-byte characters (c), kilobytes (k), or two-byte words (w). Use +N for "greater than N," or -N for "less than N."

-empty              File has zero size, and is a regular file or directory.

-user name          File is owned by the given user.

-group name         File is owned by the given group.

-perm mode          File has permissions equal to mode. Use -mode to check that all of the given bits are set, or +mode to check that any of the given bits are set.

常用的actions

-print          Simply print the path to the file, relative to the search directory.

-printf string  Print the given string, which may have substitutions applied to it in the manner of the C library function, printf().

-print0         Like -print, but instead of separating each line of output with a newline character, use a null (ASCII 0) character. Use when piping the output of find to another program, and your list of filenames may contain space characters. Of course, the receiving program must be capable of reading and parsing these null-separated lines (e.g., xargs -0).

-exec cmd;      Invoke the given shell command, cmd. Make sure to escape any shell metacharacters, including the required, final semicolon, so they are not immediately evaluated on the command line. Also, the symbol "{}" (make sure to quote or escape it) represents the path to the file found. A full example is: find . -exec ls "{}" \;

-ok cmd;        Same as -exec, but also prompts the user before invoking each command.

-ls             Perform the command ls -dils on the file.

-delete         Delete all the files found.

範例

你可以尋找特定檔案:

⤍ find . -type f -name myfile -print
./myfile

你可以搭配萬用字元使用(記得要使用跳脫字元):

⤍ find . -type f -name myfile\* -print
./myfile1.txt
./myfile2.txt
./myfile3.txt

列出所有檔案夾(遞迴):

⤍ find . -type d -print
.
./dir1
./dir2
./dir3
./dir3/mydir
./dir4
./dir5
./dir5/mt

列出所有最後修改時間(mtime)超過30分鐘的檔案:

⤍ find . -type f -mmin +30 -print
./old_file1
./old_file2
./old_file3

刪除所有最後修改時間(mtime)超過30分鐘的檔案:

⤍ find . -type f -mmin +30 -delete

發佈留言