Linux file type viewing and searching, and searching strings in files

Source: Internet
Author: User
Tags egrep

1. view the file type:


, For example: # file/etc/Password


Ii. File Search:

1. Which (find the "execution file 』)

# Which [-A] command parameter:-A: Lists All commands that can be found. By default, it is the first command to be found.
This command searches for the name of the execution file according to the path specified by the environment variable "path.

2. whereis (find [specific file ])

# Whereis [-bmsu] file or directory name parameter:-B: only find the binary file-M: only find the description file, manual path file-s: find the source file only-u: file without instructions!

# Whereis passwd-related information will be found

Passwd:/usr/bin/passwd/etc/passwd. Old/usr/share/man/Man1/passwd.1.gz/usr/share/man/man5/passwd.5.gz

# Whereis-B passwd:/usr/bin/passwd/etc/passwd. Old

# Whereis-M passwd:/usr/share/man/Man1/passwd.1.gz/usr/share/man/man5/passwd.5.gz

3. Locate: Any search name is found.


# Locate filename

Example: # locate passwd


Because the Linux system records all files in the system in a database file
And when you useWhereis, locate

The contents of the database file will prevail.
Therefore, sometimes you will find the files that have been killed when using these two execution files.
! You cannot find the latest file you just created. You can use/usr/bin/updatedb to update the database file.
! Therefore, this command is much faster than find. You can select the directory where you want to create a file database! In the/etc/updatedb. conf configuration file. Change "daily_update = No" to "daily_update = yes 』.

4. Find: powerful, but slow


# Find [path] [Option] [action]


1). Time-related parameters:

-Atime N: the files that have been accessed within one day before n days;

-Ctime N: A file whose status has been changed within one day before n days;

-Mtime N: indicates the file that has been modification within one day before n days;

-Newer file: file is an existing file, which means that as long as the file is newer than file, it will be listed.

2) parameters related to the user or group name


-Uid n: searches for UID-related information. This uid is recorded in/etc/passwd

-Gid N: searches for GID-related GID records in/etc/group,

-User name: user account! -Group Name: group name;

-Nouser: the owner of the file to be searched does not exist./etc/passwd!

-Nogroup: The owner Group of the file to be searched does not exist in/etc/group!

3). parameters related to file permissions and names


-Name filename: search for a file named filename;

-Size [+-] size: searches for files larger (+) or smaller (-) than the size. The size has the following specifications: C: represents byte, and K: represents 1024 bytes.

-Type: the type of the search file. The main types are: General Regular files (F), device files (B, c), directories (d ), link file (L), socket (s), and FIFO (p) attributes.

-Perm mode: searches for files with the File Attribute "exactly equal to" mode. This mode is similar to the CHMOD attribute value. For example, the property of rwsr-XR-X is 4755!

-Perm-mode: searches for files whose file attributes are greater than or equal to the mode attribute.

-Perm + mode: searches for files whose attributes contain any mode!

4). Additional actions:

-Exec command: commands are other commands.-exec can be followed by additional commands to process the search results.

-Print: print the result to the screen,
The default value is. For example, list the files with updated content (mtime) in the past 24 hours. # Find/-mtime 0 0 indicates the current time? #
3*24 ~ before find/-mtime 3 ~ 4*24 hours: Find the files under/etc. If the file date ratio is/etc/passwd, list # Find/etc-newer/etc/passwd. Example: search for CC files under/home # Find/home-user CC example: Search for files that do not belong to anyone in the system # Find/-nouser (this has a certain effect on security, many new software files do not belong to anyone. For example, find the file named passwd # Find/-name passwd. For example, search for a file whose attribute is F. # Find/home-type F. Example: search for attributes containing SGID/SUID/sbit in the file # Find/-Perm + 7000 examples: use LS-L to list the top files. # Find/-Perm + 7000-exec LS-l {}/; {} indicates the content found by find 』, /; indicates the-exec command so far-exec must end with/; end example: Find the file larger than 1 mb in the system # Find/-size + 1000 K

3. Search for strings in the file:

Grep, egrep, fgrep:

The last step is to find the strings in the file. We generally use grep, but egrep supports all regular expressions.

For example, we want to view the character "xingshangdeluren" in the./intelism file"
Grep-n "xingshangdeluren"./intelism

For example, to view all strings starting with a or:
Grep-n-I '/<A'./intelism

For example, we want to see ~ /Intel/file:
Ls-L ~ /Intel/| grep "0" | cut-F10-d''

Of course, the more you enter the required path, the better you stay away from the root directory. Otherwise, the following command may take a long time.

Grep-ANR "you_string "*
In this case, recursion is performed on each sub-directory. If you do not want recursion, remove-R. Note that this command cannot be completed quickly

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: 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.