Linux Find Files 6 highly efficient tool
1 Introduction
We use Linux process, there is often a file search needs, then what tools can quickly find files in Linux command it? Let's look at these tools:
1.1 find command
Linux find command is a Linux command all the most useful one, but also the most confusing one. It is difficult, because of the different standard syntax syntax with other Linux commands. However, it is very powerful because it allows you to by file name, file type, users can even find the file timestamp. Use the find command, you can not only find files with any combination of these properties, it can also perform file operations found.
1.2 locate command
Linux locate command to find qualified document, he will go to save a database file and directory names, find the document or template style directory in line with the conditions. so that users can locate the specified file if there is a very fast file search system. The method is to build a database of all file names and paths within the system, including, when looking after you just query the database, without actually deep in the file system. Among the general distribution, the establishment of a database are in crontab automatically.
1.3 grep command
Linux grep command is used to find files that match the criteria string. grep can also find files by file name, but is generally used to find content within the file.
grep (global search regular expression (RE ) and print out the line, comprehensive search regular expression and print out the line) is a powerful text search tool, you can use a regular expression search text, and print the matching lines come out.
1.4 whereis command
whereis command can only be used to search the program name, and only search binary files (parameter -b), man documentation (parameter -m) and source code files (parameter -s). If the parameter is omitted, all the information is returned.
1.5 which command
Action which command is specified in the PATH variable path, searches the position of a system command, and returns a search result first. In other words, the use of which command, you can see whether there is a system command, and in the end which is a position of command execution.
1.6 type command
In fact, type command can not be considered the Find command, which is used to distinguish a command in the end is a shell built-in, or provided by an external shell of independent binary files. If a command is an external command, then use the parameter -p path displays the command, which command corresponds.
1.7 summary
>
Which view the location of the executable file.
whereis view the file.
locate with the database to view the file location.
The actual search queries find the hard disk file name.
type distinction command type
>
2. find command Use Cases
2.1 locate the file name
Find zcwyou.txt this file from the beginning to find / that look at all mounted partitions and Linux directories.
[root@zcwyou ~]# find / -name zcwyou.txt
Finding just the end of .txt files
[root@zcwyou ~]# find / -name '*.txt'
Find txt and pdf files ending
[root@zcwyou ~]# find . -name "*.txt" -o -name "*.pdf"
2.2 file from one directory to start looking
From the current user's home directory Find
[root@zcwyou ~]# find ~ -name zcwyou.txt
From the current directory lookup
[root@zcwyou ~]# find . -name zcwyou.txt
From / var directory Find
[root@zcwyou ~]# find /var -name zcwyou.txt
2.3 ignore case
Find files with the file name abc, ignoring the case
with the option -iname
[root@zcwyou ~]# find / -iname \*abc\*
2.4 Find by file type
Use option -type
find a certain type of file, such as:
b - block device file.
d - directory.
c - a character device file.
p - pipe file.
l - a symbolic link file.
f - regular file.
Find all directories under the / etc directory and print it out
[root@zcwyou ~]# find /etc -type d -print
Find all types of files except the directory in the current directory
[root@zcwyou ~]# find . ! -type d -print
Find all symbolic link file in the / etc directory
[root@zcwyou ~]# find /etc -type l -print
2.5 depth directory-based search
Find the current directory and down the maximum depth limit for all file 3
[root@zcwyou ~]# find . -maxdepth 3 -type f
2.6 Basic Documents Find
Using the format:
Find time -type F type.
Each file UNIX / Linux file system there are three types of time:
Access time (-atime / day, -amin / min): Last user access time.
Modified (-mtime / day, -mmin / min): the file was last modified time.
Change Time (-ctime / day, -cmin / min): the last file data element (e.g. permissions) modification time.
Been visited in the last seven days search all files
[root@zcwyou ~]# find . -type f -atime -7
Search happens to be visited during the first seven days of all documents
[root@zcwyou ~]# find . -type f -atime 7
It has been visited by more than seven days in the search for all files
[root@zcwyou ~]# find . -type f -atime +7
Search over 10 minutes of access time of all files
[root@zcwyou ~]# find . -type f -amin +10
Find all files modified time longer than file.log
[root@zcwyou ~]# find . -type f -newer file.log
Find 2.7 based on file size
Use the format:
. The -type f -size the Find File Size
File Size Unit:
B - block (512 bytes)
C-- byte
w-- word (2 bytes)
K-- kilobytes
M-- megabytes
G-- gigabytes
Find files larger than 10KB of
[root@zcwyou ~]# find . -type f -size +10k
Find the file less than 30M
[root@zcwyou ~]# find . -type f -size -30M
Search equal to 55MB of files
[root@zcwyou ~]# find . -type f -size 55M
Find deleted after 2.8
Delete all files in the current directory .test
[root@zcwyou ~]# find . -type f -name "*.test" -delete
2.9 based on matching file permissions / ownership
Search the current directory permissions to 755 files
[root@zcwyou ~]# find . -type f -perm 755
Find out the current directory is not a php file permissions 600
[root@zcwyou ~]# find . -type f -name "*.php" ! -perm 600
Directory to find all the files currently owned by the user zcwyou
[root@zcwyou ~]# find . -type f -user zcwyou
Find all files in the current directory owned by user group zcwyou
[root@zcwyou ~]# find . -type f -group zcwyou
3. find in conjunction with other tools
With 3.1 -exec option in conjunction with other command
Find out the root of all files under the current directory, and change the ownership to the user zcwyou
[root@zcwyou ~]# find .-type f -user root -exec chown zcwyou {} \;
{} Used in combination with the use of -exec option to match all of the files, and then is replaced by the corresponding file name.
Find all .txt files in your home directory and delete
[root@zcwyou ~]# find $HOME/. -name "*.txt" -ok rm {} \;
The example in -ok and behavior as -exec, but it will prompt you whether appropriate action.
3.2 in conjunction with xargs
When using find -exec command options to deal with the matching file, find the command passed along to all matching files to execute exec. However, some systems have limitations on the length of the command can be transmitted to the exec, so after the find command to run a few minutes, an overflow error occurs. Error message is usually "argument list too long" or "parameter column overflow." This is useful where the xargs command, especially when combined with the find command.
find command to match the file transfer command to the xargs, and xargs command each get only part of the file, but not all, unlike the -exec option that. So that it can first deal with the first part of the file retrieved, and then the next batch, and so continue.
In some systems, using -exec option to process each matching file to initiate a corresponding process is not matched to the file all at once executed as a parameter; so in some cases there will be too many processes, system performance degradation problems, which is not efficient;
The xargs command using only one process. In addition, when using the xargs command, whether it is the time to get all the parameters, or in batches to obtain parameters, acquisition parameters and the number of each of the options will be determined based on the command and the corresponding kernel tunable parameters.
Take a look at how xargs command is used with the find command, and give some examples.
Find every ordinary file system, and then use the xargs command to test what kind of file they belong
[root@zcwyou ~]# find . -type f -print | xargs file
Find a memory dump file (core dump) throughout the system, and then save the results to /tmp/core.log file:
[root@zcwyou ~]# find / -name "core" -print | xargs echo "" >/tmp/core.log
With the grep command to search for the word hostname in all common file
[root@zcwyou ~]# find . -type f -print | xargs grep "hostname"
Delete all the stuff under the current directory older than 30 days
[root@zcwyou ~]# find . -ctime +30 -exec rm -rf {} \;
or
[root@zcwyou ~]# find ./ -mtime +30 -print|xargs rm -f -r
Delete files with zero size files
[root@zcwyou ~]# find ./ -size 0 | xargs rm -f &
use the find command with exec and xargs allows users to perform almost all of the command to match the file.
4. locate command
locate command is actually "find -name" is another way, but much faster than the latter, because it does not search for a specific directory, but searching a database, the database location CentOS7 in / var / lib / locatedb, CentOS6 database location file contains all the local information /var/lib/mlocate/mlocate.db, this database. Linux system to automatically create the database, automatic updates by default once a day, so use the locate command file finding out the latest changes before. To avoid this, before you can use locate, first use the updatedb command manually update the database.
CentOS7.5 to minimize the installation, for example, by default, the system is not integrated tools to install it manually:
Installation locate tool:
[root@zcwyou ~]# yum -y install mlocate
看到以下输出表明安装成功:
Total download size: 113 k
Installed size: 379 k
Downloading packages:
mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mlocate-0.26-8.el7.x86_64 1/1
Verifying : mlocate-0.26-8.el7.x86_64 1/1
Installed:
mlocate.x86_64 0:0.26-8.el7
Complete!
Update the database, that system all the file information into the database /var/lib/mlocate/mlocate.db
[root@zcwyou ~]# updatedb
View the files as .txt ending:
[root@zcwyou ~]# locate *.txt
Output:
/root/abc.txt
/root/cisco1.txt
/root/cisco2.txt
/root/compress.txt
/root/cp1.txt
/root/cp2.txt
/root/cut.txt
/root/cut2.txt
/root/diff1.txt
/root/diff2.txt
/root/test.txt
/root/zcwyou.txt
5. grep command to find text
Find 5.1 based on content
Show all files that start with d rows to include in the test.
[root@zcwyou ~]# grep 'test' d*
Display matching lines test in aa, bb, cc file.
[root@zcwyou ~]# grep 'test' aa bb cc
Displays the lines of strings to at least 5 contiguous string of lower case characters
[root@zcwyou ~]# grep ‘[a-z]\{5\}’ aa
Display / usr / files (sub-folders) src directory contains rows of test
[root@zcwyou ~]# grep test /usr/src
Display / usr / src file in the directory (including subdirectories) contains the line test
[root@zcwyou ~]# grep -r test /usr/src
Zcwyou.txt entire word lookup file, rather than part of the string (such as matching 'test', rather than 'tester123'),
[root@zcwyou ~]# grep -w test zcwyou.txt
Case-insensitive search. By default case sensitive
[root@zcwyou ~]# grep -i test zcwyou.txt
Find keyword test, lists of filenames including a test to find in the current directory
[root@zcwyou ~]# grep -l test *.txt
Find keyword test, lists the file name does not include a test to find in the current directory
[root@zcwyou ~]# grep -L test *.txt
5.2 recursively find qualified file
In / var / log and its subdirectories to find files with the shutdown keyword
[root@zcwyou ~]# grep -r shutdown /var/log
5.3 Reverse Lookup
By "-v" argument can print out the contents do not meet the conditions of the line.
Find a line in the file filename contains test is not included in the test, this time using the command:
[root@zcwyou ~]# grep -v test *test*
6. whereis command
whereis command can only be used to search the program name, and only search binary files (parameter -b), man documentation (parameter -m) and source code files (parameter -s). If the parameter is omitted, all the information is returned.
Example whereis command:
Location pwd command procedures and documentation for all locations where
[root@zcwyou ~]# whereis pwd
Output Results:
pwd: / usr / bin / pwd /usr/share/man/man1/pwd.1.gz
7. which command
Action which command is specified in the PATH variable path, searches the position of a system command, and returns a search result first. In other words, the use of which command, you can see whether there is a system command, and in the end which is a position of command execution.
[root@zcwyou ~]# which wget
Output:
/ usr / bin / wget
8. type the command
Find a location wget program
[root@zcwyou ~]# type wget
Output Results:
wget IS / usr / bin / wget
Find du program position, use the option -p, which is equivalent to the command
[root@zcwyou ~]# type -p du
Output of
/ usr / bin / du
Find a location cd program appears as builtin, Linux is the built-in command
[root@zcwyou ~]# type cd
The results showed that output, cd command is a command system integration.
cd is a shell builtin
Ll find the location of the program
[root@zcwyou ~]# type ll
The output represents, it ll be ls -l alias.
ll is aliased to `ls -l –color=auto’