linux系统邮件配置及find命令相关练习题

# 标准输入
[root@localhost ~]# cat <<EOF
> 1. apple
> 2. pear
> 3. banana
> 4. qiandao
> EOF
1. apple
2. pear
3. banana
4. qiandao


[root@localhost ~]# cat >> 1.txt <<EOF
> 1. apple
> 2. pear
> 3. banana
> 4. qiandao
> EOF
[root@localhost ~]# cat 1.txt 
1. apple
2. pear
3. banana
4. qiandao

## 删除数据
# 安装数据库
[root@localhost ~]# yum install -y mariadb-server
# 设置密码
[root@localhost ~]# mysqladmin -uroot password '123'

# 连接数据库
[root@localhost ~]# mysql -uroot -p123

# 创建数据库
MariaDB [(none)]> create database zls;

# 查看数据库
MariaDB [(none)]> show databases;

# 进入数据库
MariaDB [(none)]> use wxx

# 创建表
MariaDB [wxx]> create table wxx_table2(id int,name varchar(10),age tinyint);

# 插入数据
MariaDB [wxx]> insert into wxx_table2 values(1,'zls',18),(2,'qiandao',80);

# 查看数据
MariaDB [wxx]> select * from wxx.wxx_table2;
+------+---------+------+
| id   | name    | age  |
+------+---------+------+
|    1 | wxx     |   18 |
|    2 | qiandao |   80 |
+------+---------+------+

[root@localhost ~]# mysqldump -uroot -p123 -B zls > /tmp/wxx.beifen

[root@localhost ~]# mysql -uroot -p123
MariaDB [(none)]> drop database wxx;


## 恢复数据
[root@localhost mysql]# mysql -uroot -p123 < /tmp/wxx.beifen
[root@localhost mysql]# cat /tmp/wxx.beifen|mysql -uroot -p123

## 发邮件
[root@localhost ~]# yum install -y mailx
[root@localhost ~]# vim /etc/mail.rc 
############################# 以下是配置文件内容 ##################

#发件人
set from=xx@qq.com
#邮件服务器
set smtp=smtp.qq.com
#发件人用户名
set smtp-auth-user=xxx@qq.com
#发件人密码(QQ邮箱不可以使用密码,只能使用授权码)
set smtp-auth-password=xxx
#登录方式
set smtp-auth=login
#邮件服务器协议及端口
set smtp=smtps://smtp.qq.com:465
#忽略证书
set ssl-verify=ignore
#指定证书位置
set nss-config-dir=/root/.certs


################## 以下都是命令行操作 ############################
# 获取腾讯证书
[root@localhost ~]# mkdir -p /root/.certs
[root@localhost ~]# cd /root/.certs

[root@localhost ~]# echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

[root@localhost ~]# certutil -A -n "GeoTrust SSL CA" -t "C,,"  -d  ~/.certs  -i  ~/.certs/qq.crt

[root@localhost ~]# certutil -A -n "GeoTrust Global CA" -t "C,,"  -d  ~/.certs  -i  ~/.certs/qq.crt

[root@localhost ~]# certutil -L -d /root/.certs

[root@localhost ~]# certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/qq.crt

#### 发邮件
[root@localhost ~]# mail -s '测试发送邮件给小姐姐' 111111@qq.com < /etc/passwd
[root@localhost ~]# echo '自古深情留不住,唯有套路得人心,现在不懂点套路,怎样去撩妹?' | mail -s '再来一封' 111111@qq.com



## dd
[root@localhost ~]# dd if=/dev/zero of=/opt/boot_disk.txt bs=1M count=1024
[root@localhost ~]# dd </dev/zero >/opt/boot2_disk.txt bs=1M count=102


# 管道 |
作用:将管道符左边的标准输出交给右边命令的标准输入来处理

# tee 命令
可以将前面命令的标准输出,输出到文件或者其它设备上,然后将标准输入交给后面的命令处理

# xargs
将前面命令的输出结果作为数据流交给后面命令处理
-n:接数字,可以将指定的数据按照指定数字来排列

文件查找-find

find 命令的基本语法如下

命令 路径 选项 表达式 动作
find [path...] [options] []expression [action]
查找 地区 小姐姐 18 约...
视频 路径 日韩 无码
# 举个栗子
[root@localhost ~]# find /etc/ -name '*.sh'
/etc/profile.d/colorgrep.sh
/etc/profile.d/colorls.sh
/etc/profile.d/which2.sh
/etc/profile.d/less.sh
/etc/profile.d/256term.sh
/etc/profile.d/lang.sh
/etc/profile.d/vim.sh
/etc/dhcp/dhclient-exit-hooks.d/azure-cloud.sh
/etc/kernel/postinst.d/51-dracut-rescue-postinst.sh

根据文件名查找文件

//创建文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}

//查找/etc目录下包含ifcfg-eth0名称的文件
[root@zls ~]# find /etc -name "ifcfg-eth1"

//-i 忽略大小写
[root@zls ~]# find /etc -iname "ifcfg-eth1"
//查找/etc目录下包含ifcfg-eth名称所有文件
[root@zls ~]# find /etc/ -name "ifcfg-eth*"
[root@zls ~]# find /etc -iname "ifcfg-eth*"

//查找包含eth的文件
[root@localhost opt]# find /opt/ -name '*eth*'
[root@localhost opt]# find /opt/ -iname '*eth*'


find /root/dir1 ! (-name 'file5' -o -name 'file9' )

根据文件大小查找

-size n[cwbkMG]
              `b'    block

              `c'    bytes 字节

              `w'    words 单词

              `k'    kb

              `M'    MB

              `G'    GB

# 查找大于5M的文件
[root@localhost ~]# find /etc/ -size +5M
/etc/udev/hwdb.bin

# 查找等于5M的文件
[root@localhost ~]# find /etc/ -size 5M

# 查找小于5M的文件
[root@localhost ~]# find /etc/ -size -5M

## 动作,查看
[root@localhost ~]# find /etc/ -size +5M -ls

## 动作,删除
[root@localhost ~]# find /tmp/ -size +5M -delete

## 集合name
[root@localhost ~]# find /etc/ -size -5M -name '*.sh'

## 需求:在/etc 找到 .sh  和 .conf 结尾的 小于5M的文件
[root@localhost ~]# find /etc/ -size -5M -name '*.sh' -o -name '*.db'

## 需求:在/etc/ 找到 大于3M 小于5M 名字以.sh结尾和.conf结尾
[root@localhost ~]# find /etc/ \( -size +3M -a -size -6M \) -a \( -name '*.sh' -o -name '*.conf' \)

## 需求:在/etc 目录下找到小于5M 并且 文件名不是以 .sh 结尾 或者 不是以 .db结尾
[root@localhost ~]# find /etc/ -size -5M ! \( -name '*.sh' -o -name '*.db' \)

根据文件类型查找

f:文件
d:目录
l:软连接
s:socket
p:管道文件
b:块设备
c:字符设备

find / -type f
find / -type d
find / -type b

find / -type f -ls

find / -type f|head -5|xargs ls -l

根据日期查找

-mtime

## 找七天之前的文件,(不包含今天)
[root@localhost ~]# find /opt/ -mtime +7 -name '*.txt'

## 找最近七天的文件
[root@localhost ~]# find /opt/ -mtime -7 -name '*.txt'

## 找第七天的(不包含今天)
[root@localhost ~]# find /opt/ -mtime 7 -name '*.txt'


## 企业需求:只保留N天的备份,其余的都删除
[root@localhost ~]#  find /opt/ ! -mtime -7 -name '*.txt' -delete
[root@localhost ~]#  find /opt/ ! -mtime -30 -name '*.txt' -delete

条件语句

-a:and 和,并且
-o:or 或者
!:取反

动作

-ls
-delete
-exec

find的动作很少用

# find 命令

## 1. 根据类型查找文件
-type:
	f:普通文件
	d:目录
	p:管道文件
	l:软链接文件
	b:块设备
	c:字符设备
	s:socket文件

### 查找/目录下的所有普通文件
find / -type f

## 2. 根据文件名查找文件
-name:
	'*.conf'
	'conf.*'
	'*conf*'
	'*.conf.*'

## 3. 根据时间查找文件
-atime:access
-ctime:change
-mtime:modify
-7:查找最近7天的文件
+7:查找7天之前的文件,不包含今天
7:查找第7天的文件,不包含今天

find / -mtime +7
find / -mtime 7
find / -mtime -7

## 4. 根据文件大小查找文件
-size:
	+:大于
	-:小于
	N:等于

## 5. 条件语句
-a:and和
-o:or或者
!:取反

## 6. 动作
-ls  : 查看
-delete: 删除
## 根据用户查找文件
-user:查找属主是X个用户
-group:查找属组是X个组
-nouser:查找没有用户
-nogroup:查找没有组

[root@localhost opt]# find ./ -user zls|xargs ls -l
-rw-r--r--. 1 zls qiandao 0 Apr 16 00:36 ./file1
-rw-r--r--. 1 zls zls     0 Apr 16 00:36 ./file3

[root@localhost opt]# find ./ -user zls -group qiandao
./file1

[root@localhost opt]# find ./ -user zls -o -group qiandao
./file1
./file3
./file4
## 根据层级深度查找
-maxdepth level

[root@localhost ~]# find /etc/ -maxdepth 2 -type f -name '*.conf'
## 根据文件权限查找
-perm

## 精确查找
[root@localhost opt]# find /opt/ -perm 644

## 包含指定权限的文件
root@localhost opt]# find /opt/ -perm -222

-222:and
/222:or

属主 属组 其它用户
- 1    2     3
  x    w     wx
  r-x  rwx   rwx
  
/ 1    2     3
  x    w     wx
  r-x  rwx   rwx
  rw   wx    rwx
  r-x  r     r
-print:打印出查找的内容,find默认就会打印
-ls:查看找出的文件相信信息
[root@localhost ~]# find /opt/ ! -perm /222 -ls

-delete
[root@localhost opt]# find /opt/ -type d ! -name 'opt'|xargs rm -fr


-ok
语法: -ok \;
-exec
语法: -exec \;

## 拷贝找到的文件到/tmp下
[root@localhost opt]# find /opt/ -mtime +5 |xargs cp -t /tmp/
[root@localhost opt]# find /opt/ -mtime +5 -exec cp {} /tmp/ \;
[root@localhost opt]# find /opt/ -mtime +5 |xargs -I {} cp {} /tmp/
[root@localhost opt]# find /opt/ -name '*.txt' -ok  cp {} /tmp/ \;
< cp ... /opt/2020-04-01_file.txt > ? y
< cp ... /opt/2020-04-02_file.txt > ? y
< cp ... /opt/2020-04-03_file.txt > ? y
< cp ... /opt/2020-04-04_file.txt > ? y
< cp ... /opt/2020-04-05_file.txt > ? y
< cp ... /opt/2020-04-06_file.txt > ? y
< cp ... /opt/2020-04-07_file.txt > ? y
< cp ... /opt/2020-04-08_file.txt > ? y
< cp ... /opt/2020-04-09_file.txt > ? y
< cp ... /opt/2020-04-10_file.txt > ? y
< cp ... /opt/2020-04-11_file.txt > ? y
< cp ... /opt/2020-04-12_file.txt > ? y
< cp ... /opt/2020-04-13_file.txt > ? y
< cp ... /opt/2020-04-14_file.txt > ? y
< cp ... /opt/2020-04-15_file.txt > ? y
< cp ... /opt/2020-04-16_file.txt > ? y
< cp ... /opt/zls.txt > ? y


## find 结合xargs
#拷贝
find  / -type f |xargs cp -t /tmp
#查看
find  / -type f |xargs ls -l
#替换
find  / -type f |xargs sed -i 's###g'
#移动
find  / -type f |xargs mv -t /tmp
#删除
find  / -type f |xargs rm -fr

01.找出/tmp目录下,属主不是root,且文件名不以f开头的文件
[root@wzh ~]# find /tmp -type f ! -user root ! -name 'f'
02.查找/etc/目录下,所有.conf后缀的文件
[root@wzh ~]# find /etc/ -type f -name '
.conf'

03.查找/var目录下属主为root,且属组为mail的所有文件
[root@wzh ~]# find /var -type f -user root -group mail
/var/spool/mail/root

04.查找/var目录下7天以前,同时属主不为root,也不是postfix的文件
[root@wzh ~]# find /var -mtime +7 -type f ! -user root -a ! -user postfix

05.查找/etc目录下大于1M且类型为普通文件的所有文件
[root@wzh ~]# find /etc -type f -size +1M
06.查找/etc目录下所有用户都没有写权限的文件
[root@wzh ~]# find /etc -type f -not -perm /222

07.查找/目录下最后创建时间是3天前,后缀是.log的文件
[root@wzh ~]# find / -type f -mtime +3 -name '
.log'
08.查找/目录下文件名包含txt的文件
find / -type f -name 'txt
09.查找/目录下属主是oldboy并且属组是oldboy的文件
[root@wzh ~]# find / -type f -user oldboy -group oldboy

10.查找/目录下属主是oldboy但是属组不是oldboy的文件
[root@wzh ~]# find / -type f -user oldboy ! -group oldboy

11.查找/目录下属主是oldboy或者属主是oldgirl的文件
find / -user oldboy -o -user oldgirl -type f
12.查找/tmp目录下属主既不是oldboy,也不是oldgirl的文件
find /tmp ! -user oldboy -a ! -user oldgirl -type f
13.查找/var/log目录下7天以前的文件
find /var/log -type f -mtime +7
14.查找/home目录下,类型是目录的,并且属主是oldboy的目录
find /home -type d -user oldboy
15.查找/var/log下大于100kb且以log结尾的所有文件
find /var/log -type f -size +100k -name '*log'
16.查找tmp目录下所属组group1,所属主user1的目录
find /tmp -type d -user user1 -group group1

17.同时查找根目录下名为1.txt,2.txt的文件和名字带a的目录
find / (-type f -name ' 1.txt ' -o -name '2.txt' ) -o (-type d -name 'a' )
18.查找/tmp目录下所有文件并删除
find /tmp -type f -delete
19.查找根目录下所有的隐藏目录
[root@wzh ~]# find / -type d -name '.'
20.查找根目录下以rpm结尾的所有文件
[root@wzh ~]# find / -type f -name ‘*rpm’

21.查找/data/bak目录下15天以前的文件删除(自行修改系统时间模拟相关环境)
find /tmp -type f -mtime +15 -delete
22.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
find $(touch file{1..10}) -type f ! -name ‘file9’ -delete

23.查找/app/logs下7天以前的log文件并删除(至少三种方法)
find /app/logs -type f -mtime +7 -delete
find /app/logs -type f -mtime +7 -exec rm -f {} ;
find /app/logs -type f -mtime +7 |xargs rm -f
24.将/etc目录下大于100k的文件移动至/tmp下(至少三种方法)
mv find /etc -type f -size +100k /tmp/
find /etc -type f -size +100k -exec mv {} /tmp/ ;
find /etc -type f -size +100k |xargs mv -t /tmp/

25.文件权限为r-x------, 请找出在/oldboy目录下面的所有此权限目录,并复制到/tmp目录(至少三种方法)
find /oldboy -type d -perm /xxx -exec cp -r {} /tmp/ ;
cp -r find /oldboy -type d -perm /500 /tmp/
find /oldboy -type d -perm /500 |xargs cp -rt /tmp/
find /oldboy -type d -perm /500 |xargs -i cp -r {} /tmp/

posted @ 2020-07-12 20:37  王顺子  阅读(455)  评论(0编辑  收藏  举报