Command in Linux - Find, Grep, Which, Whereis, Locate

Source: Internet
Author: User
Keywords find command in linux linux commands linux commands list
find command
find <path> <expression> <cmd>
  • path: The directory to be searched and all its subdirectories. The default is the current directory.
  • expression: The characteristics of the file to be searched.
  • cmd: Perform specific processing on search results.

If no parameters are added, find searches the current directory and its subdirectories by default, and does not filter any results (that is, returns all files), and displays them all on the screen.

Alibaba Cloud Simple Application Server:  Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.

Common options and examples of find command
-name Find the file according to the file name.


ffind /dir -name filename Find the file named filename under the /dir directory and its subdirectories

find. -name "*.c" Find any file with extension "c" in the current directory and its subdirectories (indicated by ".")


-perm Find files according to file permissions.

find. -perm 755 -print Find files with file permissions of 755 in the current directory, that is, files that the owner of the file can read, write, and execute, and other users can read and execute files

-prune Use this option to make the find command not search in the currently specified directory. If you also use the -depth option, then -prune will be ignored by the find command.
find /apps -path "/apps/bin" -prune -o -print Find files in the /apps directory, but don't want to find them in the /apps/bin directory
find /usr/sam -path "/usr/sam/dir1" -prune -o -print Find all files in the /usr/sam directory that are not in the dir1 subdirectory
depth: When searching for a file, first search for the file in the current directory, and then search in its subdirectories.
find / -name "CON.FILE" -depth -print It will first match all files and then enter the subdirectories to find
user searches for files according to the owner of the file.
find ~ -user sam --print Find the file whose owner is sam in the $HOME directory
group Find files according to the group the file belongs to.
find /apps -group gem --print Find files belonging to the gem user group in the /apps directory
mtime -n +n searches for files according to the change time of the file. -n means the file change time is within n days from now, and +n means the file change time is n days ago.
find / -mtime -5 --print Find files whose change time is less than 5 days in the system root directory
find /var/adm -mtime +3 --print Find files whose change time is 3 days before in the /var/adm directory
nogroup finds a file without a valid group, that is, the group to which the file belongs does not exist in /etc/groups.
find / –nogroup -print
nouser finds a file without a valid owner, that is, the owner of the file does not exist in /etc/passwd.
find /home -nouser –print
-newer file1! file2 Find files whose change time is newer than file1 but older than file2.
-type find a certain type of file,
Such as:
b-Block device file.
d-Directory.
c-Character device file.
p-the pipe file.
l-Symbolic link file.
f-normal file.
find /etc -type d -print Find all directories in the /etc directory
find.! -type d -print Find all types of files except directories in the current directory
find /etc -type l --print Find all symbolic link files in the /etc directory
size n[c] finds a file with a file length of n blocks, with c means the file length is in bytes.
find. -size +1000000c -print Find files with a file length greater than 1 M bytes in the current directory
find /home/apache -size 100c -print Find a file whose length is exactly 100 bytes in the /home/apache directory
find. -size +10 -print Find files longer than 10 blocks in the current directory (one block is equal to 512 bytes)
-mount: Do not cross file system mount points when searching for files.
find. -name "*.XC" -mount -print start from the current directory to find files in this file system whose file name ends with XC (do not enter other file systems)
-follow: If the find command encounters a symbolic link file, it will follow the file pointed to by the link
-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’ {} \;, pay attention to the space between {} and \;
$ find ./ -size 0 -exec rm {} \; delete files with zero file size
$ rm -i `find ./ -size 0`
$ find ./ -size 0 | xargs rm -f &
 
In order to list the matched files with the ls -l command, you can put the ls -l command in the -exec option of the find command:
$ find. -type f -exec ls -l {} \;
In the /logs directory, find the files whose change time is before 5 days and delete them:
find /logs -type f -mtime +5 -exec rm {} \;
-ok, has the same effect as -exec, except that it executes the shell command given by this parameter in a safer mode. Before executing each command, a prompt will be given to let the user determine whether to execute it.
find. -name "*.conf" -mtime +5 -ok rm {} \; Find in the current directory all the files whose name ends with .LOG and whose change time is more than 5 days, and delete them, but delete them Give a hint before
Note: If you are looking for a file, then using find would be a good idea. However, since find consumes hard disk when searching for data, don't use find if you have nothing to do! There are better instructions to replace Yo, that is whereis and locate~

Some commonly used commands
1. find. -Type f -exec ls -l {} \;
Find all common files in the current path and list them.
2. find logs -type f -mtime +5 -exec rm {} \;
Delete files in the logs directory whose update time is more than 5 days.
3.find. -Name "*.log" -mtime +5 -ok rm {} \;
Delete the prefix under the current path. The files more than five days after the end of the log must be confirmed before being deleted.
4. find ~ -type f -perm 4755 -print
Find the suid bit in the $HOME directory is set, and the file with the file attribute of 755 is printed out.
Explanation: In some systems, find will get all matched files to exec at one time, but some systems limit the length of the exec command and report: "The parameter list is too long", which requires the use of xargs. xargs is a partial fetching of the incoming file.
5. find / -type f -print |xargs file
xargs test file classification
6. find. -Name "core*" -print|xargs echo "">/tmp/core.log
Report the results of core file information query to core. log log.
7. find / -type f -print | xargs chmod o -w
8. find. -Name * -print |xargs grep "DBO"

grep command
grep [options] pattern [file name]

The options in the command are:
  • -? Display the top and bottom of the matching line at the same time? Line, such as: grep -2 pattern filename Display the upper and lower lines of the matching line at the same time.
  • -b, -byte-offset Print the block number in front of the matching line.
  • -c,—count Only print the number of matched lines, not the matched content.
  • -f File, —file=File Extract template from file. The empty file contains 0 templates, so nothing matches.
  • -h, -no-filename When searching multiple files, do not display the matching file name prefix.
  • -i, —ignore-case Ignore case differences.
  • -q, -quiet Cancel the display and only return to the exit status. 0 means that a matching line was found.
  • -l, —files-with-matches Print a list of files matching the template.
  • -L, —files-without-match Print a list of files that do not match the template.
  • -n, -line-number Print the line number before the matched line.
  • -s, -silent does not display error messages about non-existent or unreadable files.
  • -v, -revert-match Reverse the search and only display the unmatched lines.
  • -w, --word-regexp If it is quoted by \< and >, search the expression as a word.
  • -V, -version Display software version information.
ls -l | grep'^a' Filter the output of ls -l through a pipe, and display only the lines beginning with a.
grep'test' d* displays all lines that contain test in files beginning with d.
grep'test' aa bb cc displays the lines matching test in the aa, bb, cc files.
grep'[a-z]' aa displays all lines that contain at least 5 consecutive lowercase characters in each string.
grep'w(es)t.*' aa If west is matched, es is stored in memory and marked as 1, and then any characters (.*) are searched for, these characters are followed by another es( ), the line is displayed when found. If you use egrep or grep -E, you don't need to escape the "" sign, just write'w(es)t.*' directly.
grep -i pattern files: Search case-insensitively. Case sensitive by default
grep -l pattern files: only list the matching file names,
grep -L pattern files: List the file names that do not match,
grep -w pattern files: only match the whole word, not part of the string (such as matching ‘magic’ instead of ‘magical’),
grep -C number pattern files: the matched context displays [number] lines respectively,
grep pattern1 | pattern2 files: Display the lines matching pattern1 or pattern2,
grep pattern1 files | grep pattern2: Display the lines that match both pattern1 and pattern2.
pattern is the string to be matched, the following patterns can be used
. Matches any character
* Matches 0 or more characters before *
^ Matches the beginning of the line
$ Matches end of line
[] matches any character in [], available in []-means range,
For example, [a-z] represents any one of the letters a to z
\ Escape character

Other find commands

1. locate command
The locate command is actually another way of writing "find -name", but it is much faster than the latter because it does not search a specific directory, but a database (/var/lib/locatedb), which contains All local file information. The Linux system automatically creates this database and updates it once a day, so the latest changed files cannot be found using the locate command. In order to avoid this situation, you can use the updatedb command to manually update the database before using locate.

Examples of use of locate command:
$ locate /etc/sh
Search all files beginning with sh in the etc directory.
$ locate -i ~/m
Search for all files starting with m in the user's home directory, and ignore case.
2. whereis command
The whereis command can only be used to search for program names, and only searches for binary files (parameter -b), man description files (parameter -m), and source code files (parameter -s). If the parameter is omitted, all information is returned.

Examples of the use of whereis command:
$ whereis grep
grep: / bin / grep /usr/share/man/man1p/grep.1p.gz /usr/share/man/man1/grep.1.gz
3. which command
The function of which command is to search for the location of a system command in the path specified by the PATH variable and return the first search result. In other words, by using the which command, you can see whether a certain system command exists and where the command is being executed.
$ which grep
/bin/grep
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.