Five linux search commands, five linux commands
Five SEARCH Command Learning
Explain the five linux search commands: locate, find, which, whereis, and grep.
Locate
Explanation: From the man manual, we can see that the locate search is based on the file name, but only depends onUpdatedbCommand. This command updates the search database/var/lib/locatedb in linux.
This database contains information about all local files. The Linux system automatically creates the database and updates the database once a day. Therefore, the latest changed files cannot be found using the locate command. To avoid this situation, you can use
Use the updatedb command to manually update the database.
Find
Explanation: The find command searches for the given constraints in the file system and returns the search results.
Time-related options:-atime. -Ctime. -Mtime.
-Mtime-n + n: Find the file based on the file change time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Atime-n + n: searches for files based on the file access time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
-Ctime-n + n: Find the file based on the file creation time.-n indicates that the file is less than n days, and + n indicates that the file is earlier than n days.
Example:
-Newer file: file is an existing file that lists the file names that are newer than file.
-Amin-n: Find the files accessed in the last N minutes of the system.
-Atime-n: Find the last n * 24 hours of files in the system
-Cmin-n: Find the file whose status is changed in the last N minutes in the system.
-Ctime-n: Find the file whose status is changed in the last n * 24 hours in the system.
-Mmin-n: Find the file whose data is changed in the last N minutes in the system.
-Mtime-n: Find the file whose data has been changed for the last n * 24 hours in the system.
Conclusion: min is minute, time is hour, a is accessed, c is changed, and m is changed.
-Name: searches for files by file name
-Perm searches for Objects Based on file permissions.
-The user searches for files based on the file owner.
-Group according? Find the file in the group to which the file belongs .?
-Nogroup: Find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups.
-Nouser: Find the file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.
-Newer file1! File2 searches for files whose change time is newer than file1 but older than file2.
-Type: Find? One type? File
Example:
1. Find the directory named code under the current directory
Find.-name code-print
./Code
2. Find and delete files whose modification time is earlier than 5 days in the directory:
Find-atime + 5-exec rm {}\;
3. Search all current directories and sort them.
Find/-type d | sort
4. Find the directories whose names end with ". log"
Find/-type d-name "*. log"
5. Find the files accessed in the system in the last 5 days
Find-atime-5
6. Find the directory accessed in the system in the last 5 minutes
Find-amin-5
7. Search for all files in the current directory with 755 permissions and set it to 777
Find.-perm 755-exec chmod 777 {}\;
Grep
Explanation: grep searches for strings containing matching characters in the file, and supports regular expressions.
Grep [Option] [file]
Eg: grep "test *"./test.txt
Whersis
Explanation: Find executable programs, binary files, and other paths
Eg whereis ls
Which
Explanation: Find the path of an executable command.
Eg: which ls