Linux Learning-find Command __linux

Source: Internet
Author: User
Tags file permissions
Linux Learning-find command

Used to look up files in the file tree and make the appropriate processing


Command format:

Find Pathname-options [-print-exec-ok ...]


Command parameters:

The directory path that the Pathname:find command looks for. For example, to represent the current directory, with/to represent the system root directory.

The-print:find command outputs matching files to the standard output.

The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form ' command ' {} \;, note the space between {} and \;

-ok: The same as the-exec, but in a more secure mode to execute the shell command given by this parameter, before executing each command, will be prompted to let the user determine whether to execute.


Command options:

-name find files by file name

-perm Find files by file permissions

-user find files by file owner

-group finds files by the group to which the file belongs.

-type find a type of file, such as:

B-Block device files

D-Directory

C-Character device files

L-Symbolic Link file

P-Pipe File

F-Ordinary Documents

-size N: [c] Find file length is n block file, with C time table file byte size

-amin N Find the last n minutes of files accessed in the system

-atime N Find the last n*24-hour access to files in the system

-cmin n finds files in the system with the last n minutes of changed file status

-ctime n finds files in the system where the last n*24 hours are changed file status

-mmin n finds files in the system with the last n minutes of changed file data

-mtime n finds files in the system for the last n*24 hours changed file data

(with a minus sign-to limit the time to change within the n days of the file, and with the Plus + to limit the change time before the N-day file. )

-maxdepth n Maximum Lookup directory depth

-prune option to indicate the directory you want to ignore. Be careful when using the-prune option, because if you use the-depth option at the same time, the-prune option is ignored by the Find command

-newer You can use the-newer option if you want to find all files that have changed more than a new file but older than another file.


Instance:

(1) Find the modified files within 48 hours

Find-atime-2


(2) Find a file in the current directory that ends with. log. ". " Represents the current directory

Find./-name ' *.log '


(3) Find files under the/opt directory with permission of 777

Find/opt-perm 777


(4) Find files larger than 1K

Find-size +1000c


Find-size 1000c Find files equal to 1000 characters

The-exec parameter is followed by the command command, which terminates with; For the end sign, so the semicolon behind the command is indispensable, and the preceding backslash is preceded by a semicolon, given the different meanings of semicolons in each system. {} curly braces represent the names of the files found in the previous find.

(5) Find files in the current directory that have changed time before 10th and delete them (no reminders)

Find. -type f-mtime +10-exec rm-f {} \;


(6) Find all files in the current directory that end with. Log, change the time above 5th, and delete them, but give a hint before deleting them. Press the Y key to delete the file, press N to not delete

Find. -name ' *.log ' Mtime +5-ok-exec rm {} \;


(7) Find file name in current directory with passwd, content containing "pkg" character files

Find. -f-name ' passwd* '-exec grep "pkg" {} \;


(8) Execute CP command with EXEC option

Find. -name ' *.log '-exec cp {} test3 \;

The-xargs find command passes the matching file to the Xargs command, and the Xargs command gets only a portion of the file at a time instead of all, unlike the-exec option. This allows it to process the first part of the file, then the next batch, and so on.

Instance:

(9) Find each normal file under the current directory and use Xargs to determine the file type


Find. -type F-print | Xargs file


(10) Find all normal files with the ' editor ' character at the end of JS in the current directory

Find. -type f-name "*.js"-exec grep-lf ' ueditor ' {} \;
Find-type f-name ' *.js ' | Xargs grep-lf ' editor '


(11) Using Xargs to execute MV command

Find. -name "*.log" | Xargs-i MV {} test4


(12) Use the grep command to search the word hostnames in all normal files in the current directory and mark the

Find. -name \* (Escape)-type f-print | xargs grep-n ' hostnames '


(13) Find files in the current directory that start with a lowercase letter, and finally 4 to 9 plus. Log End

Find. -name ' [A-z]*[4-9].log '-print


(14) In the test directory lookup is not test4 subdirectory lookup

Find Test-path ' test/test4 '-prune-o-print


(15) Instance 1: Find change time than file Log2012.log new but older files than file Log2017.log

Find-newer Log2012.log! -newer Log2017.log


Use the depth option:

The depth option enables the Find command to back up the file system on tape by first backing up all files and then backing up files in subdirectories.

&nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.