码迷,mamicode.com
首页 > 系统相关 > 详细

linux查找命令

时间:2019-09-23 15:26:07      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:ssh   数字   位置   依据   默认   sel   bashrc   dir   单引号   

查看命令本身

  • which
    -whereis

查看文件内容

  • cat
  • more
  • less
  • head
  • tail

过滤匹配

  • grep

查找文件

  • locate
  • find

查看命令本身

which

用于查找命令的位置

[root@meditation ~]# which cd
/usr/bin/cd

whereis

输出命令相关的目录和配置文件

[root@meditation ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz

?

# 查看文件内容

[root@VM_0_15_centos ~]# ls /etc/ > /root/123.txt

### cat
从第一行开始显示所有内容输出到屏幕上

  • -n 显示行数
[root@VM_0_15_centos ~]# cat -n /root/123.txt 
...
   187  trusted-key.key
   188  tuned
   189  udev
   190  updatedb.conf
   191  usb_modeswitch.conf
   192  uuid
   193  vconsole.conf
   194  vimrc
   195  virc
   196  wgetrc
   197  wpa_supplicant
   198  X11
   199  xdg
   200  xinetd.d
   201  yum
   202  yum.conf
   203  yum.repos.d

### more
从第一行开始,输出文件内容,当一页无法全部输出时,回车翻行,空格翻页,q退出
常配合管道符使用
```
[root@VM_0_15_centos ~]# ll /etc/ | more

drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.monthly
-rw-r--r--. 1 root root 451 Jun 10 2014 crontab
drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.weekly
-rw-------. 1 root root 0 Mar 7 14:38 crypttab
-rw-r--r--. 1 root root 1620 Oct 31 2018 csh.cshrc
-rw-r--r--. 1 root root 866 Oct 31 2018 csh.login
drwxr-xr-x. 4 root root 4096 Jul 2 15:03 dbus-1
drwxr-xr-x. 2 root root 4096 Jul 2 15:03 default
drwxr-xr-x. 2 root root 4096 Mar 7 14:39 depmod.d
drwxr-x---. 4 root root 4096 Mar 7 14:39 dhcp
--More--

 
 ### less
 **和more命令相似,但是支持向前查看,还可以通过/passwd来查找passwd字符串**
 

[root@VM_0_15_centos ~]# ll /etc/ | less
total 1456
drwxr-xr-x. 3 root root 4096 Mar 7 14:39 abrt
drwxr-xr-x. 4 root root 4096 Mar 7 14:39 acpi
-rw-r--r--. 1 root root 16 Mar 7 14:42 adjtime
-rw-r--r--. 1 root root 1518 Jun 7 2013 aliases
-rw-r--r-- 1 root root 12288 Mar 7 14:44 aliases.db
drwxr-xr-x. 2 root root 4096 Jul 2 15:03 alternatives
-rw------- 1 root root 541 Nov 20 2018 anacrontab
...



 
 
 
 
 ### head
 
 显示文件的开始部分
 -n 指定显示文件开始部分的行数,默认10行

[root@VM_0_15_centos ~]# head -n 2 /root/123.txt
abrt
acpi

### tail
显示文件的结束部分
-n 指定显示文件结束部分的行数,默认10行
-f 监视文件是否有增加变化

[root@VM_0_15_centos ~]# tail -n 2 /root/123.txt
yum.conf
yum.repos.d

 
 
 # 过滤匹配
 

 
 
### locate 
>根据文件名查找
> 优点速度快,从数据库中查找

[root@meditation ~]# touch /home/sunlizhao/abc123.txt
[root@meditation ~]# locate abc123.txt
[root@meditation ~]#

- 默认数据库24个小时更新.
- 当我们在用户目录下新建一个文件, 直接locate是查找不到的
- 这时就要更新一下数据库updatedb

[root@meditation ~]# updatedb
[root@meditation ~]# locate abc123.txt
/home/sunlizhao/abc123.txt

### grep
> 用于查找文件中的内容
> 在文件当中匹配符合条件的字符串

**语法**

grep [选项] 字符串 文件名

**选项**
-     -i ---- 忽略大小写
-     -v ---- 排除指定字符串(取反)
-    -n ---- 显示行号
-     -r ---- 递归
- ^# ---- 以#开头
- #$ ---- 以#结尾
- ^$ ---- 空行
- -n ---- 对过滤的内容加上行号
- | ---- 或者的意思


##### 1匹配

**1.1匹配一个词**

[root@meditation ~]# grep root /home/sunlizhao/abc123.txt
root?0:0:root:/root:/bin/bash
operator?11:0:operator:/root:/sbin/nologin


**1.2将文件中没有nologin的那行取出来,并显示行号**

[root@meditation ~]# grep -nv nologin /home/sunlizhao/abc123.txt
1:root?0:0:root:/root:/bin/bash
6:sync?5:0:sync:/sbin:/bin/sync
7:shutdown?6:0:shutdown:/sbin:/sbin/shutdown
8:halt?7:0:halt:/sbin:/sbin/halt
25:syslog?996:994::/home/syslog:/bin/false
27:jfedu1?1000:1000::/home/jfedu1:/bin/bash
28:jfedu2?1001:1001::/home/jfedu2:/bin/bash
29:sunlizhao?1002:1002::/home/sunlizhao:/bin/bash
30:sunlizhao31?1003:1003::/home/sunlizhao31:/bin/bash

**1.3从多个文件中匹配,并显示行号**

[root@meditation ~]# grep -n root /home/sunlizhao/abc123.txt /etc/passwd
/home/sunlizhao/abc123.txt:1:root?0:0:root:/root:/bin/bash
/home/sunlizhao/abc123.txt:10:operator?11:0:operator:/root:/sbin/nologin
/etc/passwd:1:root?0:0:root:/root:/bin/bash
/etc/passwd:10:operator?11:0:operator:/root:/sbin/nologin


**1.4搜索运行中的sshd进程,并过滤掉grep命令本身**

[root@VM_0_15_centos ~]# ps aux | grep sshd | grep -v grep
root 1209 0.0 0.5 156744 5440 ? Ss 22:43 0:00 sshd: root@pts/1
root 1323 0.0 0.5 156744 5436 ? Ss 22:44 0:00 sshd: root@pts/2
root 3081 0.0 0.4 112864 4304 ? Ss Aug13 0:52 /usr/sbin/sshd -D
root 7396 0.0 0.4 112864 4244 ? Ss 23:35 0:00 sshd: [accepted]
root 7969 0.0 0.5 156744 5436 ? Ss 14:12 0:00 sshd: root@pts/0


**1.5统计检索出的指定内容有多少行**

[root@VM_0_15_centos ~]# egrep "nologin|root" /etc/passwd | wc -l
21




##### 2比较
**2.1比较两个文件中共同存在的行,并显示行号**

[root@meditation ~]# grep -nxf /home/sunlizhao/abc123.txt /etc/passwd
1:root?0:0:root:/root:/bin/bash
2:bin?1:1:bin:/bin:/sbin/nologin
3:daemon?2:2:daemon:/sbin:/sbin/nologin
4:adm?3:4:adm:/var/adm:/sbin/nologin
5:lp?4:7:lp:/var/spool/lpd:/sbin/nologin

**2.2比较abc123.txt文件比passwd文件多的部分**

添加数据
[root@meditation ~]# echo "hhhhh" >> /home/sunlizhao/abc123.txt

[root@meditation ~]# grep -vxf /etc/passwd /home/sunlizhao/abc123.txt
hhhhh


##### 3查找
**查找abc123.txt文件中以root开头的行**

[root@meditation ~]# grep ^root /home/sunlizhao/abc123.txt
root?0:0:root:/root:/bin/bash

**查找abc123.txt文件中以bash结尾的行**

[root@meditation ~]# grep bash$ /home/sunlizhao/abc123.txt
root?0:0:root:/root:/bin/bash
jfedu1?1000:1000::/home/jfedu1:/bin/bash
jfedu2?1001:1001::/home/jfedu2:/bin/bash
sunlizhao?1002:1002::/home/sunlizhao:/bin/bash

##### 4递归查询
**查找/sunlizhao/目录下包含"root"的文件

[root@meditation ~]# echo "root" > /home/sunlizhao/abc.txt
[root@meditation ~]# grep -r root /home/sunlizhao/
/home/sunlizhao/abc.txt:root
/home/sunlizhao/abc123.txt:root?0:0:root:/root:/bin/bash
/home/sunlizhao/abc123.txt:operator?11:0:operator:/root:/sbin/nologin


### find
> 注意,使用find时要避免大范围搜索,会非常耗费系统资源

##### 1,名称
###### 1.1name和iname
**选项**
- -name 根据文件名称进行检索
- -iname 根据文件名进行检索时忽略大小写

[root@VM_0_15_centos ~]# find / -name "passwd"
/etc/passwd
/etc/pam.d/passwd
/usr/share/bash-completion/completions/passwd
/usr/bin/passwd

###### 1.2path和ipath
**如果需要在搜索时匹配某个文件或目录的完整路径,而不仅仅是匹配文件名
可以使用-path或-ipath**
- -path 指定要匹配文件的父目录
- -ipath 指定要匹配的文件的父目录,忽略大小写

**查找/目录下的以passwd开头的文件,父目录必须是/etc**
> 发现有两个文件,passwd-是passwd的自动备份文件

[root@VM_0_15_centos /]# find / -path "/etc/passwd*"
/etc/passwd
/etc/passwd-





##### 2,类型
- -type
    - f 文件
    - d 目录
    - | 符号链接

**查看root目录下后缀名是.sh的文件**

[root@VM_0_15_centos ~]# find /root -type f -name "*.sh"
/root/eof.sh
/root/ping.sh
/root/useradd1.sh
/root/packups.sh



##### 3,空文件

- -empty  检索为空的文件或者目录

**检索用户主目录下所有的空目录**

[root@VM_0_15_centos ~]# find ~ -type d -empty
/root/DES_DIR
/root/.config/abrt



##### 5,反义匹配
> 对当前匹配条件进行“反义”类似于逻辑非操作

**检索出/root下所有文件, 但是后缀不能是.sh**

[root@VM_0_15_centos ~]# find /root -type f ! -name "*.sh"
/root/cpu.txt
/root/.lesshst
/root/.cshrc
/root/.bashrc
/root/.viminfo


**检索出/root下所有不为空的文件**

[root@VM_0_15_centos ~]# find /root/ -type f ! -empty
/root/eof.sh
/root/cpu.txt
/root/.lesshst
/root/ping.sh
/root/.cshrc
/root/useradd1.sh


##### 5,文件的所属权检索

- -user   检索归属于特定用户的文件或者目录
- -group   根据文件或者目录的属组进行检索


**查看/root目录下,是否有不属于root的文件**

[root@VM_0_15_centos ~]# find /root/ -type f ! -user root
[root@VM_0_15_centos ~]#

**检索/root目录下


##### 6,根据时间日期进行检索
> 需要根据文件被修改的时间进行检索

|选项/天 | 选项/分钟 | 描述 |
|-|-|-|
|-mtime|-mmin|修改时间:最后一次文件内容有过更改的时间点|
|-atime|-amin| 访问时间:最后一次文件有被读取过的时间点|
|-ctime|-cmin| 变更时间: 最后一次文件有被变更过的时间点(如内容被修改,权限被修改)|

|常见写法|描述|
|-|-|
|-mtime 2 | 该文件2天前被修改过|
|-mtime -2 |该文件2天以内被修改过|
|-mtime +2 |该文件距离上次修改已经超过2天时间|

**检索/root目录下,五天内被修改的文件,并去掉隐藏文件**

[root@VM_0_15_centos ~]# find /root/ -type f -mtime -5 ! -name ".*"
/root/.cache/abrt/lastnotification
/root/123.txt




##### 7,根据文件大小检索
- -size 选项允许用户通过文件大小进行搜索(只适合于文件)


**表示大小的单位**

- c(小写):字节
- k(小写):KB
- M(大写):MB
- G(大写):GB

**另外,还可以使用+或者-表示大于或小于当前条件**

[root@VM_0_15_centos ~]# find /root -type f -size +1k
/root/.viminfo
/root/.bash_history
/root/.packup.swp
/root/123.txt
/root/c.txt


##### 8,根据文件权限检索

- -perm选项以文件权限为依据进行搜索

###### 8.1使用符号形势
find /usr -perm u=rwx,g=rx,o=x
如果要匹配的文件的权限为- r-xr-xr-x,即u,g,o的权限是相同的
可以使用

[root@VM_0_15_centos ~]# find /root/ -perm u=rw,g=r,o=r
/root/eof.sh
/root/cpu.txt
/root/ping.sh



###### 8.2使用数字形势

[root@VM_0_15_centos ~]# find /root/ -perm 644
/root/eof.sh
/root/cpu.txt
/root/ping.sh



##### 9,限制遍历的层数

> find命令默认是以递归方式检索项目的,但这种方式会导致得到的结果数量非常大

**使用-maxdepth限制find命令递归的层数**

[root@VM_0_15_centos ~]# find /etc/ -name "passwd"
/etc/passwd
/etc/pam.d/passwd
[root@VM_0_15_centos ~]# find /etc/ -maxdepth 1 -name "passwd"
/etc/passwd





**10,逻辑组合**

- 在之前的例子中有出现多个搜索天剑的组合以及对某个搜索条件的反转

- 实际上find命令支持and和or两种逻辑运算,对应的命令选项分别为-a和-o

- 此外还可以通过小括号对搜索条件进行分组

> 但是要注意,命令钟的小括号常需要用单引号包裹起来,因为小括号在shell中有特殊含义

**检索/etc下名为ssh的目录**

[root@VM_0_15_centos ~]# find /etc/ -type d -name "ssh"
/etc/ssh
/etc/selinux/targeted/active/modules/100/ssh

**等同于**

[root@VM_0_15_centos ~]# find /etc/ -type d -a -name "ssh"
/etc/ssh
/etc/selinux/targeted/active/modules/100/ssh
```

linux查找命令

标签:ssh   数字   位置   依据   默认   sel   bashrc   dir   单引号   

原文地址:https://www.cnblogs.com/inmeditation/p/11572321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!