Ubuntu search command list, ubuntu search command details, you can look at Ubuntu
1. search by file name:
1. find command
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.
2. locate command
Locate filename
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 and exit from the script to the prompt.
Search
----
Whereis <程序名称>
Find the software installation path
-B: only query binary files.
-M: only search for help files
-S: only find the source code
-U: exclude files of the specified type.
-F only displays file names
-B <目录> Search for binary files in a specified directory
-M <目录> Search for help files in the specified directory
-S <目录> Find the source code in the specified directory
Locate <文件名称>
Search for files in the file index database
-D <数据库路径> Search for a specified database
Updatedb
Update a file index database
Find [path] <表达式>
Search for files
-Name <表达式> Search for files by file name
-Iname <表达式> Search for files by file name, case insensitive
-Path <表达式> Search for files by path
-Ipath <表达式> Search for files by PATH, case insensitive
-Amin <分钟> Files accessed in the past N minutes
-Atime <天数> Files accessed in the past N days
-Cmin <分钟> Files modified in the past N minutes
-Ctime <天数> Files modified in the past N days
-Anewer <参照文件> Objects read later than the reference file
-Cnewer <参照文件> Files modified later than the reference files
-Size <大小> Search for files based on the file size, in the unit of B c w k M G
-Type <文件类型> Search for a file based on the file type. Block B device c character device d Directory p pipeline file f common file l link s Port file
-User <用户名> Search for files by owner user
-Uid: search for files by UID
-Group <群组名> Search for files by group
-Gid: search for files by GID
-Empty
Grep <字符串> |" <正则表达式> "[File name]
Search for content in the file
Common Format: find pathname-options [-print-exec-OK]
Example:
Find/-name filename search the file named filename in the root directory
Find/etc-name * s * search for files with s in the directory
Find/etc-name * S search for files ending with s in the directory
Find/etc-name s * search for files starting with s in the directory
Find/-amin-10 search the files accessed in the last 10 minutes in the system
Find/-atime-2 find the file accessed in the last 48 hours in the system
Find/-empty
Find/-group groupname find the file that belongs to groupname in the system
Find/-mmin-5 find the files modified in the last 5 minutes in the system
Find/-mtime-1 find the last 24 hours modified files in the system
Find/-nouser search for files belonging to the paying user in the system
Find/-user username search for files belonging to username in the system
Find/-ctime-1 find the file whose status is changed in the last 24 hours in the system
Find/-fstype type: What is the file type in the system? File
Find/-user user1name-or-user user2name search for files in the system that belong to user1name or belong to user2name
Find/-user user1name-and-user2name search for files in the system that belong to both user1name and user2name.
I. find command
1. the common form of the find command is;
Find pathname-options [-print-exec-OK...]
2. 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.
In addition, the following three differences:
-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.
4, Execute shell commands using exec or OK
When using find, you only need to write the desired operation in a file, you can use exec to search with find, which is very convenient.
In some operating systems, only the-exec option is allowed to execute commands such as l s or ls-l. Most users use this option to find and delete old files. Before executing the rm command to delete files, we recommend that you use the ls command to check whether they are the files to be deleted.
The exec option is followed by the command or script to be executed, followed by a pair of {}, a space, a \, and a semicolon. To use the exec option, you must use the print option at the same time. If you verify the find command, you will find that this command only outputs the relative path and file name from the current path.
For example, to use the ls-l command to list the matched files, you can place the ls-l command in the-exec option of the find command.
# Find.-type f-exec ls-l {}\;
-Rw-r -- 1 root 34928 2003-02-25./conf/httpd. conf
-Rw-r -- 1 root 12959 2003-02-25./conf/magic
-Rw-r -- 1 root 180 2003-02-25./conf. d/README
In the preceding example, the find command matches all common files in the current directory and lists them using the ls-l command in the-exec option.
In the/logs Directory, find the files whose changes were earlier than 5 days and delete them:
$ Find logs-type f-mtime + 5-exec rm {}\;
Remember: you should check the corresponding file before deleting the file in shell in any way. be careful! When using commands such as mv or rm, you can use the-exec option safe mode. It will prompt you before performing operations on each matching file.
In the following example, the find command searches for all file names in the current directory. files whose names end with logs and whose change time is more than five days ago are deleted, but a prompt is provided before the deletion.
$ Find.-name "*. conf"-mtime + 5-OK rm {}\;
<Rm.../conf/httpd. conf>? N
Press y to delete the file, and press n to not delete the file.
Any form of command can be used in the-exec option.
In the following example, we use the grep command. The find command first matches all files named "passwd *", such as passwd, passwd. old, passwd. bak, and then runs the grep command to check whether a sam user exists in these files.
# Find/etc-name "passwd *"-exec grep "sam "{}\;
Sam: x: 501: 501:/usr/sam:/bin/bash