Common Linux commands (version 2)-file search commands

Source: Internet
Author: User

Common Linux commands (version 2)-file search commands
File SEARCH Command

1. which/usr/bin/which # displays the directory where the system command is located. It is an absolute directory and cannot find files.

Format: which [system command]

E.g. which ls

Appendix-whereis: You can also find the absolute path of the command.

Unlike whereis, which lists the alias records of this command, and whereis displays the location of the help document for this command.

2. find/usr/bin/find # search for files or directories. All files are available, files, and compressed packages.

Syntax: find [search path] [search option] filename

A) find/etc-name file name #-name is the most common option

Find/etc-name init // find the file init in the/etc directory

Note:

1-narrow the search scope as much as possible. Do not search in the root directory; otherwise: 1. The search speed is very slow; 2. A large amount of system resources are occupied.

2-The less system resources occupied, the better, and try to use find when the server is under pressure.

3-find...-name: the search result is different from that of Windows. For example, Windows lists all files containing the init keyword, while Linux only matches the init keyword.

4-use wildcards:

*: Used to match any character

Find/etc-name init * # search for all files starting with init

Find/etc-name * init * # There are no spaces around init. It is used to find all files containing the init keyword.

? : Used to match a single character

Find/etc-name init ??? # This file contains seven characters.

Find/etc-name? Init ??

B) find/etc-size file size

# It is measured in data blocks! 512 bytes = kb, 1 K = 2 Blocks

100 M =? Blocks

100 M = 102400 K = 102400*2 blocks

E. g

Find/etc-size + 204800 # search for files larger than 80 mb and larger than MB in/etc

Find/etc-size-204800 # search for files larger than 80 mb and smaller than MB in/etc

Find/etc-size 204800 # search for files equal to MB in/etc, not commonly used!

C) find/etc-user username to find the file belonging to username

Find/home/xiaofang/Programme/Data_Structure/-user xiaofang

D) search by time value:

1. In days: ctime, atime, mtime

2. In Minutes: cmin, amin, and mmin # are more common.

C-change: indicates that the file attributes have been modified, for example, owner, group, and permission.

A-access: the file has been browsed.

M-modify modification: The file content has been modified

-How long is the time + exceeded?

E. g.

Find/etc-mtime-1

Find/etc-amin-60

Find/home-cmin-120

E) find/etc-type file type # search by file type

F binary file

L soft link file

D directory

E. g.

Find/etc-type f

Find/etc-type l

F)-inum # search by I Node

E. g.

Touch ---abc # Delete: rm ---abc

Touch "a B" # Delete: rm "a B"

Find.-inum159341

Find.-inum 159341-exec rm-f {}\; # find the file with the I node 159341 and delete it

Appendix-find connector:

1.-a: and logic and

-O: or logic or

E. g.

Find/etc-size + 163840-a-size 204800 # find Files> 80 M, <M

Find/etc-name init *-a-type f # find the file named init and binary, excluding the Directory

2. find ..... -Exec command {}\; # fixed format, which can only be written in this way

{}: Query Result of find

\: Escape Character-meaning of the symbolic command

;: The statement ends.

E.g.

Find/etc-name inittab-exec ls-l {}\;

# Search for the inittab file in/etc and display its details

Find/home-user sax-exec rm-rf {}\;

# Deleting all user's sax files

Find/home-user sax-OK rm-rf {}\; #-OK Connector

# Delete all the user's sax files. The user will prompt you if you want to confirm

Find/etc-name init *-OK rm-rf {}\;

3. locate [list file in databases]:/usr/bin/locate

Note:

# It is a Linux-specific command. It is best to find a file or directory to quickly locate system commands and configuration files.

# Although the search speed is very fast, sometimes it cannot be found

# Locate is found in the file database, so the speed will be very fast

# However, if the database does not contain this file, it cannot be found;

Format: locate [Search Keyword]

Cooperation: updatedb [update a database for mlocate]:/usr/bin/updatedb

# Create a database for the entire system directory file

Note: The execution permission is root !!!

4. grep:/bin/grep # search for matching strings in the file and Output

Format: grep [specified string] [source file]

E. g. grep ftp/etc/services

Appendix: grep is particularly powerful and supports regular expressions. For more details, see my other blog post:

Four days proficient in Shell programming (2)

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.