13笔记---find
2019-08-12 本文已影响0人
口口帅日日
上周回顾
用户管理
用户基本概述
介绍创建用户会影响的文件
/etc/passwd
/etc/shadow
如何创建用户
useradd -u ,-g -G ,-M -d -c ,-s ,-r
如何修改用户
usermod -u, -g -G ,-d ,-c ,-s ,-r ,-l
如何删除用户
userdel -r
如何给用户设定密码
passwd passwd --stdin
用户创建流程
/etc/login.defs /etc/defaults/useradd
组的管理
组的基本概念
基本组:创建用户时使用-g指定基本组,基本组必须实现存在。
私有组:创建用户时,不指定基本组,默认创建同名的私有组。
附加组:当基本组无法满足权限需求,可关联多个附加组
组的配置文件
/etc/group
/etc/gshadow
如何创建组 groupadd -g 指定gid
如何修改组 groupmod -g 修改gid -n 修改组名称
如何删除组 groupdel
如何给用户提权?
su 切换用户
交互式shell 输入一条命令,返回一条输出
非交互式shell 输入一条指令,批量的完成任务,无需人为干预
登录shell 输入用户名和密码登录系统
非登录式shell 无需输入用户名和密码,即可登录用户
登录式shell和非登录式shell 加载的配置文件不一样
su username使用的是非登录式shell (有些环境变量没有 加载,会导致执行失败)
su - username 使用的是登录式shell (加载所有的环境变 量)
PS: su 切换用户时需要输入用户的密码,如果是root使用su 则无限制
su 需要知道用户的密码, 不安全
sudo 提权
提权的作用: 让用户临时的使用root身份进行操作.
首先必须先进行权限划分 ---> root
其次将权限与用户进行关联 ----> root
最后用户才能正常使用,或者提权 sudo -l
检查分配的权限 是否合理
权限管理
权限: 指用户能对系统操作的一种范围限制.
用户和权限的关系?
权限中rwx是什么意思?
r 可读 ---->4
w 可写 ----->2
x 可执行 ------>1
对一个文件进行授权? chmod 755
权限中rwx对文件有什么限制?
权限中rwx对目录有什么限制?
PS: 操作文件看文件本身的权限, 如果是删除\移动\复制\等操作,
需要看文件的上级目录是否给予了足够的权限,通常是看有没有 w 权限
授权基本规则:
文件: chmod 644 chmod 600 为了安全
目录: chmod 755
命令: chmod 755 通常来说,命令的属主和属组都是root
变更一个文件的属主和属组? chown -R
特殊权限
Suid: 为命令设定suid,当任何用户执行该命令时,都将以该命令 的属主身份运行
Sgid: 使多个用户共享一个组
Sbit: 粘滞位, 任何人都可以在该目录下创建文件,但只允许删 除自己的. 除root以外
特殊属性
chattr -i 锁住文件 -a 仅能追加,但不允许其他操作
lsattr 查看文件特殊属性
Umask 控制默认权限 (通常表示要减去的权限)
文件: 666 - umask = 最终权限
目录: 777 - umask = 最终权限
/etc/profile 会控制umask
重定向与管道
重定向: 将输出至屏幕的内容,重新指向到一个文件,或丢失.
标准输入 0 标准输出1 错误输出2 修改指向使用 >
重定向: > 覆盖重定向,会先清空掉原有文件的内容,然后写入新的. 文件不存在则创建
>> 追加重定向, 不会覆盖原有的内容,而是往文件尾部新增内容
2> 错误覆盖重定向
2>> 错误追加重定向
&> 混合输出, 正确和错误都输出到同一个文件中, 没次都 会覆盖文件已有内容
&>> 混合输出, 文件尾部添加内容
管道技术
管道: 将左边命令的输出 --> 管道 ---> 传递给右边命令的输入
tee: 将左边命令的输出 -->| tee a.log ---> 传递给右边命令的输 入
今日内容
1.为什么要有文件查找
资料很多,忘记位置,需要通过查找的方式进行搜索。
2.windows如何实现文件查找
计算机--->搜索框
3.linux如何实现文件查找
1.没有图形工具,
2.使用命令工具搜索-->find
4.find命令查找语法
命令 路径 选项 表达式 动作
find [path...] [options] [expression] [action]
5.find针对文件名称、类型、大小 、时间等方式进行查找文件
1.按照名称查找
#1.按照名称进行查找
[root@testedu ~]# find ./ -name "*eth0
#2.按照名称查找(不区分大小写)
[root@testedu ~]# find ./ -iname "*eth0"
2.按文件大小查找size
#1.查找/etc/目录下大于5M的文件
[root@testedu ~]# find /etc/ -size +5M
2.查找/etc/目录下小于5M的文件
[root@testedu ~]# find /etc/ -size -5M
#3 .查找/etc/目录下等于5M的文件
[root@testedu ~]# find /etc/ -size 5M
3.按文件类型查找type
f # 文件
d # 目录
s # socket套接字文件
l # 链接文件
c # 字符设备
b # 块设备
#1.查找当前目录下类型是文件的,并且名称跟eth0相关的都列出来
[root@testedu ~]# find ./ -type f -iname "*eth0" | xargs ls -l
#2.查找/etc/目录下类型是文件的,大小是大于5M,名称以.bin结尾的
[root@testedu ~]# find /etc/ -type f -size +5M name "*.bin"
#3.查找/etc/目录下类型是文件的,名称是.repo结尾的
[root@testedu ~]# find /etc/ -type f -name "*.repo"
#4.查找/dev下的类型是块设备的,并名称是sda开头的
[root@testedu ~]# find /dev/ -type b -name "sda*" | xargs ls -l
#5.查找/dev下的类型是字符设备,并名称是tty开头的 [root@testedu ~]# find /dev/ -type c -name "tty*"
4. 按修改时间进行查找 -mtime
[root@testedu ~]# for i in {1..31}; do date -s "2019/08/$i" ; touch file-$i ; done
1.第7天
[root@testedu ~]# find ./ -type f -mtime 7
2.7天以前的内容都会被筛选出来,然后删除. 保留了最近7天的内容
[root@testedu ~]# find ./ -type f -mtime +7 -name "file-*"
3.最近7天的内容都会被筛选出来
[root@testedu ~]# find ./ -type f -mtime -7 -name "file-*"
4.本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件 (实际使用方案)
find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +180 -delete
5. 按用户和组进行查找 -user -group -nouser -nogroup
#查找属主是jack
[root@xuliangwei ~]# find /home -user jack
#查找属组是admin
[root@xuliangwei ~]# find /home -group admin
#查找属主是jacky, 属组是jack
[root@testedu ~]# find /home/ -type d -user jacky group jack
#查找没有属主
[root@xuliangwei ~]# find /home -nouser
#查找没有属组
[root@xuliangwei ~]# find /home -nogroup
#查找没有属主或属组
[root@xuliangwei ~]# find / -nouser -nogroup
6.find查找后的处理动作
查找到一个文件后,需要对文件进行如何处理,find的默认动作是 -print.
动作 含义
-print 打印查找到的内容(默认) ---ignore
-ls 以长格式显示的方式打印查找到的内容 ---ignore | xargs ls -l
delete 删除查找到的文件 (删除目录,仅能删除空目录) ---ignore | xargs rm -f
-ok 后面跟自定义 shell 命令(会提示是否操作) ---ignore
-exec 后面跟自定义 shell 命令(标准写法 -exec \;) | xargs
[root@testedu ~]# time find ./ -type f -name
"file*" -exec rm -f {} \;
real 0m6.585s
user 0m3.617s
sys 0m3.532s
[root@testedu ~]# time find ./ -type f -name "file*" | xargs rm -f
real 0m0.152s
user 0m0.016s
sys 0m0.146s
#查找/var/log/ 类型是文件的,并且名称是.log结尾的,并且7天以前 的,然后删除
[root@testedu ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -exec rm -f {} \;
[root@testedu ~]# #find /var/log/ -type f -name "*.log" -mtime +7 -delete
[root@testedu ~]# #find /var/log/ -type f -name "*.log" -mtime +7 | xargs rm -f
记得文件的内容是什么,但是不清楚文件名称是什么,也不知道路 径在哪,怎么办?
#find 是查询文件
#grep 过滤内容
#将ifnd查询的文件结果,作为grep的参数
[root@testedu ~]# find /etc/ -type f | xargs grep "log_group" --color=auto
/etc/audit/auditd.conf:log_group = root
4.find逻辑运算符
符号 作用
-a 与
-o 或
-not|! 非
#1.查找当前目录下,属主不是root的所有文件
[root@testedu ~]# find /home/ ! -user root -ls
[root@testedu ~]# find /home/ -not -user root -ls
# 使用较少
#2.查找当前目录下,属主属于jack,并且大小大于1k的文件
[root@testedu ~]# find /home/ -type f -a -user jacky -a -size +1k
#3.查找当前目录下的属主为root 或者 以xml结尾的普通文件
[root@xuliangwei ~]# find . -type f -a \( -user hdfs o -name '*.xml' \)
5.find练习
1.查找/var目录下,属主不是root,且文件名不以f开头的文件
[root@testedu ~]# find /var/ -type f ! -user root a ! -name "f*"
2.查找/var目录下属主为root,且属组为mail的所有文件
[root@testedu ~]# find /var/ -type f -user root -a group mail
3.查找/var目录下不属于root、lp的所有文件
[root@testedu ~]# find /var/ -type f ! -user root -a ! -user lp
4.查找/var目录下最近一周内产生的文件,同时属主不为root,也不是 postfix的文件
[root@testedu ~]# find /var/ -type f -mtime -7 ! user root ! -name "postfix"
5.查找/etc目录下大于1M且类型为普通文件的所有文件
[root@testedu ~]# find /etc/ -type f -size +1M
6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
[root@testedu ~]# find /etc/ -type d -exec mkdir -p /tmp/{} \;
7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限 777/var/tmp/etc目录中所有文件权限666
[root@testedu ~]# cp /etc/ /var/tmp/ -rp
[root@testedu ~]# find /var/tmp/etc/ -type d -exec chmod 777 {} \;
[root@testedu ~]# find /var/tmp/etc/ -type f -exec chmod 666 {} \;
8.保留/var/log/下最近7天的日志文件,其他全部删除
[root@testedu ~]# find /var/log/ -type f -mtime +7 -exec rm -f {} \
9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
[root@testedu ~]# find ./ -type f -name "file*" ! name "file9" -exec rm -f {} \;
10.解释如下每条命令含义
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5" find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) ls
find /root/dir1 \( -name "file5" -o -name "file9" \) exec rm -vf {} \;
find /root/dir1 ! \( -name "file4" -o -name "file8" \) -exec rm -vf {} \;
今日总结
1.find 查找文件
文件名称
文件大小
文件类型
修改时间
用户用户组
2.find处理动作
-print 忽略 默认送
-ls 长格式显示,不能加参数,所以忽略
-delete 删除文件,删除目录必须确保目录为空
-ok 执行command命令,但会提示,忽略
-exec 执行command命令
3.find的逻辑运算
与 -a
或 -o
非 ! | -no