The UNIX find command

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Reprint:

In running a very resource consuming f i n d command, many people have a tendency to put it in the background of the implementation, because the traversal of a large file system may take a long time (here refers to the 30 byte G above the file system).
The general form of F i n d commands to:
find pathname -options [-print -exec -ok]
Let us have a look of the order parameter:
The directory path pathname find command lookup. For example. To indicate the current directory, / to represent the system root directory.
The -print find command will match the file to standard output.
The -exec find command to execute the argument s h e l l on the matching file. The corresponding order form as' c o m m and'{} \ {} \;, attention and the space between;.
-ok and e x E C - the same, but with a more safe mode to execute the argument s h e l l command, before executing each command, will give tips, allowing the user to determine whether the implementation of.

The find command options:

The fi n d command has a lot of options or expression, in front of each option with a bar -. The main options let us look first for the command, and then give some examples.
In accordance with the file name to find the file -name.
According to -perm file permissions for a file.
-prune use this option to enable the f i n d command is not present in the directory specified in the search, if you use the - d e p t h option, then - P R U N e f i n d option will be ignored the command.
The -user document in accordance with the owner to find the file.
-group according to the file group to find the file.
-mtime -n +N according to the time change file to find the file, n said file change time is n days from now, + n said when the file was changed from n now days ago. F i n d a t I m command and - E and - c t I m e option, but all of them and m t I m e option is like, so we only introduce; here m t I m e option.

-nogroup find no effective belong to group documents, namely the the file group in T C / G R / e o u p s does not exist.
-nouser find no effective master file, the owner of the file in the / E T C / p a s s w d does not exist.
-newer file1 ! File2 find the changes time than the file f I L E 1 f I l e files but than 2 old files.
-type for a certain file types, such as:
B - block device file.
The D directory.
C character device file.
P pipeline file.
L - a symbolic link file.
F - the common files.
-size n[c] Find the file length is n block of the file with C, indicates that the file length in bytes.
-depth in the search for documents, first find the files in the current directory, and then in the sub directory lookup.
-fstype search is a type of file in the file system, the file system type is typically in the configuration file / E T C / f s t a found in the B, the configuration file contains information about the file system information in this system.
-mount does not cross file system m o u n t in the search for documents.
If -follow f i n d command in a symbolic link file, on track to link to a file.

Use the name option:

The file name is f i n d command is the most commonly used options, either to use this option, or used with other options. You can use a file name pattern to match the file, remember to use the quotes will file name pattern led.
No matter what is the current path, if you want to $H O M E root directory is searched in the file name with *. T x T file, use the a T H ~ as' p n a m e parameter, tilde ~ represents your $H O M E directory.
$ find ~ -name "*.txt" -print
Want to in the current search directory and subdirectories all '*. T file, x T' can be used:
$ find . -name "*.txt" -print
To the current directory and subdirectory search for file name begins with a capital letter file, can use:
$ find . -name "[A-Z]*" -print
Want to find the file E / T in the C directory name to h o s t at the beginning of the file, you can use:
$ find /etc -name "host*" -print
Want to find $H O M files in the E directory, can use:
$ find ~ -name "*" -pri or NTF -print ind.
To make the high load operation, began to find all the files from the root directory. If you want to keep a good impression in the system administrator there, preferably before doing so to think clearly!
$ find / -name "*" -print
If you want to in the current directory for a file name with a lowercase letters at the beginning of two, followed by two digits, the last is the *. T x T file, the following command will return called a x 37 t x T files:

$ find . -name "[a-z][a-z][0--9][0--9].txt" -print

Use the perm option:

If you want to find the file according to the file access mode, can use p e r m option. You may need to find all the user has permission to execute the file, or wish to view a user file directory permission type. When using this option, the best use of octal permission representation. In order to search the current directory file permission bits into 755 files, the file owner can read, write, execute, other users can read, execute the file, you can use:
$ find . -perm 755 -print
If you want to find in the current directory of all users are readable, write, execute the file (be careful in this case), we can use the f i n d p e r m command options. In the octal digits must be prefixed with a bar -. In the following order - p e r m on behalf of the file permissions to find, and '007' and you are using c h m o in the absolute mode D command representation in exactly the same.
$ find . -perm -007 -print

Overlook a directory:

If you want to ignore a directory in the search for documents, for you know not what you want to find files in that directory, you can use the p r u n e option to ignore the directory pointed out. In the use of P R U N E option to be careful, because if you use the - d e p t h option, then - P R U N e f i n d options will be the command to ignore. If you want to in the / a p p s directory to find the file, but do not want to look at / a P P S / b i n directory, you can use:
$ find /apps -name "/apps/bin" -prune -o -print

Using the user and nouser options:

If you want to find the file according to the file owner, can give the corresponding name. For example, in the $H O M in the E directory to find the file owner d a V E files, can be used:
$ find ~ -user dave -print
In the / E T c Directory to find the file owner u u c p files:
$ find /etc -user uucp -print
In order to find the owner of the account has been deleted files, you can use n o u s e r option. So we can find the owner in / E T C / p a s s w d file does not contain a valid account file. In the use of n o u s e r option, do not give the user name; f i n d command to complete the work for you. For example, in the hope of / h o m e directory search all the files, can be used:
$ find /home -nouser -print

Using the group and nogroup options:

Using the group and nogroup options like u s e r and n o u s e r option, the user group of files belonging to the f I, n d command has the same options, in the / a p p s directory search belonging to the a c c t s user group file, can use:
$ find /apps -group accts -print

To find all files without a valid users belong to the group, you can use the n o g r o u p option. N f I the D command from the root directory of the file system is to find this file
$ fine/-nogroup-print

According to the change of time to find the file:

According to the change of time to find the file
If you want to find the file in accordance with the change of time, you can use the m t I m e option. If the system suddenly no space available, probably during the length of a file of this rapid growth, then we can find this file with m t I m e option. With a minus sign to limit the change time about n days of the file, and using the plus + to limit the time before the change was n file. In the hope that the system root directory search time of change within 5 days of the file, can use:
$ find / -mtime -5 -print
In order to / V A R / a D M directory search time of change in the 3 days before the file, can use:
$ find /var/adm -mtime +3 -print

Search than a file new or old files:

If you want to find the changes of time than a new file but another file all documents of old, can use the n e w e r option. Its general form is:
newest_file_name ! oldest_file_name
Among them,! Logic is not symbol. There are two files, time to change their difference of about two days.

The following f i n d command to find the changes of time than the file a G E. A w K B e l t than the file s. A w k the old file:

If the options you want to use f i n d command to find the changes of time within two hours of the document, unless there is a ready-made documents the change time just two hours ago, otherwise there can be used to change the time compared files. In order to solve this problem, create a file and its date and time stamp is set to the time required for. It can use the t o u c h command to realize.
Assume now that time is 23: 40, hope to find the changes of time within two hours of the file, create a file:

To meet the requirements of a file has been created; here we assume that today is May 4th, and the changes to a file time is 21: 40, just two hours earlier than now. Now we can use the f i n d n e w e command - R option looks in the current directory of all change the time within two hours of the file:
$ find . -newer dstamp -print

Use the type option:

If you want to in the / E T C directory search all of the directory, can use:
$ find /etc -type d -print
In order to search the current directory in the directory outside of all types of documents, can be used:
$ find . ! -type d -print
In order to / all file symbolic link E T C directory search, can use:
$ find /etc -type l -print

Use the size option:

Can find the file according to the file size, the length of a file can be used block (b l o c k) to measure, can also be used to measure the byte. In order to express the form byte file length measurement N C; to block measurement file length using only digital representation can be. Personally, I always use in bytes, in accordance with the length of the file to find the file, most people like to use the byte length of the file, not the number of blocks that, unless it is in view of the size of the file system, because then the block to measure the easier conversion.
In order to find files in the current directory length greater than 1 bytes of M files, can be used:
$ find . -size +1000000c -print
In order to / h o m e / a p a c h e directory for the file length exactly 100 bytes file, can use:
$ find /home/apache -size 100c -print
In order to in the current directory to find the length of more than 10 pieces of the file (a is equal to 512 bytes), can be used:
$ find . -size +10 -print

Use the depth option:

In the use of f i n d command, may want to match all files, then looks in the directory in. Use the d e p t h option can make f i n d command to do so. One reason for this is, when in the use of f i n d command to the tape backup file system, like the first backup all files, then the backup files in subdirectories. In the following example, the f i n d command starts from the root directory file system, to find a C O N F I L E files.
It will be the first to match all the files and then enter the search subdirectories.
$ find / -name "CON.FILE" -depth -print

Use the mount option:

Find files in the file system of the current (not into other file system), you can use the f i n d m o u n t command options. In the example below, we began to look at the file system file name ends with the X C file from the current directory:
$ find . -name "*.XC" -mount -print

Use exec or OK to execute the shell command.:

When matched to some files, may wish to do something with it, then you can use the e x E C option. Once the f i n d command is matched to the appropriate file, can operate on the x E C option in the e command (only in some operating system of e x E C option such as l s or LS -l order). Most people use this option in order to find the old file and delete them. I strongly suggest you before the R M command to delete the file, it is best to use the l s command to check, confirm them is to delete the file. E x E C option followed the command to execute, then a pair of {the}, an empty and a \, and finally a
Semicolon . In order to use the e x E C option, we must also use the p r i n t option. If the check f i n d command, will find the command output from the current relative path and file path name.
In order to use the LS -l command to list the matching to the file, the LS -l command on the f i n d e x E C command options, for example:

In the example above, the f i n d command to match all the ordinary files in the current directory, and in the e x E C option LS -l ordered them to be listed.
In order to / l o g in the s directory to find the change time to remove them and in 5 previous papers, can use:
$ find logs -type f -mtime +5 -exec rm {} \;
Remember, in any way in the s h e l l delete files before, should first check the relevant documents, must be careful!

Finally, to teach you a useful example, use the find command to statistical file record number:

find . -type f -exec cat * {} \; | wc

The number of records this command statistics on current directory of all documents, in my work is very useful command.

find / -name httpd.conf
find / -name access_log 2>/dev/null
find /etc -name '*srm*'
find / -amin -10 # Look at the system in the last 10 minutes to access files
find / -atime -2 # Look at the last 48 hour access to system files
find / -mmin -5 # Find changes in the system in the last 5 minutes of the file
find / -mtime -1 #Find changes in system file in the last 24 hours
find / -cmin -5 # Search is to change the state of a file on the system in the last 5 minutes
find / -ctime -1 #Search is to change the state of a file on the system in the last 24 hours
find / -user reda #Look at the system belongs to the Fred of the user's files
find / -not -user reda #FRED search does not belong to the user in the system file
find / -group redagrp # Find belong to redagrp group in the system file
find / -gid 501 #Find the system belongs to the group of 501 for ID files
find / -user fred -a -group redagrp
find / -user reda -o -user tracy
find / -nouser #Look at the system belongs to the invalid user files
find / -empty # Search in the system is empty file or folder is empty
find / -false #Always the wrong file search system
find / -size +5k #5K bytes greater than the search system in the document
find / -size +5c #More than 5 bytes of file search system
find / -perm +6000
find / -type b
The file type:
B block (buffer) equipment.
C character device.
The D directory.
P named pipe(FIFO).
F rules file.
L symbolic link.
S socket.
find / -maxdepth 2 -name fred
find /tmp -size +10000000c -and -mtime +2
find / -user reda -or -user tracy
find /tmp ! -user reda
find / -name 'httpd.conf' -ls
find / -user reda -exec ls -l {} /;
find / -user reda -ok #Confirmation
find / -user reda | xargs ls -l

The find command is one of the most powerful one of the command, the command but also command line structure is the most difficult to master.

# find / -print | WC -l shows the number of all files and directories in the system.
# find / -user $LOGNAME -print show the user all of the files and directories in the system.
# find / -size 100 -print display the file size is 100 blocks.
# find / -size -100 -print displays the file size is less than 100 blocks .
# find / -size +100 -print display file size is greater than 100 blocks .
# find / -name core -exec rm {} \, Find and delete the core file.
# find . -exec chown $LOGNAME {} \, Modify all files in a directory of the user belongs to.
# find .-type d -exec chmod 770 {} \, Modify all directory a directory permissions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Timothy at December 28, 2013 - 4:01 PM