Linux知识心得11 三剑客+文件查找1

笔记

1、虚拟机安装

# 按tab键
net.ifnames=0  biosdevname=0

2、系统初始化

1、上传脚本
[root@localhost ~]# ll
total 12
-rwxr-xr-x. 1 root root 4544 Jun 29 15:23 init.sh

2、对脚本授权
[root@localhost ~]# chmod +x init.sh

3、执行初始化脚本
[root@localhost ~]# ./init.sh 

4、关机,拍摄快照

3、习题

1、找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写


3、找出/etc/init.d/function文件下包含小括号的行
grep '[()]' /etc/init.d/functions

4、输出指定目录的基名
echo "/fsd/fsdaf/sdfs/dfs/dfs/df/sdf/sdf/sdf/sdafa/fsdf/fsd" | awk -F"/" '{print $NF}'

5、找出网卡信息中包含的数字
ip a | grep -oE '[0-9]+'

6、找出/etc/passwd下每种解析器的用户个数
awk -F: '{word[$NF]++}END{for(i in word){print i,word[i]}}' /etc/passwd

7、获取网卡中的ip
ip a | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'

8、搜索/root目录下,所有的.html或.php文件中main函数出现的次数
find /root/ -name '*.html' -o -name '*.php' | xargs -I {} grep -lR 'main' {}

9、过滤php.ini中注释的行和空行
[root@localhost ~]# grep -vE '^$|^#' /etc/fstab 
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=8c73a8c6-1694-4059-87d8-0424b8f51109 /boot                   xfs     defaults        0 0

10、找出文件中至少有一个空格的行
grep -E '\ +' /etc/passwd

11、过滤文件中以#开头的行,后面至少有一个空格
grep -E '^#\ +' /etc/fstab

12、查询出/etc目录中包含多少个root
grep -Rc 'root' /etc/ | wc -L

13、查询出所有的qq邮箱
grep -E '[0-9a-zA-Z]+\@qq\.com'

14、查询系统日志中所有的error
grep -iE 'error' /var/log/messages

4、文件查找

1、命令查找(查找某一个命令在什么地方)
which [command]

2、文件查找(find)
	1、按照文件名称查找
	
		格式:
			find [路径] -name '[匹配规则]'
		
		例1:查询以.html结尾的文件
			[root@localhost ~]# find /root -name '*.php'
			/root/2.php
		
		例2:查询/etc目录下,文件名称中包含pass的文件
			[root@localhost ~]# find /etc -name '*pass*'
		
		例3:查询/ect目录下,文件名以pass开头的文件
			[root@localhost ~]# find /etc -name 'pass*'

	2、按照文件类型查找
	
		-(f) : 普通文件
		d : 目录文件
		l : 链接文件
		b : 块设备文件
		c : 字符设备
		s : 套接字文件
		p : 管道文件

		格式:
			find [路径] -type '[匹配类型]'
		
		例1:查询/root目录下,有哪些普通文件
			[root@localhost ~]# find  /root/ -type f
		
		例2:查询/dev目录下,有哪些字符设备文件?
			[root@localhost ~]# find /dev/ -type c
			
		例3:查询/run目录下,有哪些套接字文件
			[root@localhost ~]# find /run -type s

	3、按照属性来查
	
		属主,属组,不存在用户或用户组的
		
		格式:
			find [路径] -user '用户名'
			find [路径] -group '组名'
			
			find [路径] -nouser 
			find [路径] -nogroup
		
		例1:查询/tmp目录下,有哪些属主为root的文件?
			find /tmp -user 'root'	
         
         例2:查询/tmp目录下,有哪些属组为test的文件?
         	find /tmp -group 'test'
		
		例3:查询/tmp目录下没有用户的文件有哪些?
			find /tmp -nouser 
		
		例4:查询/tmp目录下没有属组的文件?
			find /tmp -nogroup
		
		例5:查询/tmp目录下,没有属组同时没有属主的文件?
			find /tmp -nouser -nogroup
	
	4、按照权限来查找
	
		可读权限(4),可写权限(2),可执行权限(1)
		
		格式:
			find [路径] -perm [数字权限]
			
		例1:查询/tmp目录下,权限为664的文件有哪些
			find /tmp -perm 664
			
		例2:查询/tmp目录下,属组只有可读可写权限的文件有哪些?
			find /tmp -perm 664
	
	5、按照文件大小查
	
		dd :创建文件的命令
			if : 指定读取的文件
			of : 指定写入的文件
			bs : 每次写入的大小
			count : 写几次
		例1:写入文件50M
			dd if=/dev/zero of=/tmp/test4.txt bs=10M count=5
			
		参数:
			-a : 并且(默认)
			-o : 或者
	
		格式:
			find [路径] -size '文件大小'
			
		例1:查新/tmp目录下,大小为40M的文件有哪些?
			find /tmp/ -size 40M
		
		例2:查找/tmp目录下,大小为20M的文件有哪些?
			find /tmp -size 20M
		
		例3:查找/tmp目录下,大于20M的文件并且小于40M的文件有哪些?
			find /tmp -size +20M -a -size -40M
		
		例4:查找/tmp目录下,小于30M 或 大于40M的文件有哪些?
			find /tmp -size 30M -o -size +4M 
	
	6、按照时间来查询
		
		date -s '修改的时间'
		
		同步互联网时间:/usr/sbin/ntpdate ntp1.aliyun.com
		
		参数:
			-ctime : 创建时间
			-atime : 访问时间
			-mtime : 修改时间
		
			+7	: 7天前创建的
			7	:正好七天创建的
			-7	:7天内创建的
		
		格式:
		
			find [路径] -ctime '7'
		
		例1:查询7天前创建的文件?
		
			find /tmp -ctime +7
		
		例2:查询7天内创建的文件?
			find /tmp -ctime -7

3、作业
	1. 查找ifconfig命令⽂件的位置,⽤不同的⽅式实现
        [root@ljl ~]# find / -name ifconfig
        /usr/sbin/ifconfig
        [root@ljl ~]# which ifconfig
        /usr/sbin/ifconfig
        [root@ljl ~]# 	

	2. 查找/etc/中的所有⼦⽬录(仅⽬录)
		find /etc/ -type d		

4、预习
	1. 查找/etc/中的所有⼦⽬录(仅⽬录)复制到/tmp下
	2. 查找/etc⽬录复制到/var/tmp/, 将/var/tmp/etc中的所有⽬录设置权限777(仅⽬录) 将/var/tmp/etc中所有⽂件权限设置为666
posted @ 2021-07-01 17:01  虾虾78  阅读(115)  评论(0)    收藏  举报