Ubuntu system file and content SEARCH Command summary, ubuntu Content Search
This article summarizes the file and content search commands in ubuntu, such as the find, locate, which, grep, and other file and content search commands, for more information, see article 1. search by File Name:
1. find command
Because find has powerful functions, there are many options, and most of them are worth the time to understand. Even if the system contains a Network File System (NFS), the find command is equally valid in the file system, and you only have the corresponding permissions.
When running a find command that consumes a lot of resources, many people tend to put it in the background for execution, because it may take a long time to traverse a large file system (this is a file system with more than 30 GB bytes ).
The common form of the find command is;
Find pathname-options [-print-exec-OK...]
For example
Find/-name "filename"
Purpose: To search for a file named filename in the root directory "/". The "filename" file name can contain wildcards (*,?), Note: filename is a file name string, which can contain double quotation marks or not. It has many options for you to search for files in different ways, such as date and file size, permission, owner, and so on.
Parameters of the find command;
Pathname: directory path searched by the find command. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system.
-Print: The find command outputs matching files to the standard output.
-Exec: The find command executes the shell command given by this parameter on the matching file. The corresponding command is in the form of 'command' {};. Note the space between {} And.
-OK: The Role of-exec is the same, but the shell command given by this parameter is executed in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute.
3. find Command Options
-Name
Search for files by file name.
-Perm
Find the file according to the file permission.
-Prune
This option can make the find command not to be searched in the specified directory. If the-depth option is used at the same time,-prune will be ignored by the find command.
-User
Search for files by file owner.
-Group
Find the file according to the file group.
-Mtime-n + n
Find the file according to the file change time.-n indicates that the file change time is less than n days from now, and + n indicates that the file change time is earlier than n days from now. The find command also has the-atime and-ctime options, but they both have the-m time options.
-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 a file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.
-Newer file1! File2
Find the file whose modification time is newer than file1 but earlier than file2.
-Type
Find a type of file, such:
B-block device files.
D-directory.
C-character device file.
P-MPs queue file.
L-Symbolic Link file.
F-common file.
-Size n: [c] searches for files with a length of n blocks. If a file contains c, the file length is measured in bytes.
-Depth: when searching for a file, first find the file in the current directory and then find it in its subdirectory.
-Fstype: searches for files in a certain type of file system. These file system types can usually be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system.
-Mount: the mount point of the file system is not crossed during file search.
-Follow: If the find command encounters a symbolic link file, it will trace the file to which the link points.
-Cpio: Use the cpio command to back up the files to the tape device.
2. locate command
Locate filename
The locate command is actually another method of writing "find-name", but it is much faster than the latter because it does not search for a specific directory, instead, search for a database (/var/lib/locatedb) that 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 problem, you can use the updatedb command to manually update the database before using locate.
Use instance of the locate command:
$ Locate/etc/sh
Search for all files starting with sh in the etc directory.
$ Locate ~ /M
Search all files starting with m in the user's home directory.
$ Locate-I ~ /M
Search all files starting with m in the user's home directory, and ignore the case.
The file name containing the string "filename" is found. This is easier than the find command. But it is based on the database (usually rebuilt at night), so you cannot find the file just saved to the file system. To force immediate database update, you can use: updatedb & (no space in the middle) as a Super User)
3. which command
Which executeable_name
Find the executable file according to the file name of the executable file.
For example, if which apache2 is returned,/usr/sbin/apache2 is returned.
2. Search by File Content
1. grep-n string name/filepath/filename
Returns the row containing the string, which can be multiple rows. And contains the number of rows.
2. sudo gedit/filepath/filename
Then, use ctrl + F to find the corresponding string.
3. The vi or less command can find the corresponding content
For example, vi/filepath/filename. Then, enter "/string" and press the letter "n" to the next matched string.
4. tail command
Special methods for viewing File Content
1. If you only want to view the first five lines of the file, you can use the head command, for example:
Head-5/etc/passwd
2. If you want to view the last 10 lines of the file, you can use the tail command, such:
Tail-20/etc/passwd
Tail-f/var/log/messages
Parameter-f enables tail to constantly read the latest content, so as to monitor the content in real time.
Tail-f/var/log/messages
Press Ctrl + C, directly exit from the script to the prompt the article Reprinted from the http://www.jb51.net/ OS /Ubuntu/62771.html