Beginners how to quickly learn the find command under Linux

  

Simple understanding of the Linux find command is not enough, we also need to know how to use it, the following small series from the find example to introduce the use of find, I hope that Beginners can help.

Let's start with a simple example to start.

$ find /-name test

. /backup/modules/field/modules/test

$

“Find the file named ’test’ in the root directory, this command will let the system find all files. Includes mounted file devices. This can take a while, especially to find a network shared hard drive. However, we can tell by the parameter -mount that the system ignores the mounted device:

$ find /-mount -name test

The find command has the following format:

find [path ] [options] [tests] [actions]

[path]

Paths; should not be difficult to understand. Here you can use absolute paths, and you can use relative paths quickly.

[options]

Parameters; more commonly used parameters:

-depth: first find subdirectories and then view current directory -follow: trace find connection file -maxdepths N : Subdirectory Recursion Maximum Depth - mount(or -xdev): Ignore Mounted Files

[tests]

Conditional Matching;

-atime -N/N/+ N: The last time the file was accessed in N days/N days/N days ago -mtime -N/N/+N: The last time the file was modified in N days/N days/N days ago-name pattern: and pattern Matching files (including directories) -newer f1 ! F2: newer file than file f1, older than file f2 -type b/d/c/p/l/f: file type: block device/directory/character device/pipe/link/file-user username: The owner of the file is username

We can connect the matching conditions by the following operators:

-not (!): Direction matching -and (-a): and -or ( -o): or

We can also merge some matching symbols in parentheses. For example,

\\(-newer -o -name ‘*test’ \\)

Now take a slightly more complicated example, look for files that have been accessed or modified that day, file names Contains & rsquo;python’, and the file owner is ’anthony’:

# find /\\( -atime -1 -or -mtime -1 \\) -and -name ‘*python*&rsquo ; -and -user ‘anthony’

/home/anthony/svn_code/subversion-1.7.2/subversion/bindings/swig/python

/home/anthony/svn_code/subversion- 1.7.2/subversion/bindings/ctypes-python

/home/anthony/python

/home/anthony/python/Python-3.2.2/build/temp.linux-x86_64- 3.2/home/anthony/python

/home/anthony/python/Python-3.2.2/Tools/unicode/python-mappings

/home/anthony/.local/lib/python3 .2

#

[actions]

Operation;

-exec command: Execute the command, as described later. -ok command: Same as -exec, which requires user permission in addition to command execution. -print: Print file name -ls: List file details

Now give an example -exec command

anthony@z:~$ find -mtime -1 -type f -exec ls - l {} \\;

-rw-r--r-- 1 anthony anthony 0 Apr 5 12:04 . /search/search.txt

-rw------- 1 anthony anthony 22997 Apr 5 12:04 . /.viminfo

-rw------- 1 anthony anthony 125 Apr 5 14:25 . /.lesshst

anthony@z:~$

Simply put, -exec or -ok, pass the queried file as a parameter to the subsequent command execution, and the position of the parameter is used. {}Identification, ie in the command, “{}” replaced with the file name found by find, and finally ”\\;” indicates the terminator.

The above is the introduction of the Linux find command. Learning the find command from the example will be much better than reading the theoretical knowledge. For beginners, it is necessary to see more examples.

Copyright © Windows knowledge All Rights Reserved