In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "sharing 10 methods and skills of using find commands in Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Match search based on file name or regular expression
The option-name parameter specifies the string that the file name must match, we can use wildcards as parameters, and "* .txt" matches all file names that end in .txt.
The code is as follows:
[root@localhost test] # touch {data,log,file,File,LOG} _ {1,2,3,4,5,6} _ {.txt, .pdf, .log, .conf}
[root@localhost test] # find. -name "* .txt"-print
If you want to match one of multiple conditions, you can use the-o parameter.
The code is as follows:
[root@localhost test] # find. \ (- name "* .txt"-o-name "* .log"\)
Option-iname ignores letter case
Option-the parameters of path can use wildcards to match file paths or files.
2. Negative parameters
Find uses "!" To negate the parameter, matching all file names that do not end in .txt.
The code is as follows:
[root@localhost test] # find. !-name "* .txt"-print
3. Search based on directory depth
The find command traverses all subdirectories when used, and we can use-maxdepth and-mindepth to limit the depth of traversal by the find command.
-maxdepth: specify the maximum depth
-mindepth: specify the minimum depth.
The code is as follows:
[root@localhost] # find. -maxdepth 1-type f
Lists all ordinary files in the current directory, with these two commands immediately following the target path.
4. Search by file type
The code is as follows:
Find. -type d-print
File type parameters ordinary file f symbol file l directory d character device c block device b socket sfifop
5. Search according to the file time
Each file in the Linux file system has three timestamps
Access time (- atime): the last time the user accessed the file
Modification time (- mtime): the time when the content of the file was last modified.
Change time (- ctime): the time when the file metadata (metadata, such as permissions or ownership) last changed.
-atime,-mtime,-ctime is used as a time parameter in days. You can use + to indicate greater than, and-to indicate less than.
The code is as follows:
[root@localhost] # find. -type f-atime 7
# print out the files that were accessed exactly 7 days ago
[root@localhost] # find. -type f-mtime + 7
# print files with a modification time of more than 7 days
[root@localhost] # find. -type f-ctime-7
# print out files with a modification time of less than 7 days
Similar parameters include-amin (access time),-mmin (modification time), and-cmin (change time), in minutes.
Another beautiful feature of find is the-newer parameter. We can specify a parameter file for comparing timestamps, and then find all the files that are newer than the parameter file.
Find. -type f-newer file.txt
P find all files in the current directory that take longer to modify than file.txt.
6. Search based on file size
Search for available units
BMY-block (512 bytes); CME-byte; WMY-word (2 byte)
KMY-kilobytes; MMY-megabytes; Gmure-gigabytes.
The code is as follows:
[root@localhost tmp] # find. -type f-size 2k
# files equal to 2k
[root@localhost tmp] # find. -type f-size + 2k
# Files greater than 2k
[root@localhost tmp] # find. -type f-size-2k
# Files less than 2k
7. Delete matching files
-delete can be used to delete matching files found by find.
The code is as follows:
[root@localhost tmp] # find. -type f-name ".sWp"-delete
# Delete all .swp files in the current directory
8. Matching based on file permissions and ownership
The code is as follows:
[root@localhost tmp] # find. -type f-perm 644
# find files with 644 permissions in the current directory
[root@localhost tmp] # find. -type f-user reed
# find the files whose current directory file owner is reed
9. Execute commands or actions in conjunction with find
The find command can be combined with other commands with the option-exec.
The code is as follows:
[root@localhost tmp] # find. -type f-user reed-exec chown cathy {}\
# change the current directory file owned by reed to cathy
{} is a special string, and for each matching file, {} is replaced with the corresponding file name.
The code is as follows:
[root@localhost test] # find. -type f-mtime + 10-name "* .log"-exec cp {} / data/bk_log\
# copy the log files of the current directory greater than 10 days to the / data/bk_log directory
[root@localhost test] # find / tmp/test/-type f-name "* .txt"-exec printf "Text file:% s\ n" {}\
Text file: / tmp/test/File_6_.txt
Text file: / tmp/test/file_4_.txt
Text file: / tmp/test/data_3_.txt
Text file: / tmp/test/data_1_.txt
# list all txt files of the directory
10. Skip the specified directory
When we have time to search, we need to skip some subdirectories.
The code is as follows:
[root@localhost test] # find. \ (- name "jump_dir"-prune\)-o\ (- type f-print\)
#\ (- name "jump_dir"-prune\) specifies the name of the subdirectory to skip
This is the end of the content of "sharing 10 ways to use find commands in Linux system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.