A. Find by FileName:
1. Find command
Because find has a powerful feature, it has a lot of options, and most of the options are worth taking time to understand. Even if the system contains a network file system (NFS), the Find command is equally valid in the file system, only you have the appropriate permissions.
When running a very resource-consuming find command, many people tend to put it in the background because it can take a long time to traverse a large file system (this refers to a file system above 30G bytes).
The general form of the Find command is;
Find Pathname-options [-print-exec-ok ...]
Such as
Find/-name "filename"
Objective: To start searching the file called filename in the root directory "/", the filename can contain a wildcard character (*,?). Note: filename is a filename string, can be with double quotes, or without the find command powerful, it has many options to allow you to search for files in different ways, such as by date, file size, permissions, owners, and so on.
The parameters of the Find command;
The directory path that the Pathname:find command looks for. For example, to represent the current directory, with/to represent the system root directory.
The-print:find command outputs matching files to the standard output.
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {};, note the space between {} and;
-ok: The same as the-exec, but in a more secure mode to execute the shell command given by this parameter, before executing each command, will be prompted to let the user determine whether to execute.
3, Find command options
-name
Find files by file name.
-perm
Locate files by file permissions.
-prune
Use this option to make the Find command not look in the currently specified directory, and if you use the-depth option, then-prune will be ignored by the Find command.
-user
Locate files according to the owner of the file.
-group
Locate the file by the group to which the file belongs.
-mtime-n +n
To find a file by the time the file was changed-n indicates that the file change time is now n days, + n indicates that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they all and the-m time option.
-nogroup
Finds a file that does not have a valid owning group, that is, the group that the file belongs to does not exist in/etc/groups.
-nouser
Finds a file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.
-newer file1! File2
The Find change time is newer but older than the file File1 file file2.
-type
Find a type of file, such as:
B-block device files.
D-Directory.
C-Character device files.
P-pipe file.
L-Symbolic link file.
F-Normal files.
-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
-depth: When looking for a file, first look for the files in the current directory and then look in their subdirectories.
-fstype: Find files in a type of file system that can usually be found in the configuration file/etc/fstab, which contains information about the file system in this system.
-mount: Does not cross the file system mount point when locating files.
-follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.
-cpio: Use the cpio command for matching files to back up these files to a tape device.
2. Locate command
Locate filename
The locate command is another way of writing "Find-name", but it is much faster than the latter because it does not search for a specific directory, but rather searches a database (/var/lib/locatedb), which contains all of the local file information. The Linux system automatically creates this database and updates it automatically every day, so you won't be using the locate command to find the most recently changed files. To avoid this, you can use the UpdateDB command to manually update the database before using locate.
Usages of the Locate command:
$ locate/etc/sh
Search all files in the ETC directory that start with SH.
$ locate ~/m
Searches the user's home directory for all files beginning with M.
$ locate-i ~/m
Searches the user's home directory for all files that start with M and ignores case.
The name of the file containing the string "filename" was found. This is easier than find command. But based on the database (usually rebuilt at night), you can't find the file you just saved to the file system. To force immediate updating of the database, you can use:updatedb& as a superuser (no spaces in the middle)
3. Which command
Which Executeable_name
Locate the executable file, depending on the file name of the executable file.
For example which apache2, return to/usr/sbin/apache2
Two. Find the contents of the file
1. Grep-n String name/filepath/filename
Returns the line containing the string, which can be multiple lines. and contains the number of rows.
2. Sudo gedit/filepath/filename
Then, use Ctrl+f to find the appropriate string.
3. vi or less command to find the appropriate content
For example Vi/filepath/filename then, enter "/string", press the letter "n" to the next matching string
4. Tail command
Special ways to view the contents of a file
1. If you only want to see the first 5 lines of the file, you can use the Head command, such as:
Head-5/etc/passwd
2. If you want to view the following 10 lines of the file, you can use the tail command, such as:
tail-20/etc/passwd
Tail-f/var/log/messages
Parameter-F allows tail to keep reading the latest content, which has the effect of real-time monitoring
Tail-f/var/log/messages
After pressing CTRL + C, exit directly from the script to the prompt