1. 使⽤ls查看/etc/⽬录下所有的⽂件信息
ls选项:
-l :以长格式显示文件信息
-A:显示所有文件,包括名称以 . 开头的隐藏文档
-a:显示所有文件,包括.与..
-d:显示目录本身(而不是内容)的属性
-h:提供易读的容量单位(K、M等)
-s:显示每个文件占用的硬盘空间大小
[root@2 ~]# ls -la /etc
2. 使⽤ls查看/etc/⽬录下名包含“a”字⺟的⽂件或者⽬录信息
[root@2 ~]# ls -d /etc/*a*/
3. 使⽤ls查看/etc/⽬录下以".conf"结尾的⽂件信息
[root@2 ~]# ls -l /etc/*.conf
4. 使⽤ls查看/etc/⽬录中以"y"字⺟开头的⽂件信息
[root@2 ~]# ls -l /etc/y*
5. find查找/var/⽬录中以“.log”⽂件
find基本语法:fifind [⽂件路径] [选项 选项的值]
-name 根据⽂件的名称搜索⽂件,⽀持通配符*
-type f 代表普通⽂件,d 代表⽬录
[root@2 ~]# find /var/ -name "*.log" -type f
6. 在opt⽬录下创建test⽬录
[root@2 ~]# mkdir /opt/test
7. 在test⽬录中创建abc.txt,def.txt.ghi.txt,xxx.txt.yyy.txt五个⽂件
[root@2 ~]# cd /opt/test
[root@2 test]# touch abc.txt def.txt ghi.txt xxx.txt yyy.txt
[root@2 test]# ls
abc.txt def.txt ghi.txt xxx.txt yyy.txt
8. 修改以上5个⽂件的最后修改时间分别为15,14,13,12,11⽇
使用stat命令获取文件的时间信息:stat 文件路径
修改或创建配置文件的修改时间:touch -m -d "日期时间格式" 文件名称
文件不存在就创建并修改时间,存在的话就只配置最后时间
[root@2 test]# stat /opt/test/abc.txt
文件:"/opt/test/abc.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:16780288 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:usr_t:s0
最近访问:2024-07-16 00:21:14.239980662 +0800
最近更改:2024-07-16 00:21:14.239980662 +0800
最近改动:2024-07-16 00:21:14.239980662 +0800
创建时间:-
[root@2 test]# touch -m -d "2024-07-15 00:00" /opt/test/abc.txt
[root@2 test]# stat /opt/test/abc.txt
文件:"/opt/test/abc.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:16780288 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:usr_t:s0
最近访问:2024-07-16 00:21:14.239980662 +0800
最近更改:2024-07-15 00:00:00.000000000 +0800
最近改动:2024-07-16 00:38:29.797933618 +0800
创建时间:-
[root@2 test]# touch -m -d "2024-07-14 00:00" /opt/test/def.txt
[root@2 test]# touch -m -d "2024-07-13 00:00" /opt/test/ghi.txt
[root@2 test]# touch -m -d "2024-07-12 00:00" /opt/test/xxx.txt
[root@2 test]# touch -m -d "2024-07-11 00:00" /opt/test/yyy.txt
9. 在test⽬录下创建a⽬录
[root@2 test]# mkdir /opt/a
[root@2 test]# ls /opt
a test
10. 将以上5个⽂件复制⼀份到a⽬录中
cp — Copy 复制
格式:cp [选项]... 原文件… 目标路径
常用命令选项
-r:递归,复制目录时必须有此选项,文件使用无影响
[root@2 test]# cp -r abc.txt def.txt ghi.txt xxx.txt yyy.txt /opt/a/
[root@2 test]# ls
abc.txt def.txt ghi.txt xxx.txt yyy.txt
[root@2 test]# cd /opt/a
[root@2 a]# ls
abc.txt def.txt ghi.txt xxx.txt yyy.txt
11. 将a⽬录⽂件做成bak.tar.gz⽂件保存到家⽬录中
tar是集成打包,较少占用磁盘空间
压缩格式:.gz gzip压缩格式 压缩速度快
.bz2 bzip2压缩格式
.xz xz压缩格式 空间占用小
压缩:tar [选项] /路径/压缩文件的名称 /被打包压缩的文件1 /被打包的文件2
解压缩:tar [选项] /路径/被压缩的文件名称 [选项] /目标位置
tar常用选项
-c 创建归档
-x 释放归档
-f 指定归档文档名称
-z,-j ,-J 代表调用 相应的压缩格式 -z代表 gz -j代表 bz2 -J 代表 xz
-t 列出压缩文件里的内容 --清单
-C 解压可以指定解压的目录
[root@2 a]# tar -zcf /home/bak.tar.gz .
[root@2 a]# ls /home
bak.tar.gz ww
12. 使⽤find删除test⽬录下3天前的⽂件
通过文件的最后修改时间搜索文件:find 文件路径 -mtime -days/+days
-mtime 根据⽂件最后修改时间搜索⽂件
+号 搜索⼏天之前的⽂件信息
-号 搜索⼏天之内的⽂件信息
find的-exec rm -rf {} \; 表示对符合条件的每个文件执行rm -rf 来删除它,{}是一个占位符,代表找到的文件名
[root@2 ~]# find /opt/test -type f -mtime +3 -exec rm -f {} \;
[root@2 ~]# ls /opt/test
abc.txt def.txt ghi.txt
13. find删除opt⽬录下3天内的⽂件
[root@2 opt]# find . -type f -mtime -3 -exec rm -f {} \; #.代表当前目录
[root@2 opt]# ls #a和test都是目录,不是文件
a test
14. find删除正好第三天的⽂件
[root@2 opt]# find /opt/test -type f -mtime 2 -exec rm -f {} \; #-mtime表示选择修改时间在当前时间之前两天(刚好第三天)的文件
[root@2 opt]# ls /opt/test
ghi.txt
15. 将/opt/test/a⽬录中的⽂件复制⼀份到/opt/test/⽬录下
[root@2 opt]# find /opt/test/a -type f -exec cp --parents \{\} /opt/test \; #找到/opt/test/a中的所有文件,并将它们复制到/opt/test目录中,同时保持目录结构
16. 创建⽬录/opt/test0
[root@2 test]# mkdir /opt/test0
17. 在/opt/test0/⽬录中创建三个⽂件 a.mp4(5M),b.mp4(20M),c.mp4(80M)
扩展命令 dd 使⽤dd创建扩展命令 ⽣成指定⼤⼩的测试⽂件
语法 dd if=/dev/zero of=⽂件名称 bs=1M count=1
if表示输⼊⽂件 of表示输出⽂件 bs代表字节为单位的块⼤⼩ count代表被复制的块
其中/dev/zore是⼀个字符设备,会不断地返回0字节的⽂件
[root@2 opt]# cd test0
[root@2 test0]# dd if=/dev/zero of=a.mp4 bs=5M count=1
记录了1+0 的读入
记录了1+0 的写出
5242880字节(5.2 MB)已复制,0.00466624 秒,1.1 GB/秒
[root@2 test0]# dd if=/dev/zero of=b.mp4 bs=20M count=1
记录了1+0 的读入
记录了1+0 的写出
20971520字节(21 MB)已复制,0.01712 秒,1.2 GB/秒
[root@2 test0]# dd if=/dev/zero of=c.mp4 bs=80M count=1
记录了1+0 的读入
记录了1+0 的写出
83886080字节(84 MB)已复制,0.127206 秒,659 MB/秒
[root@2 test0]# ls
a.mp4 b.mp4 c.mp4
18. 创建⽬录/opt/test0/b/
[root@2 test0]# mkdir b
[root@2 test0]# ls
a.mp4 b b.mp4 c.mp4
19. 将/op t/test0/中的⽂件复制⼀份/opt/test0/b/⽬录中
[root@2 test0]# cp -r a.mp4 b.mp4 c.mp4 /opt/test0/b/
[root@2 test0]# ls /opt/test0/b/
a.mp4 b.mp4 c.mp4
20. find查询/opt/test0/⽬录中⽂件⼤于20M的,并删除
根据文件size大小搜索文件:find 文件路径 -size 文件大小 [常用单位K M G]
size值 搜索等于size的⽂件
-size值 【0,size值)
+size值 (size值,正⽆穷)
[root@2 test0]# find /opt/test0/ -size +20M -exec rm -rf {} \;
[root@2 test0]# ls
a.mp4 b b.mp4
21. find查询/opt/test0/⽬录中⽂件⼩于20M的⽂件并删除
[root@2 test0]# find /opt/test0/ -type f -size -20M -delete
[root@2 test0]# ls
b b.mp4
22. find查找/opt/test0/⽬录中⽂件size为20M的⽂件并删除
[root@2 test0]# find /opt/test0/ -size 20M -exec rm -rf {} \;
[root@2 test0]# ls
b
23. /opt/test0/b中的⽂件复制⼀份到/opt/test0中
[root@2 b]# cp a.mp4 b.mp4 c.mp4 /opt/test0
[root@2 b]# cd /opt/test0
[root@2 test0]# ls
a.mp4 b b.mp4 c.mp4
24. 打开新的虚拟主机
25. 将家⽬录中的bak.tar.gz⽂件上传到新主机的/opt⽬录中
使⽤scp下载⽂件和⽬录
语法 scp [选项] ⽤户名@linux主机地址:/资源路径 linux本地⽂件路径
[root@1 ~]# scp root@192.168.2.4:/home/bak.tar.gz /opt/ #新的虚拟主机,获取原主机的文件
Are you sure you want to continue connecting (yes/no)? yes
root@192.168.2.4's password:
bak.tar.gz 100% 177 108.1KB/s 00:00
[root@1 ~]# ls /opt
bak.tar.gz
26. 将新主机的/etc/skel/⽬录下载到 当前主机的/opt⽬录中
复制目录需要添加-r选项,否则无法执行
[root@2 test0]# scp -r root@192.168.2.3:/etc/skel/ /opt/
root@192.168.2.3's password:
.bash_logout 100% 18 7.4KB/s 00:00
.bash_profile 100% 193 105.1KB/s 00:00
.bashrc 100% 231 125.2KB/s 00:00
[root@2 test0]# ls /opt
a skel test test0
27. 设置计划任务,每周3将/etc/yum.repos.d/⽬录下的.repo⽂件压缩保存到tmp,在⽂件名中添加时间戳
计划任务:crontab [选项]
选项:-l list查看当前⽤户的计划任务信息
-e edit编写计划任务
编写计划任务:crontab 分 时 ⽇ ⽉ 周 要使⽤的完整路径(which命令)(0,7都代表周日)
[root@1 ~]# crontab -e
0 0 * * 3 /usr/bin/tar -zcvf /tmp/yum-$(data "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/yum.repos.d/*.repo
crontab: installing new crontab
[root@1 ~]# ls -l /tmp/
总用量 0