linux common commands (six)

The system is used to find files related commands

  • grep
  • find
  • locate

grep: find files string symbol conditions (keyword)

Command Syntax: grep [options] Find mode [filename]

Options Option Meaning
-E Mode is an extended regular expressions
-F It is a group of a fixed length string line-break delimited
-P Mode is a Perl regular expressions
-b Byte offset of each line of the file in the front row output display comprising the matched string
-c Show only the number of matching lines
-i Comparisons are not case sensitive
-h Suppress the output file name prefix
-l Show only matching file names
-L Show only file names that do not match
-n Before adding the output matching string row line number (first file line number is 1)
-v Displays all lines that do not contain matching characters
-x Forced mode only exact match row
-w Forced mode only for the exact match
-e <mode> Pattern matching operation is performed with
-f <file> Acquisition mode from the file
-r Recursively read all files in each directory
-q Prohibit all registered output
-I Forced think this binary file does not contain any search pattern
-s Suppress error messages
--color Color display

Examples: sy filtered off line containing the / etc / passwd file

[root@localhost ~]# grep sy /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 

Display color matching keywords

[root@localhost ~]# grep --color sy /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 

Show all rows of data content to the beginning of the file d contains "test" of the

[root@localhost ~]# cat d1
1
test1
[root@localhost ~]# cat d2 2 test2 [root@localhost ~]# grep "test" d* d1:test1 d2:test2 

B beginning to find out the contents of the line in / root / aa file

[root@localhost ~]# cat aa
bbb
111
[root@localhost ~]# grep ^b /root/aa bbb 

In / root / kkk document vegetables line content ending of le

[root@localhost ~]# cat kkk
test file
test file1
123
abc
[root@localhost ~]# grep le$ /root/kkk
test file

Find sshd process information

[root@localhost ~]# ps -ef|grep sshd
root       7078      1  0 18:50 ? 00:00:00 /usr/sbin/sshd -D root 7642 7078 0 20:38 ? 00:00:00 sshd: root@pts/1 root 7958 7646 0 22:04 pts/1 00:00:00 grep --color=auto sshd 

find: search within the file system and directory symbolic conditions

Command syntax: find [command options] [path] [Expression Options]

Options Option Meaning
-name <filename> Find a document by file name
-perm <permission> In accordance with the permissions of the file to find the file
-user <username> According to the owner of the file to find the file in German
-group <group name> According to the group owner of the file to find the file
-atime n File is accessed (atime) in the last n days, n represents a number
-amin n File is accessed (atime) in the last n minutes, n represents a number
-ctime n It has been changed (ctime) file in the last n days, n represents a number
-cmin n It has been changed (ctime) in the last minute file n, n represents a number
-mtime n It has been modified (mtime) of the file in the last n days, n represents a number
-mmin n It was modified (the mtime) n minutes in the past documents, n represents a number
-size n[ckMG] Find uppercase file n, n represents a number, c = byte, k representative of KB, M Representative MB, G Representative GB
-empty Find an empty file, it can be an ordinary file or directory
-type <type> Search by document type, document (f), directory (d), the device (b, c), a link (l), etc.
-fstype <file system type> According to the specified file system type to find files
-nogroup No group of files
-nouser Without the user's file
-uid <User UID> According to the owner of the file UID users to find files
-gid <group GID> According to the group GID owner of the file to find the file
n -inum According to inode numbers of files to find files
-readable Matching read-only file
-samefile <filename> Locate and specify the file with the same file inode
-writable Matching writable files
-links n According to the file number of links to find the file, n represents a number

You can define different file types when looking for files

character meaning
b Block device file
d table of Contents
c Character device file
p Pipe file
l Symbolic link file
f Trivial File
s socket file

Example: find / boot menu configuration file in the boot directory grub.cfg

[root@localhost ~]# find /boot name grub.cfg

Find "/" directory of all to ".conf" file extension

[root@localhost ~]# find / -name "*.conf"

Lists all the files currently in the last 20 days have changed directory and its subdirectories

[root@localhost ~]# find . -ctime -20

Find / root directory empty file or subdirectory

[root@localhost ~]# find /root -empty

Find / boot directory to find the file type is a directory of files

[root@localhost ~]# find /boot -type d

Users find the owner of the / home directory file UID 1000

[root@localhost ~]# find /home -uid 1000

Find the file inode number is 33582158, (using the "start [File]" command to view the file inode number)

[root@localhost ~]# find /root -inum 33582158
/root/anaconda-ks.cfg

Find is not case sensitive documents jacob

root@localhost ~]# find -iname "cs.log"
./cs.log

Find the computer two days before the day is modified documents

root@localhost ~]# find / -mtime 2

Find documents larger than 10MB of the current directory

[root@localhost ~]# find ./ -size +10M
./boot/initramfs-0-rescue-7ad525c9157940e3847220b1bdb8be06.img
./boot/initramfs-3.10.0-957.el7.x86_64.img 

Find all the ordinary files in the current directory

[root@localhost ~]# find ./ -type f

Find all documents owned computers tom

[root@localhost ~]# find ./ -user tom

Details are listed in the file after the find files larger than 1MB

[root@localhost ~]# find ./ -size +1M -exec ls -l {} \;

Find all files larger than 1MB computer

[root@localhost ~]# find / -size +1M -a -type f

locate: Find files in the database

使用locate命令可以通过数据库(var/lib/mlocate/mlocate.db文件)来查找文件,这个数据库每天由cron程序来建立。当创建好这个数据库后,就可以方便地搜索所需文件了,它比find命令搜索速度还要快。 命令语法:locate [选项] [范本样式]

选项 选项含义
-q 安静模式,不会显示任何错误信息
-r 使用正则表达式作为搜索的条件
-i 匹配模式是忽略区分大小写
-c 显示找到的条目数
-w 匹配完整路径名

例子:查找httpd.conf文件

[root@localhost ~]# locate
-bash: locate: command not found
[root@localhost ~]# yum -y install mlocate
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.cn99.com
base                                                                                | 3.6 kB  00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/4): base/7/x86_64/group_gz | 166 kB 00:00:02 (2/4): extras/7/x86_64/primary_db | 205 kB 00:00:02 (3/4): base/7/x86_64/primary_db | 6.0 MB 00:00:04 (4/4): updates/7/x86_64/primary_db | 6.4 MB 00:00:04 Resolving Dependencies --> Running transaction check ---> Package mlocate.x86_64 0:0.26-8.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: mlocate x86_64 0.26-8.el7 base 113 k Transaction Summary =========================================================================================================== Install 1 Package Total download size: 113 k Installed size: 379 k Downloading packages: warning: /var/cache/yum/x86_64/7/base/packages/mlocate-0.26-8.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Public key for mlocate-0.26-8.el7.x86_64.rpm is not installed mlocate-0.26-8.el7.x86_64.rpm | 113 kB 00:00:02 Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Importing GPG key 0xF4A80EB5: Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>" Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5 Package : centos-release-7-6.1810.2.el7.centos.x86_64 (@anaconda) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mlocate-0.26-8.el7.x86_64 1/1 Verifying : mlocate-0.26-8.el7.x86_64 1/1 Installed: mlocate.x86_64 0:0.26-8.el7 Complete! [root@localhost ~]# updatedb [root@localhost ~]# locate httpd.conf /root/httpd.conf

摘自:《Linux实用教程》人民邮电出版社出版发行 於岳编著

Guess you like

Origin www.cnblogs.com/mylive/p/11106786.html