In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to find files in the Linux system, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Linux lookup file
1.1 find command
The Linux find command is the most useful and confusing of all Linux commands. It is difficult because its syntax is different from the standard syntax of other Linux commands. However, it is powerful because it allows you to find files by file name, file type, user, or even a timestamp. Using the find command, you can not only find a file with any combination of these attributes, but also perform operations on the files it finds.
1.2 locate command
The Linux locate command is used to find documents that meet the criteria, and it goes to the database where the document and directory names are saved to find documents or directories that meet the criteria of the template style. Locate allows users to quickly search for specified files in the file system. The method is to first establish a database that includes all the file names and paths in the system, and then when looking for it, you only need to query the database without actually going deep into the file system. In the general distribution, the establishment of the database is automatically executed in crontab.
1.3 grep command
The Linux grep command is used to find strings that match the criteria in the file. Grep can also look up files by file name, but it is generally used to find the contents of the file.
Grep (global search regular _ expression (RE) and print out the line, full search for regular expressions and printing lines) is a powerful text search tool that uses regular expressions to search for text and print matching lines.
1.4 whereis command
The whereis command can only be used to search for program names, and only binary files (parameter-b), man description files (parameter-m), and source code files (parameter-s) are searched. If the parameter is omitted, all information is returned.
1.5 which command
The which command searches for the location of a system command in the path specified by the PATH variable and returns * search results. That is, using the which command, you can see whether a system command exists and where the command is being executed.
1.6 type command
The type command is not really a lookup command, it is used to tell whether a command is native to shell or provided by a separate binary outside of shell. If a command is an external command, using the-p argument displays the path to the command, which is equivalent to the which command.
1.7 Summary
Which looks at the location of the executable file.
Whereis views the location of the file.
Locate works with the database to view the file location.
Find actually searches the hard disk to query the file name.
Type distinguishes command types
2. Find command use case
2.1 find by file name
Look for the file zcwyou.txt, starting with /, that is, in all the mounted partitions and directories of Linux.
[root@zcwyou ~] # find /-name zcwyou.txt
Only find files at the end of .txt
[root@zcwyou ~] # find /-name'* .txt'
Find files at the end of txt and pdf
[root@zcwyou] # find. -name "* .txt"-o-name "* .pdf"
2.2 start looking for files from a directory
Start with the current user's home directory
[root@zcwyou] # find ~-name zcwyou.txt
Start with the current directory
[root@zcwyou] # find. -name zcwyou.txt
Start with the / var directory
[root@zcwyou ~] # find / var-name zcwyou.txt
2.3 ignore case
Find files with abc filenames, ignoring case
Use options-iname
[root@zcwyou ~] # find /-iname * abc*
2.4 find by file type
Use options-type
Find a certain type of file, for example:
B-block device file.
D-directory.
C-character device file.
P-pipe file.
L-symbolic link file.
F-ordinary file.
Find all the directories in the / etc directory and print them 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 files in the / etc directory
[root@zcwyou] # find. !-type d-print
[root@zcwyou] # find. !-type d-print
2.5 search based on directory depth
Find the current directory and all files with a downward depth limit of 3
[root@zcwyou] # find. -maxdepth 3-type f
2.6 basic file time lookup
Use format:
Find. -type f time type
The UNIX/Linux file system has three time types for each file:
Access time (- atime/ days,-amin/ minutes): the last time the user visited.
Modification time (- mtime/ days,-mmin/ minutes): one modification time for the file *.
Change time (- ctime/ days,-cmin/ minutes): file data elements (such as permissions, etc.) * a modification time.
Search for all files accessed in the last seven days
[root@zcwyou] # find. -type f-atime-7
Search for all files that were accessed exactly seven days ago
[root@zcwyou] # find. -type f-atime 7
Search all files that have been accessed in more than seven days
[root@zcwyou] # find. -type f-atime + 7
Search all files that have been accessed for more than 10 minutes
[root@zcwyou] # find. -type f-amin + 10
Find all the files that take longer to modify than file.log
[root@zcwyou] # find. -type f-newer file.log
2.7 find by file size
Use format:
Find. -type f-size file size
File size unit:
B-block (512 bytes)
C-byte
W-word (2 bytes)
K-kilobytes
M-megabyte
G-gigabyte
Find files larger than 10KB
[root@zcwyou] # find. -type f-size + 10k
Find files less than 30m
[root@zcwyou] # find. -type f-size-30m
Search for files equal to 55MB
[root@zcwyou] # find. -type f-size 55m
2.8 delete after lookup
Delete all .test files in the current directory
[root@zcwyou] # find. -type f-name "* .test"-delete
2.9 match based on file permissions / ownership
Search for files with permissions of 755 in the current directory
[root@zcwyou] # find. -type f-perm 755
Find out the php files in the current directory with permissions other than 600,
[root@zcwyou] # find. -type f-name "* .php"!-perm 600
Find out all the files owned by the current directory user zcwyou
[root@zcwyou] # find. -type f-user zcwyou
Find out all the files owned by the current directory user group zcwyou
[root@zcwyou] # find. -type f-group zcwyou
3. Find combines with other tools
3.1 use the-exec option in conjunction with other commands
Find all the root files in the current directory and change the ownership to the user zcwyou
[root@zcwyou] # find.-type f-user root-exec chown zcwyou {}
{} is used in conjunction with the-exec option to match all files, which is then replaced with the appropriate file name.
Find all the .txt files in your home directory and delete them
[root@zcwyou ~] # find $HOME/. -name "* .txt"-ok rm {}
The-ok behavior in the example is the same as the-exec behavior, but it gives a hint as to whether to perform the appropriate action.
3.2 combine with xargs
When processing matched files using the-exec option of the find command, the find command passes all matched files together to exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error occurs a few minutes after the find command is run. The error message is usually "Parameter column too long" or "Parameter column overflow". This is what the xargs command is for, especially when used with the find command.
The find command passes the matching files to the xargs command, while the xargs command fetches only some files at a time, not all of them, unlike the-exec option. In this way, it can first process some of the files obtained by *, then the next batch, and so on.
In some systems, using the-exec option will initiate a corresponding process to process each matched file, instead of executing all the matched files as parameters at once; in some cases, there will be too many processes and poor system performance, so the efficiency is not high.
There is only one process using the xargs command. In addition, when using the xargs command, whether to get all the parameters at once or in batches, and the number of parameters each time will be determined according to the options of the command and the corresponding adjustable parameters in the system kernel.
Take a look at how the xargs command works with the find command, and give some examples.
Find every ordinary file in the system, and then use the xargs command to test which type of file they belong to.
[root@zcwyou] # find. -type f-print | xargs file
Look for the memory information dump file (core dump) throughout the system, and save the results to the / tmp/core.log file:
[root@zcwyou ~] # find /-name "core"-print | xargs echo "" > / tmp/core.log
Use the grep command to search all ordinary files for the word hostname
[root@zcwyou] # find. -type f-print | xargs grep "hostname"
Delete everything from 30 days ago in the current directory
[root@zcwyou] # find. -ctime + 30-exec rm-rf {}
Or
[root@zcwyou ~] # find. /-mtime + 30-print | xargs rm-f-r
Delete files with zero file size
[root@zcwyou ~] # find. /-size 0 | xargs rm-f &
The find command in conjunction with exec and xargs enables the user to execute almost all commands on the matched file.
4. Locate command
The locate command is actually another way to write "find-name", but much faster because it does not search for a specific directory, but searches for a database where the CentOS7 database is located in the / var/lib/locatedb,CentOS6 database location in / var/lib/mlocate/mlocate.db, which contains all the local file information. The Linux system automatically creates this database and automatically updates it every day by default, so you cannot find the changed files by using the locate command. To avoid this, you can manually update the database using the updatedb command before using locate.
Take the minimized installation of CentOS7.5 as an example, by default, the system does not integrate the tool, and you need to install it manually:
Install the locate tool:
[root@zcwyou ~] # yum-y install mlocate
The following output indicates that the installation was successful:
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 Compact 1
Verifying: mlocate-0.26-8.el7.x86_64 1 Compact 1
Installed:
Mlocate.x86_64 0RO 0.26-8.el7
Complete!
Update the database, that is, write all the file information in the system to the database / var/lib/mlocate/mlocate.db
[root@zcwyou ~] # updatedb
View files that end in .txt:
[root@zcwyou] # locate * .txt
Output result:
/ 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 content
5.1 find based on content
Displays lines that contain test in all files that begin with d.
[root@zcwyou ~] # grep 'test' d*
Displays the lines that match the test in the aa,bb,cc file.
[root@zcwyou ~] # grep 'test' aa bb cc
Displays all lines of strings that contain at least five consecutive lowercase characters per string
[root@zcwyou ~] # grep'[a murz] {5}'aa
Show the files in the / usr/src directory (excluding subdirectories) that contain lines of test
[root@zcwyou ~] # grep test / usr/src
Show the files in the / usr/src directory (including subdirectories) that contain lines of test
[root@zcwyou] # grep-r test / usr/src
Look for the whole word in the zcwyou.txt file, not part of the string (such as matching 'test', instead of' tester123')
[root@zcwyou ~] # grep-w test zcwyou.txt
Search case-insensitive. The default is case sensitive
[root@zcwyou] # grep-I test zcwyou.txt
Find the keyword test, list the file names including test, and find them in the current directory
[root@zcwyou] # grep-l test * .txt
Find the keyword test, list the file names that do not include test, and find them in the current directory
[root@zcwyou] # grep-L test * .txt
5.2 find files that meet the criteria recursively
In / var/log and its subdirectories, look for the file with the shutdown keyword
[root@zcwyou] # grep-r shutdown / var/log
5.3 reverse lookup
The "- v" parameter allows you to print out the contents of lines that do not meet the criteria.
Look for lines that do not contain test in the file that contains test in the file name. In this case, the command used is:
[root@zcwyou] # grep-v test* test*
6. Whereis command
The whereis command can only be used to search for program names, and only binary files (parameter-b), man description files (parameter-m), and source code files (parameter-s) are searched. If the parameter is omitted, all information is returned.
Examples of use of the whereis command:
View all the locations of the pwd command program and the location of the description file
[root@zcwyou ~] # whereis pwd
Output result:
Pwd: / usr/bin/pwd / usr/share/man/man1/pwd.1.gz
7. Which command
The which command searches for the location of a system command in the path specified by the PATH variable and returns * search results. That is, using the which command, you can see whether a system command exists and where the command is being executed.
[root@zcwyou ~] # which wget
Output result:
/ usr/bin/wget
8. Type command
Find the location of the wget program
[root@zcwyou ~] # type wget
Output result:
Wget is / usr/bin/wget
Find the location of the du program, using the option-p, which is equivalent to the which command
[root@zcwyou] # type-p du
Output result
/ usr/bin/du
Find the location of the cd program and display it as builtin, which is the Linux built-in command
[root@zcwyou ~] # type cd
The output results show that the cd command is a system integration command.
Cd is a shell builtin
Find the location of the ll program
[root@zcwyou ~] # type ll
The output indicates that ll is an alias for ls-l.
Ll is aliased to `ls-l-color=auto'/
Https://www.linuxrumen.com/rmxx/176.html
The above is how to find files in the Linux system. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.