Ubuntu file search method (find Command)

Source: Internet
Author: User
Ubuntu file search method (find command) common format: findpathname-options [-print-exec-OK] example: find-namefilename then search for the findetc-name * s file named filename in the root directory. search for the ubuntu search file method (find command) with s in the directory in the common format: find pathname-options [-print-exec-OK] example: find/-name filename search file named filename in the root directory find/etc-name * s * search for files with s in the directory find/etc-name * S in the directory search for the find/etc-name s file ending with s * in the directory and search for the find/-amin-10 file starting with s in the system /-atime-2 find the last 4 in the system Find/-empty files accessed within 8 hours to find files that are empty in the system or find/-group groupname folders find/-mmin-5 files that belong to groupname in the system find/-mtime-1 files modified in the last 5 minutes in the system find/-nouser files in the last 24 hours in the system find/ -user username: Search for the file "find/-ctime-1" that belongs to username in the system. find/-fstype type "is the file type in the system that has been changed for the last 24 hours. is it? File find/-user user1name-or-useruser2name search in the system belong to user1name or belong to user2name file find/-user user1name-and-user2name in the system to find both belong to user1name and belong to user2name user files. 1. find command format 1. the common form of find command is; find pathname-options [-print-exec-OK...] 2. parameters of the find command; pathname: directory path searched by the find command. For example, use "." to represent the current directory, and use "/" to represent the root directory of the system. -Print: The find command outputs matching files to the standard output. -Exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}/;. note the space between {} and. -OK: the role of-exec is the same, but the shell command given by this parameter is executed in a safer mode. a prompt is displayed before each command is executed, let the user determine whether to execute. 3. find command option-name searches for files by file name. -Perm searches for files based on the file permissions. -Prune uses this option to make the find command not to be searched in the specified directory. if The-depth option is used at the same time,-prune will be ignored by the find command. -The user searches for files based on the file owner. -The group searches for files based on the group to which the files belong. -Mtime-n + n: find the file based on the file change time.-n indicates that the file change time is earlier than n days, and + n indicates that the file change time is earlier than n days. The find Command also has the-atime and-ctime options, but they both have the-m time options. -Nogroup: find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups. -Nouser: find the file without a valid owner, that is, the owner of the file does not exist in/etc/passwd. -Newer file1! File2 searches for files whose change time is newer than file1 but older than file2. -Type: search for a certain type of files, such as B-block device files. D-directory. C-character device file. P-MPs queue file. L-symbolic link file. F-common file. -Size n: [c] searches for files with a length of n blocks. if a file contains c, the file length is measured in bytes. -Depth: When searching for a file, first find the file in the current directory and then find it in its subdirectory. -Fstype: searches for files in a certain type of file system. These file system types can usually be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system. -Mount: The mount point of the file system is not crossed during file search. -Follow: If the find command encounters a symbolic link file, it will trace the file to which the link points. -Cpio: Use the cpio command to back up the files to the tape device. In addition, the following three differences: -amin n: find the files accessed in the last N minutes in the system.-atime n: find the files accessed in the last n * 24 hours in the system.-cmin n: find the files whose status is changed in the last N minutes in the system. FILE-ctime n find the file whose status has changed in the last n * 24 hours in the system-mmin n find the file whose data has been changed in the last N minutes in the system-mtime n find the last file in the system files whose file data is changed in n * 24 hours 4. when using exec or OK to execute shell commands to use find, as long as you write the desired operation in a file, you can use exec to search with find, in some operating systems, it is convenient to only allow the-exec option 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. /conf/httpd. conf-rw-r -- 1 root 12959. /conf/magic-rw-r -- 1 root 180 2003-02-25. /conf. in the above example of d/README, the find command matches all common files in the current directory and uses the ls-l command in the-exec option to list them. Find the files whose modification time is earlier than 5 days in the/logs directory and delete them: $ find logs-type f-mtime + 5-exec rm {}/; remember: before deleting a file in shell, check the corresponding file. 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
Related Article

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.