Ubuntu file search method (find command)

Source: Internet
Author: User


-Amin n
Find the files accessed in the last N minutes of the system
-Atime n
Find the last n * 24 hours of files in the system
-Cmin n
Find the file whose status is changed in the last N minutes in the system
-Ctime n
Find the file whose status is changed in the last n * 24 hours in the system.
 -Mmin n
Find the file whose data is changed in the last N minutes in the system
-Mtime n
Find the file whose data has been changed for the last n * 24 hours in the system.


4. execute shell commands using exec or OK.

When using find, you only need to write the desired operation in a file, you can use exec to search with find, which is very convenient.

In some operating systems, only the-exec option is allowed to execute commands such as l s or ls-l. Most users use this option to find and delete old files. Before executing the rm command to delete files, we recommend that you use the ls command to check whether they are the files to be deleted.

The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, a/, and a semicolon. To use the exec option, you must use the print option at the same time. If you verify the find command, you will find that this command only outputs the relative path and file name from the current path.

For example, to use the ls-l command to list the matched files, you can place the ls-l command in the-exec option of the find command.

# Find.-type f-exec ls-l {}/;
-Rw-r -- 1 root 34928 2003-02-25./conf/httpd. conf
-Rw-r -- 1 root 12959 2003-02-25./conf/magic
-Rw-r -- 1 root 180 2003-02-25./conf. d/README

In the preceding example, the find command matches all common files in the current directory and lists them using the ls-l command in the-exec option.
In the/logs directory, find the files whose changes were earlier than 5 days and delete them:

$ Find logs-type f-mtime + 5-exec rm {}/;

Remember: you should check the corresponding file before deleting the file in shell in any way. Be careful! When using commands such as mv or rm, you can use the-exec option safe mode. It will prompt you before performing operations on each matching file.

In the following example, the find command searches for all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion.

$ Find.-name "*. conf"-mtime + 5-OK rm {}/;
<Rm.../conf/httpd. conf>? N

Press y to delete the file, and press n to not delete the file.

Any form of command can be used in the-exec option.

In the following example, we use the grep command. The find command First matches all files named "passwd *", such as passwd, passwd. old, passwd. bak, and then runs the grep command to check whether a sam user exists in these files.

# Find/etc-name "passwd *"-exec grep "sam "{}/;
Sam: x: 501: 501:/usr/sam:/bin/bash

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.