In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the commonly used find commands in Linux, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
First, test the following find command example by creating the following empty file under your home directory.
# vim create_sample_files.sh touch MybashProgram.sh touch mycprogram.c touch MyCProgram.c touch Program.c mkdir backup cd backup touch MybashProgram.sh touch mycprogram.c touch MyCProgram.c touch Program.c # chmod + x create_sample_files.sh #. / create_sample_files.sh # ls-R.: backup MybashProgram.sh MyCProgram.c create_sample_files.sh mycprogram.c Program.c. / backup: MybashProgram.sh mycprogram.c MyCProgram.c Program.c
1. Find a file with a file name
This is a basic use of the find command. The following example shows how to use MyCProgram.c as the lookup name to find files in the current directory and its subdirectories.
# find-name "MyCProgram.c". / backup/MyCProgram.c. / MyCProgram.c
two。 Find a file with a file name, ignoring case
This is a basic use of the find command. The following example shows how to use MyCProgram.c as the lookup name to find files in the current directory and its subdirectories, ignoring case.
# find-iname "MyCProgram.c". / mycprogram.c. / backup/mycprogram.c. / backup/MyCProgram.c. / MyCProgram.c
3. Use mindepth and maxdepth to limit the depth of the search for the specified directory
Look for the passwd file in the root directory and its subdirectories.
# find /-name passwd. / usr/share/doc/nss_ldap-253/pam.d/passwd. / usr/bin/passwd. / etc/pam.d/passwd. / etc/passwd
Look for passwd in the root directory and its 1-tier deep subdirectory. (for example, root-level 1, and one sub-directory-level 2)
# find-maxdepth 2-name passwd. / etc/passwd
Look for the passwd file in the root directory and its two-tier deep subdirectory. (e.g. root-level 1, and two sub-directories-level 2 and 3)
# find /-maxdepth 3-name passwd. / usr/bin/passwd. / etc/pam.d/passwd. / etc/passwd
Look for the passwd file between the layer 2 subdirectory and the layer 4 subdirectory.
# find-mindepth 3-maxdepth 5-name passwd. / usr/bin/passwd. / etc/pam.d/passwd
4. Execute the command on the file found by the find command
The following example shows the find command to calculate the MD5 validation and for all case-insensitive files named "MyCProgram.c". {} will be replaced by the current file name.
Find-iname "MyCProgram.c"-exec md5sum {}\; d41d8cd98f00b204e9800998ecf8427e. / mycprogram.c d41d8cd98f00b204e9800998ecf8427e. / backup/mycprogram.c d41d8cd98f00b204e9800998ecf8427e. / backup/MyCProgram.c d41d8cd98f00b204e9800998ecf8427e. / MyCProgram.c
5. Opposite matching
Displays all files or directories whose names are not MyCProgram.c. Because maxdepth is 1, only the files and directories in the current directory are displayed.
Find-maxdepth 1-not-iname "MyCProgram.c". . / MybashProgram.sh. / create_sample_files.sh. / backup. / Program.c
6. Use inode numbers to find files
Any file has a * inode number, so we can distinguish between files. Create two files with similar names, such as one with a space ending and one without.
Touch "test-file-name" # touch "test-file-name" [Note: There is a space at the end] # ls-1 test* test-file-name test-file-name
The output from ls cannot tell which file has a space ending. Using the option-I, you can see the inode number of the file, thus distinguishing the two files.
Ls-i1 test* 16187429 test-file-name 16187430 test-file-name
You can specify the inode number in the find command as shown below. Here, the find command renames a file with an inode number.
Find-inum 16187430-exec mv {} new-test-file-name\; # ls-i1 * test* 16187430 new-test-file-name 16187429 test-file-name
You can use this technique when you want to do something with poorly named files like the one above. For example, a file name named file?.txt has a special character in it. If you want to execute "rm file?.txt", all three files shown below will be deleted. So, use the following steps to delete the "file?.txt" file.
Ls file1.txt file2.txt file?.txt
Find the inode number of each file.
Ls-i1 804178 file1.txt 804179 file2.txt 804180 file?.txt
As follows: use inode numbers to delete file names with special symbols.
Find-inum 804180-exec rm {}\; # ls file1.txt file2.txt [Note: The file with name "file?.txt" is now removed]
7. Find files based on file permissions
The following operations are reasonable:
Find a file with the specified permissions
Ignore other permission bits and check whether they match the specified permissions
Search based on the permissions of a given octal / symbolic expression
In this example, assume that the directory contains the following files. Note that the permissions of these files are different.
Ls-l total 0-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read-1 root root 0 2009-02-19 20:31 no_for_all-rw- 1 root root 0 2009-02-19 20:29 ordinary_file-rw-r- 1 root root 0 2009-02-19 20:27 Others_can_also_read-r-1 root root 0 2009-02-19 20:27 others_can_only_read
Find the file with group read permissions. Use the following command to find a file in the current directory that has read permissions for the same group of users, ignoring other permissions for that file.
Find. -perm-grubr-type f-exec ls-l {}\;-rw-r--r-- 1 root root 0 2009-02-19 20:30. / everybody_read-rwxrwxrwx 1 root root 0 2009-02-19 20:31. / all_for_all-r-1 root root 0 2009-02-19 20:27. / others_can_only_read-rw-r- 1 root root 0 2009-02-19 20:27. / others_can_also_read
Locate the file that has read-only access to the group user.
Find. -perm grubr-type f-exec ls-l {}\;-r-1 root root 0 2009-02-19 20:27 / others_can_only_read
Locate the file that has read-only access to the group users (in the form of octal permissions).
Find. -perm 040-type f-exec ls-l {}\;-r-1 root root 0 2009-02-19 20:27 / others_can_only_read
8. Find all empty files (0-byte files) in the home directory and subdirectories
Most of the output files of the following command are place hoders created by other programs in the locked file box.
Find ~-empty
List only the empty files in your home directory.
Find. -maxdepth 1-empty
Only non-hidden empty files in the current year's directory are listed.
Find. -maxdepth 1-empty-not-name ".
9. Find 5 * * files
The following command lists 5 * * files in the current directory and subdirectories. This will take some time, depending on the number of files that the command needs to process.
Find. -type f-exec ls-s {}\; | sort-n-r | head-5
10. Find the 5 smallest files
The method is similar to finding five * * files, except that the order of the sort is descending.
Find. -type f-exec ls-s {}\; | sort-n | head-5
In the above command, it is likely that all you see is an empty file (0-byte file). In this way, you can use the following command to list the smallest files instead of 0 bytes.
Find. -not-empty-type f-exec ls-s {}\; | sort-n | head-5
11. Use-type to find files of the specified file type
Find only the socket file:
Find. -type s
Find all the directories:
Find. -type d
Find all general files:
Find. -type f
Find all hidden files:
Find. -type f-name ". *"
Find all hidden directories:
Find-type d-name ".
twelve。 Find the file by comparing the modification time with other files
Displays the files that made changes after the file was specified. The following find command displays all files that were created and modified after ordinary_file.
Ls-lrt total 0-rw-r- 1 root root 0 2009-02-19 20:27 others_can_also_read-r-1 root root 0 2009-02-19 20:27 others_can_only_read-rw- 1 root root 0 2009-02-19 20:29 ordinary_file-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read-rwxrwxrwx 1 root root 0 2009-02- 19 20:31 all_for_all-1 root root 0 2009-02-19 20:31 no_for_all # find-newer ordinary_file. . / everybody_read. / all_for_all. / no_for_all
13. Find files by file size
Use the-size option to find files by file size.
Find a file larger than the specified file:
Find ~-size + 100m
Find a file smaller than the specified file:
Find ~-size-100m
Find a file that matches the given size:
Find ~-size 100m
Note:-the finger is smaller than the given size and the + finger is larger than the given size. No symbol represents exactly the same size as a given size.
14. Alias common find operations
If you find something useful, you can give it an alias. And do it wherever you want.
Commonly used to delete a.out files.
Alias rmao= "find.-iname a.out-exec rm {}\;" # rmao
Delete the core file generated by the c program.
Alias rmc= "find.-iname core-exec rm {}\;" # rmc
15. Delete large package files with the find command
The following command deletes * .zip files greater than 100m.
Find /-type f-name * .zip-size + 100m-exec rm-I {}\; "
Delete all heavy rain 100m * .tar files with alias rm100m. Using the same idea, you can create a class of aliases for rm1g,rm2g,rm5g to delete all files larger than 1G, 2G, and 5G.
Alias rm100m= "find /-type f-name *. Tar-size + 100m-exec rm-I {}\;" # alias rm1g= "find /-type f-name *. Tar-size + 1G-exec rm-I {}\;" # alias rm2g= "find /-type f-name *. Tar-size + 2G-exec rm-I {}\;" # alias rm5g= "find /-type f-name *. Tar-size + 5G-exec rm-I {}\ "# rm100m # rm1g # rm2g # rm5g Thank you for reading this article carefully. I hope the article" what are the find commands commonly used in Linux "shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
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.