今天进行CentOS7.X文件管理常用命令的练习及记录,再开始前,补充一下VMware虚拟机网络知识点:
1、 什么是桥接网络和nat网络模式
1.1 Vmware Workstations安装后,会在我们的物理机上面生成两个虚拟网卡,分别为Vmnet1和Vmnet8,如下图
1.2 在Vmware Workstations软件层面可以看到如下三种虚拟网络类型
- Vmnet0(桥接模式):
该模式在物理机没有出现该虚拟网卡,直接桥接到物理机的物理网卡,有线或者无线网卡都可以,跟物理机使用同个网段,如果需要上外网,可以将DNS设置为跟物理机网卡一样即可
- Vmnet8(NAT模式):
该模式跟物理机的虚拟网卡Vmnet8进行连接,这样物理机就可以直接通过工具,如XSHELL进行SSH连接;跟物理机是不同网段【Vmnet8有自身的DHCP服务】,可以直接上外网,原理就是通过NAT技术,虚拟机拥有的网段可以看做是内网,物理机的网段可以看做是外网,通过NAT将虚拟机内网IP地址映射到物理机外网IP地址,这样就可以通过物理机的IP地址去访问外网了
- Vmnet1(仅主机模式):
该模式较为少用,跟物理机的虚拟网卡Vmnet1进行连接,与物理机是不同网段【Vmnet1有自身的DHCP服务】;同样,物理机就可以直接通过工具,如XSHELL进行SSH连接;无法访问外网,是一个独立的内网;如果需要访问外网,需要采用“桥接模式”或者“NAT”模式
备注:如果电脑经常移动,IP地址更换频繁,不建议使用桥接模式,因为这样虚拟机的IP也会经常变动,建议采用NAT模式
2、 静态ip配置步骤
本次实验以NAT模式进行测试
检查当前网卡配置信息,网络协议为DHCP
IP地址为192.168.136.128/24,网关为192.168.136.2
如下为修改为静态IP地址后的测试情况
[root@node1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none #将dhcp修改为none或者static即可
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens32
UUID=37032192-1d26-4545-afcb-89bee35526e9
DEVICE=ens32
ONBOOT=yes
IPV6_PRIVACY=no
#添加如下静态IP地址信息
#注意IP地址不要与网络中其他机器的IP一样,否则会导致IP地址冲突,出现网络不通的情况
IPADDR=192.168.136.100
GATEWAY=192.168.136.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8
重启网络服务,可以看到,IP地址已经变为192.168.136.100,并且测试网络正常,如下
文件管理常用命令
1、Linux目录说明:
/boot: 放置linux系统启动时的内核文件以及引导文件
/dev:存放linux系统下的设备文件
/etc: 系统配置文件存放的目录
/home: 系统默认的普通用户家目录
/lib,lib64: 动态连接共享库
/media: 挂载可移动的设备
/opt: 存放第三方软件
/root: 管理员家目录
/bin,/sbin: 可执行二进制命令
/run: 临时文件系统目录
/srv: 早期存放数据目录
/tmp: 存放临时文件
/usr: 应用程序目录
/var: 存放动态文件,比如日志
/mnt: 临时挂载目录
2、ls命令
-l 长格式显示文件信息
-d 显示目录本身信息
-a 显示全部文件,包括隐藏的文件
-A 显示除了.或者..以外的文件,包括隐藏文件
-t 按修改时间进行排序
-r 反向排序
##-l显示详细信息
[root@node1 tmp]# ls -l
总用量 4
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
1 2 3 4 5 6 7
1:文件类型
-:普通文件 d:目录 l:链接文件 c:字符设备(即串行端口的接口设备,例如伪终端等) b:块设备(磁盘)
s:套接字文件(通常用在网络数据连接) p:管道文件
2:权限
r(4)-只读;w(2)-写入;x(1)-执行;-:表示没有权限
rw-------:每三位代表一组权限,分别为属主权限、属组权限以及其他用户权限
3:文件数量,文件类型数量为1,目录则为包含的文件数
4:所属用户和属组
5:文件大小
6:文件日期
7:文件名称
##-d显示当前文件夹
[root@node1 tmp]# ls -ld
drwxrwxrwt. 10 root root 279 11月 3 11:03 .
##-a显示隐藏文件,Linux中前面带点的文件为隐藏文件
[root@node1 tmp]# ls -la
总用量 4
drwxrwxrwt. 10 root root 279 11月 3 11:03 . #一点:表示当前目录
dr-xr-xr-x. 17 root root 224 11月 1 13:58 .. #两点:表示上一级目录
drwxrwxrwt. 2 root root 6 11月 1 13:54 .font-unix
drwxrwxrwt. 2 root root 6 11月 1 13:54 .ICE-unix
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxrwxrwt. 2 root root 6 11月 1 13:54 .Test-unix
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
drwxrwxrwt. 2 root root 6 11月 1 13:54 .X11-unix
drwxrwxrwt. 2 root root 6 11月 1 13:54 .XIM-unix
-rw-------. 1 root root 0 11月 1 13:52 yum.log
##-A显示除了.或者..以外的文件,包括隐藏文件
[root@node1 tmp]# ls -lA
总用量 4
drwxrwxrwt. 2 root root 6 11月 1 13:54 .font-unix
drwxrwxrwt. 2 root root 6 11月 1 13:54 .ICE-unix
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxrwxrwt. 2 root root 6 11月 1 13:54 .Test-unix
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
drwxrwxrwt. 2 root root 6 11月 1 13:54 .X11-unix
drwxrwxrwt. 2 root root 6 11月 1 13:54 .XIM-unix
-rw-------. 1 root root 0 11月 1 13:52 yum.log
##-t按修改时间进行排序
[root@node1 tmp]# ls -lt
总用量 4
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
-rw-------. 1 root root 0 11月 1 13:52 yum.log
##-rt按修改时间进行反向排序
[root@node1 tmp]# ls -lrt
总用量 4
-rw-------. 1 root root 0 11月 1 13:52 yum.log
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
##-r默认按文件名称进行反向排序
[root@node1 tmp]# ls -lr
总用量 4
-rw-------. 1 root root 0 11月 1 13:52 yum.log
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
3、alias命令
设置别名,方便执行较为常用或者复制的命令
查看当前设置的别名:直接执行alias命令即可
如下命令为临时设置别名,在其他会话窗口不生效,或者当前窗口关闭就失效了
设置永久别名,需要在.bashrc里面添加即可
取消别名命令如下
- 临时取消:unalias v32
- 永久取消:需要在.bashrc里面删除相应记录
4、cat命令
4.1用于查看文件内容
-n或--number:从1开始对所有输出的行数编号;
-b或--number-nonblank:对于空白行不编号;
-s或--squeeze-blank:当遇到有连续两行以上的空白行,合并为一行显示;
-A:行尾显示“$”;
##查看文件内容如下
[root@node1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 1 13:52:59 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7af07980-d9ec-486f-9d73-29e659817d35 / xfs defaults 0 0
UUID=7e21952f-eba0-41d3-8388-992582c2b7f7 /boot xfs defaults 0 0
UUID=06aad946-4d53-4d18-afea-8472a4a1d647 swap swap defaults 0 0
##-n对所有行进行编号,包括空白行
[root@node1 ~]# cat /etc/fstab -n
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Sun Nov 1 13:52:59 2020
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=7af07980-d9ec-486f-9d73-29e659817d35 / xfs defaults 0 0
10 UUID=7e21952f-eba0-41d3-8388-992582c2b7f7 /boot xfs defaults 0 0
11 UUID=06aad946-4d53-4d18-afea-8472a4a1d647 swap swap defaults 0 0
##-b对于空白行不进行编号
[root@node1 ~]# cat /etc/fstab -b
1 #
2 # /etc/fstab
3 # Created by anaconda on Sun Nov 1 13:52:59 2020
4 #
5 # Accessible filesystems, by reference, are maintained under '/dev/disk'
6 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
7 #
8 UUID=7af07980-d9ec-486f-9d73-29e659817d35 / xfs defaults 0 0
9 UUID=7e21952f-eba0-41d3-8388-992582c2b7f7 /boot xfs defaults 0 0
10 UUID=06aad946-4d53-4d18-afea-8472a4a1d647 swap swap defaults 0 0
[root@node1 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Nov 1 13:52:59 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7af07980-d9ec-486f-9d73-29e659817d35 / xfs defaults 0 0
UUID=7e21952f-eba0-41d3-8388-992582c2b7f7 /boot xfs defaults 0 0
UUID=06aad946-4d53-4d18-afea-8472a4a1d647 swap swap defaults 0 0
##如上面文件,存在很多空行
-s可以将连续两行以上的空白行,合并为一行显示
[root@node1 ~]# cat /etc/fstab -s
#
# /etc/fstab
# Created by anaconda on Sun Nov 1 13:52:59 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7af07980-d9ec-486f-9d73-29e659817d35 / xfs defaults 0 0
UUID=7e21952f-eba0-41d3-8388-992582c2b7f7 /boot xfs defaults 0 0
UUID=06aad946-4d53-4d18-afea-8472a4a1d647 swap swap defaults 0 0
##-A在行尾显示$符号
[root@node1 ~]# cat /etc/fstab -A
$
$
$
$
#$
# /etc/fstab$
# Created by anaconda on Sun Nov 1 13:52:59 2020$
#$
# Accessible filesystems, by reference, are maintained under '/dev/disk'$
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info$
#$
UUID=7af07980-d9ec-486f-9d73-29e659817d35 / xfs defaults 0 0$
UUID=7e21952f-eba0-41d3-8388-992582c2b7f7 /boot xfs defaults 0 0$
UUID=06aad946-4d53-4d18-afea-8472a4a1d647 swap swap defaults 0 0$
4.2 用于创建文件
命令解析:
- >:用于创建文件或者覆盖现有文件内容
- >>:以追加的方式向文件添加内容
- eof(end of file)为结束符,可以自定义,如上面的f、rr
5、cp命令
用于将一个或多个源文件或者目录复制到指定的目的文件或目录
-a:此参数的效果和同时指定"-dpR"参数相同
-d:可复制符号连接,不会破坏链接文件与源文件的链接关系
-f:强行复制文件或目录,不论目标文件或目录是否已存在
-i:覆盖既有文件之前先询问用户
-l:对源文件建立硬连接,而非复制文件
-p:保留源文件或目录的属性
-R/r:递归处理,将指定目录下的所有文件与子目录一并处理
-s:对源文件建立符号连接,而非复制文件(指定绝对路径)
-u:使用该参数后只会在源文件的更改时间较目标文件要新的时候或是目标文件并不存在时,才复制文件
-S:在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀~
-b:覆盖已存在的文件目标前将目标文件备份,后缀默认是~
-v:详细显示命令执行的操作
测试环境相关目录如下
##-d用于复制软连接,注意:创建软连接最好用绝对路径,否则复制的时候软连接可能会失效
[root@node1 ~]# cp /tmp/test/4.txt /tmp
[root@node1 ~]# cd /tmp
[root@node1 tmp]# ll
总用量 4
-rw-r--r--. 1 root root 0 11月 4 09:24 4.txt ##没有加-d参数,链接文件变为普通文件
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
[root@node1 test]# cp -d /tmp/test/4.txt /tmp/
cp:是否覆盖"/tmp/4.txt"? y
[root@node1 test]# cd ..
[root@node1 tmp]# ll
总用量 4
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
##-u只复制源文件有更新时,源文件没有更新将不复制
备注:默认系统cp是别名,采用反斜杠 \cp可直接调用命令
#先复制文件到tmp目录
[root@node1 test]# cp /tmp/test/1.txt /tmp/
cp:是否覆盖"/tmp/1.txt"? y
#更新源文件
[root@node1 test]# echo "1234" > /tmp/test/1.txt
#采用-u参数直接复制
[root@node1 test]# \cp -u /tmp/test/1.txt /tmp/
[root@node1 test]# cd ..
[root@node1 tmp]# cat 1.txt
1234
#更新了目标文件,可以查看到当前文件时间为10:28
[root@node1 tmp]# ll
总用量 8
-rw-r--r--. 1 root root 5 11月 4 10:28 1.txt
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
#查看系统日期,在使用-u参数进行复制,可以看到文件没有被更新
[root@node1 tmp]# date
2020年 11月 04日 星期三 10:29:30 CST
[root@node1 tmp]# \cp -u /tmp/test/1.txt /tmp/
[root@node1 tmp]# ll
总用量 8
-rw-r--r--. 1 root root 5 11月 4 10:28 1.txt
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
##-f强制复制文件或者目录,可以看到文件时间被更新了
[root@node1 tmp]# \cp -f /tmp/test/1.txt /tmp/
[root@node1 tmp]# ll
总用量 8
-rw-r--r--. 1 root root 5 11月 4 10:29 1.txt
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
##-S复制同名文件时,对目标的原文件进行备份
#确认目标源文件为空的
[root@node1 tmp]# ll
总用量 8
-rw-r--r--. 1 root root 5 11月 4 10:48 1.txt
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
[root@node1 tmp]# > 1.txt
[root@node1 tmp]# cat 1.txt
#进行-S复制
[root@node1 tmp]# \cp -S ".`date +%F`" /tmp/test/1.txt /tmp/
#可以看到多了一个文件为1.txt.2020-11-04
[root@node1 tmp]# ll
总用量 8
-rw-r--r--. 1 root root 5 11月 4 10:49 1.txt
-rw-r--r--. 1 root root 0 11月 4 10:49 1.txt.2020-11-04
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
#检查文件内容,发现之前空文件的1.txt备份为了1.txt.2020-11-04
[root@node1 tmp]# more 1.txt
1234
[root@node1 tmp]# more 1.txt.2020-11-04
[root@node1 tmp]#
##-b和-S功能差不多,都是对目标文件进行备份
-b默认后缀为~ -S可以自己指定后缀
#清空目标文件1.txt
[root@node1 tmp]# > 1.txt
#使用-b进行复制
[root@node1 tmp]# \cp -b /tmp/test/1.txt /tmp/
#可以看到多了文件1.txt~,后缀为~,该文件也是对目标文件的备份
[root@node1 tmp]# ll
总用量 8
-rw-r--r--. 1 root root 5 11月 4 11:01 1.txt
-rw-r--r--. 1 root root 0 11月 4 11:01 1.txt~
-rw-r--r--. 1 root root 0 11月 4 10:49 1.txt.2020-11-04
lrwxrwxrwx. 1 root root 15 11月 4 09:26 4.txt -> /tmp/test/1.txt
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
[root@node1 tmp]# more 1.txt~
[root@node1 tmp]# more 1.txt
1234
##-a是一个通用参数,相当于同时使用-d,-p-r三个参数,可以实现递归,复制软连接,保留文件属性
#查看test文件夹相关内容
[root@node1 test]# ll
总用量 8
-rw-r--r--. 2 test test 5 11月 4 10:27 1.txt
-rw-r--r--. 1 root root 0 11月 4 09:12 2.txt
-rw-r--r--. 2 test test 5 11月 4 10:27 3.txt
lrwxrwxrwx. 1 root root 15 11月 4 09:13 4.txt -> /tmp/test/1.txt
drwxr-xr-x. 3 root root 15 11月 4 09:12 a
#使用-a参数进行复制
[root@node1 test]# \cp -a /tmp/test /mnt
[root@node1 test]# cd /mnt
[root@node1 mnt]# ll
总用量 0
drwxr-xr-x. 3 root root 67 11月 4 09:13 test
[root@node1 mnt]# cd test/
#可以看到所有文件和目录都复制过来了,而且文件属性都没有更改
[root@node1 test]# ll
总用量 8
-rw-r--r--. 2 test test 5 11月 4 10:27 1.txt
-rw-r--r--. 1 root root 0 11月 4 09:12 2.txt
-rw-r--r--. 2 test test 5 11月 4 10:27 3.txt
lrwxrwxrwx. 1 root root 15 11月 4 09:13 4.txt -> /tmp/test/1.txt
drwxr-xr-x. 4 root root 27 11月 4 11:13 a
6、touch命令
用于创建普通文件和修改文件时间
-a 只修改访问时间(属性更改时间也会变)
-m 只更改修改时间(属性更改时间也会变)
stat file 查看文件的属性
[root@node1 test]# touch 1.txt
[root@node1 test]#
[root@node1 test]# stat 1.txt
文件:"1.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:33575032 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-11-04 11:25:54.144502265 +0800
最近更改:2020-11-04 11:25:54.144502265 +0800
最近改动:2020-11-04 11:25:54.144502265 +0800
创建时间:-
[root@node1 test]# touch -a -t 202011031125 1.txt
[root@node1 test]# stat 1.txt
文件:"1.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:33575032 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-11-03 11:25:00.000000000 +0800
最近更改:2020-11-04 11:25:54.144502265 +0800
最近改动:2020-11-04 11:32:16.739672928 +0800
创建时间:-
[root@node1 test]# touch -m -t 202011031125 1.txt
[root@node1 test]# stat 1.txt
文件:"1.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:803h/2051d Inode:33575032 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_tmp_t:s0
最近访问:2020-11-03 11:25:00.000000000 +0800
最近更改:2020-11-03 11:25:00.000000000 +0800
最近改动:2020-11-04 11:33:03.478060269 +0800
创建时间:-
7、mkdir命令
-p 自动创建上级目录,如果上级目录不存在;如果目录已经,则不创建,不会提示报错
-m 可以指定创建目录时的权限
[root@node1 test]# mkdir test2
[root@node1 test]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月 4 11:41 test2
#因上级目录test3不存在
[root@node1 test]# mkdir test3/test4
mkdir: 无法创建目录"test3/test4": 没有那个文件或目录
#-p可以自动创建上级目录,不管有多少层
[root@node1 test]# mkdir -p test3/test4
[root@node1 test]# cd test3
[root@node1 test3]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月 4 11:42 test4
#-m在创建目录时,同时指定权限
[root@node1 test]# mkdir -m 700 test4
[root@node1 test]# ll
总用量 0
drwxr-xr-x. 2 root root 6 11月 4 11:41 test2
drwxr-xr-x. 3 root root 19 11月 4 11:42 test3
drwx------. 2 root root 6 11月 4 11:43 test4
8、less命令
用于查看大文件,分页显示
空格:显示下一屏内容
b:显示上一屏内容
f:显示下一屏内容
q:退出
9、more命令
也用于查看大文件,实现分页显示
空格:显示下一屏内容
b:显示上一屏内容
f:显示下一屏内容
crtl+c:退出
备注:底下会显示百分比
10、gzip命令
压缩文件,默认压缩完后会删除源文件
-c 将压缩结果通过重定向至其他文件,以此保留源文件
-d 解压缩
-r 递归压缩目录内文件,只压缩文件,不会压缩目录
-1~9 指定压缩级别,-1最快压缩,-9最大压缩,更消耗cpu,默认级别是6
[root@node1 test]# du -sh testfile
200M testfile
#压缩文件,默认会删除源文件
[root@node1 test]# gzip testfile
[root@node1 test]# du -sh testfile.gz
200K testfile.gz
#解压文件
[root@node1 test]# gunzip testfile.gz
[root@node1 test]# ll
总用量 204800
-rw-r--r--. 1 root root 209715200 11月 4 14:28 testfile
#压缩文件并保留源文件
[root@node1 test]# gzip -c testfile > testfile.gz
[root@node1 test]# ll
总用量 205000
-rw-r--r--. 1 root root 209715200 11月 4 14:28 testfile
-rw-r--r--. 1 root root 203556 11月 4 14:34 testfile.gz
#解压文件的另一种方式
[root@node1 test]# gzip -d testfile.gz
#不解压,直接查看文件内容
[root@node1 test]# zcat testfile.gz
[root@node1 test]# ll
总用量 205000
drwxr-xr-x. 2 root root 6 11月 4 14:37 a
drwxr-xr-x. 2 root root 6 11月 4 14:37 b
-rw-r--r--. 1 root root 209715200 11月 4 14:37 testfile2
-rw-r--r--. 1 root root 203556 11月 4 14:34 testfile.gz
#压缩目录下的所有文件,不会压缩目录,并显示压缩过程
[root@node1 tmp]# gzip -rv test/
gzip: test//testfile.gz already has .gz suffix -- unchanged
test//testfile2: 99.9% -- replaced with test//testfile2.gz
[root@node1 test]# ll
总用量 400
drwxr-xr-x. 2 root root 6 11月 4 14:37 a
drwxr-xr-x. 2 root root 6 11月 4 14:37 b
-rw-r--r--. 1 root root 203557 11月 4 14:37 testfile2.gz
-rw-r--r--. 1 root root 203556 11月 4 14:34 testfile.gz
11、bzip2命令
压缩文件,默认压缩完会删除源文件,不支持递归压缩目录文件
-k 保留源文件
-d 解压缩
-1~9 定义压缩级别
[root@node1 test]# du -sh *
0 a
0 b
200M testfile2
200K testfile.gz
#保留源文件,压缩文件
[root@node1 test]# bzip2 -k testfile2
#bzip2的压缩比,比gzip和zip高很多
[root@node1 test]# du -sh *
0 a
0 b
200M testfile2
4.0K testfile2.bz2
200K testfile.gz
#解压文件
[root@node1 test]# bzip2 -d testfile2.bz2
bzip2: Output file testfile2 already exists.
[root@node1 test]# rm -rf testfile2
[root@node1 test]# bzip2 -d testfile2.bz2
[root@node1 test]# du -sh *
0 a
0 b
200M testfile2
200K testfile.gz
#默认压缩,会删除源文件
[root@node1 test]# bzip2 testfile2
[root@node1 test]# ll
总用量 204
drwxr-xr-x. 2 root root 6 11月 4 14:37 a
drwxr-xr-x. 2 root root 6 11月 4 14:37 b
-rw-r--r--. 1 root root 177 11月 4 14:37 testfile2.bz2
-rw-r--r--. 1 root root 203556 11月 4 14:34 testfile.gz
#不解压,直接查看文件内容
[root@node1 test]# bzcat testfile2.bz2
12、zip命令
压缩文件,默认压缩完不会删除源文件,可以压缩目录
安装zip和unzip软件包命令
yum install zip -y
yum install unzip -y
#压缩文件,默认保留源文件
[root@node1 test]# zip testfile2.zip testfile2
adding: testfile2 (deflated 100%)
[root@node1 test]# du -sh *
0 a
0 b
200M testfile2
200K testfile2.zip
200K testfile.gz
#指定压缩级别
[root@node1 test]# zip -9 testfile2-1.zip testfile2
adding: testfile2 (deflated 100%)
#压缩目录,并删除源文件(-r:递归 -m:删除源文件)
[root@node1 tmp]# zip -rm test.zip test/
adding: test/ (stored 0%)
adding: test/testfile.gz (deflated 100%)
adding: test/a/ (stored 0%)
adding: test/b/ (stored 0%)
adding: test/testfile2 (deflated 100%)
adding: test/testfile2.zip (stored 0%)
adding: test/testfile2-1.zip (stored 0%)
[root@node1 tmp]# ll
总用量 604
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
-rw-r--r--. 1 root root 612648 11月 4 15:01 test.zip
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
#解压文件
[root@node1 tmp]# unzip test.zip
Archive: test.zip
creating: test/
inflating: test/testfile.gz
creating: test/a/
creating: test/b/
inflating: test/testfile2
extracting: test/testfile2.zip
extracting: test/testfile2-1.zip
[root@node1 tmp]# ll
总用量 604
-rwx------. 1 root root 836 11月 1 13:59 ks-script-aJFbdw
drwx------. 3 root root 17 11月 3 10:00 systemd-private-a5bc6c5026484f55a572426317d21040-chronyd.service-BsHbZO
drwxr-xr-x. 4 root root 104 11月 4 15:01 test
-rw-r--r--. 1 root root 612648 11月 4 15:01 test.zip
drwx------. 2 root root 6 11月 3 10:00 vmware-root_831-4248090624
drwx------. 2 root root 6 11月 1 14:07 vmware-root_863-3980167256
-rw-------. 1 root root 0 11月 1 13:52 yum.log
13、3.1 tar命令
归档工具,用来打包和备份
- 打包是指将一大堆文件或目录变成一个总的文件
- 压缩则是将一个大的文件通过一些压缩算法变成一个小文件
-c 创建新的tar包
-f 指定tar包名
-r 添加文件到归档文件,须与f结合使用,指定归档文件
-z 指定gzip压缩tar包,后缀为.tar.gz
-j 指定bzip2解压缩文件,后缀为.tar.bz2
-p 保留文件的权限和属性
--remove-files 归档后删除源文件
#-v显示打包过程
[root@node1 tmp]# tar cvf test.tar test
test/
test/testfile.gz
test/a/
test/b/
test/testfile2
test/testfile2.zip
test/testfile2-1.zip
#添加新文件到归档文件里面
[root@node1 tmp]# tar rf test.tar test.zip
#提取归档文件,C指定提取到指定目录
[root@node1 tmp]# tar xvCf /mnt test.tar
test/
test/testfile.gz
test/a/
test/b/
test/testfile2
test/testfile2.zip
test/testfile2-1.zip
test.zip
[root@node1 tmp]# cd /mnt
[root@node1 mnt]# ll
总用量 600
drwxr-xr-x. 7 root root 143 11月 4 15:01 test
-rw-r--r--. 1 root root 612648 11月 4 15:01 test.zip
#更新归档文件里面的文件
[root@node1 tmp]# tar uf test.tar yum.log
#列出归档文件中的内容
[root@node1 tmp]# tar tf test.tar
test/
test/testfile.gz
test/a/
test/b/
test/testfile2
test/testfile2.zip
test/testfile2-1.zip
test.zip
yum.log
大家如果想要一起学习IT技术的,请关注,关于练习环境的搭建,可参考上一篇文章,谢谢!!!
每周不定时更新Linux学习内容
本文暂时没有评论,来添加一个吧(●'◡'●)