文件管理
1.文件系统的目录结构
-
文件和目录被组织成一个单根倒置树结构
-
文件系统从根目录下开始,用“/”表示
-
根文件系统(rootfs):root filesystem
-
以 . 开头的文件为隐藏文件
-
路径分隔的 /
-
每个文件都有两类相关数据:
元数据:metadata,即属性,
数据:data,即文件内容
-
文件名最长255个字节
-
包括路径在内文件名称最长4095个字节
-
蓝色-->目录 绿色-->可执行文件 红色-->压缩文件 浅蓝色-->链接文件 灰色-->其他文件
(文件后缀不同也会使文件颜色变化)
[root@centos8[ ~]#cat /etc/DIR_COLORS "查询Linux中对于文件颜色的配置信息,这个文件定义了什么文件显示什么颜色" gz 01;31 31代表红色
-
除了斜杠和NUL,所有字符都有效.但使用特殊字符的目录名和文件不推荐使用,有些字符需要用引
号来引用 -
标准Linux文件系统(如:ext4),文件名称大小写敏感,例如:MAIL, Mail, mail, mAiL
以 . 开头的文件为隐藏文件() -
Linux的文件系统分层结构:FHS Filesystem Hierarchy Standard
参考文档:http://www.pathname.com/fhs/
2.常见的文件系统目录功能
2.1常见的文件系统目录说明
-
/boot:引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader, grub)都存放于此目录
-
/bin:所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
-
/sbin:管理类的基本命令;不能关联至独立分区,OS启动即会用到的程序
-
/lib:启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules)
-
/lib64:专用于x86_64系统上的辅助共享库文件存放位置
-
/etc:配置文件目录
-
/home/USERNAME:普通用户家目录
-
/root:管理员的家目录
-
/media:便携式移动设备挂载点
-
/mnt:临时文件系统挂载点
-
/misc: 杂项,(这个文件夹你使用的时候自动挂载光盘,不用不会挂载光盘)
光盘挂载需要关联到文件夹中,可以使用下列命令:
[root@centos8[ ~]#lsblk “查看用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息。” NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 100G 0 part / ├─sda3 8:3 0 50G 0 part /data ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 4G 0 part [SWAP] sdb 8:16 0 20G 0 disk sr0 11:0 1 6.7G 0 rom [root@centos8[ ~]#ls /misc “显示misc目录中的所含之文件及子目录” [root@centos8[ ~]#ls /misc -a "显示misc文件夹中的所有文件包含隐藏文件与子目录" . .. [root@centos8[ ~]#cd /misc/cd “切换到misc目录中的子目录cd” [root@centos8[ cd]#ls “查看当前目录显示的是cd目录下的文件与子目录” AppStream BaseOS EFI images isolinux media.repo TRANS.TBL [root@centos8[ cd]#cd .. “返回上一级目录” [root@centos8[ misc]#pwd “立刻获得您目前所在的工作目录的绝对路径命称” /misc [root@centos8[ misc]#ls “查看当前目录中的子目录与文件” cd [root@centos8[ misc]#lsblk “查看用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系,但是它不会列出RAM盘的信息。” NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 100G 0 part / ├─sda3 8:3 0 50G 0 part /data ├─sda4 8:4 0 1K 0 part └─sda5 8:5 0 4G 0 part [SWAP] sdb 8:16 0 20G 0 disk sr0 11:0 1 6.7G 0 rom /misc/cd “查看完成后会发现这一条信息:cd光盘已经关联到misc目录中” [09:51:43 root@centos8[ misc]#
要想实现上述功能需要启用服务 [09:51:43 root@centos8[ misc]#systemctl status autofs “输入这个命令可以检查是否有这个功能” ● autofs.service - Automounts filesystems on demand Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled) Active: active (running) since Sun 2020-03-22 22:47:26 CST; 1 day 11h ago Main PID: 1613 (automount) “检查active (running)这个代码是否是绿色正在运行,如果是红色报错状态就需要安装这个功能” Tasks: 5 (limit: 12400) Memory: 5.8M CGroup: /system.slice/autofs.service └─1613 /usr/sbin/automount --systemd-service --dont-check-daemon Mar 22 22:47:26 centos8.magedu.org automount[1613]: setautomntent: lookup(sss): setautomntent: No such file> Mar 22 22:47:26 centos8.magedu.org systemd[1]: Started Automounts filesystems on demand. Mar 24 09:13:14 centos8.magedu.org automount[1613]: key "selinuxfs" not found in map source(s). Mar 24 09:13:14 centos8.magedu.org automount[1613]: key "bpf" not found in map source(s). Mar 24 09:16:45 centos8.magedu.org automount[1613]: key "selinuxfs" not found in map source(s). Mar 24 09:16:45 centos8.magedu.org automount[1613]: key "bpf" not found in map source(s). Mar 24 09:19:51 centos8.magedu.org automount[1613]: key "selinuxfs" not found in map source(s). Mar 24 09:19:51 centos8.magedu.org automount[1613]: key "bpf" not found in map source(s). Mar 24 09:51:43 centos8.magedu.org automount[1613]: key "selinuxfs" not found in map source(s). Mar 24 09:51:43 centos8.magedu.org automount[1613]: key "bpf" not found in map source(s). [root@centos8[ misc]#^C 开始安装这个功能并启用这个功能: [root@centos8[ misc]#yum -y install autofs “安装 autofs功能,并勾选yes” Repository AppStream is listed more than once in the configuration Repository extras is listed more than once in the configuration Repository PowerTools is listed more than once in the configuration Repository centosplus is listed more than once in the configuration CentOS-8 - AppStream 1.2 kB/s | 4.3 kB 00:03 CentOS-8 - Base - mirrors.aliyun.com 40 kB/s | 3.8 kB 00:00 CentOS-8 - Extras - mirrors.aliyun.com 1.5 kB/s | 1.5 kB 00:01 Package autofs-1:5.1.4-35.el8.x86_64 is already installed. Dependencies resolved. Nothing to do. Complete! [root@centos8[ misc]#^C [root@centos8[ misc]#systemctl start autofs “运行这个功能就可以了,后面再运行systemctl status autofs就可以使用misc目录关联到光盘上” 使用“systemctl enable --now autofs”可以代替systemctl start autofs ,systemctl enable autofs这两条命令
-
/dev:设备文件及特殊文件存储位置
b: block device,随机访问
/dev/sda “”
c: character device,线性访问
/dev/zero “”
-
/opt:第三方应用程序的安装位置
-
/srv:系统上运行的服务用到的数据
-
/tmp:临时文件存储位置
-
/usr: universal shared, read-only data
bin: 保证系统拥有完整功能而提供的应用程序
sbin:
lib:32位使用
lib64:只存在64位系统
include: C程序的头文件(header files)
share:结构化独立的数据,例如doc, man等
local:第三方应用程序的安装位置
bin, sbin, lib, lib64, etc, share
- /var: variable data files
cache: 应用程序缓存数据目录
lib: 应用程序状态信息数据
local:专用于为/usr/local下的应用程序存储可变数据
lock: 锁文件
log: 日志目录及文件
opt: 专用于为/opt下的应用程序存储可变数据
run: 运行中的进程相关数据,通常用于存储进程pid文件
spool: 应用程序数据池
tmp: 保存系统两次重启之间产生的临时数据
-
/proc: 用于输出内核与进程信息相关的虚拟文件系统
-
/sys:用于输出当前系统上硬件设备相关信息虚拟文件系统
-
/selinux: security enhanced Linux,selinux相关的安全策略等信息的存储位置
2.2CentOS 7 以后版本目录结构变化
- /bin 和 /usr/bin
- /sbin 和 /usr/sbin
- /lib 和/usr/lib
- /lib64 和 /usr/lib64
- 范例:
ls /bin /sbin /lib /lib64 -ld “查看bin目录中sbin目录中lib目录中lib目录中的参数仅列出目录详细格式列表
-l 参数 以详细格式列表
-d 参数 仅列目录
-ld 是-l ;-d的简写
命令在centos8 ,centos6,ubantu 中不一样
[root@centos8 ~]#ls /bin /sbin /lib /lib64 -ld “centos8中的bin目录”
lrwxrwxrwx. 1 root root 7 May 11 2019 /bin -> usr/bin
lrwxrwxrwx. 1 root root 7 May 11 2019 /lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 /lib64 -> usr/lib64
lrwxrwxrwx. 1 root root 8 May 11 2019 /sbin -> usr/sbin
[root@centos6 ~]#ls /bin /sbin /lib /lib64 -ld “centos6中的bin目录”
dr-xr-xr-x. 2 root root 4096 Mar 20 09:14 /bin
dr-xr-xr-x. 11 root root 4096 Dec 12 2018 /lib
dr-xr-xr-x. 9 root root 12288 Mar 20 09:13 /lib64
dr-xr-xr-x. 2 root root 12288 Mar 20 09:14 /sbin
[root@ubuntu1804 ~]#ll /bin /usr/bin -d “centos6中的bin目录”
drwxr-xr-x 2 root root 4096 Jan 12 18:41 /bin/
drwxr-xr-x 2 root root 24576 Jan 12 18:42 /usr/bin/
2.3应用程序的组成部分
-
二进制程序:/bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin
-
库文件:/lib, /lib64, /usr/lib, /usr/lib64, /usr/local/lib, /usr/local/lib64
-
配置文件:/etc, /etc/DIRECTORY, /usr/local/etc
-
帮助文件:/usr/share/man, /usr/share/doc, /usr/local/share/man, /usr/local/share/doc
3.Linux下的7种文件类型
-:普通文件
d :目录文件directory
b :块设备block
c :字符设备character
l :符号链接文件link
p :管道文件pipe (网络通讯有关)
s :套接字文件socket (应用程序之间互相网络通讯有关)
范例:
如何查看文件类型 :看开头字母,文件不同颜色也不同。
[root@centos7 ~]#ls -l /run/ “查看 run目录中的子目录的详细格式列表”
total 52
drwxr-xr-x 2 root root 100 Dec 9 20:41 abrt “d开头的文件类型”
-rw------- 1 root root 11 Dec 9 20:41 alsactl.pid
-rw-r--r-- 1 root root 5 Dec 9 20:41 atd.pid
-rw-r--r-- 1 root root 4 Dec 9 20:41 auditd.pid
prw------- 1 root root 0 Dec 9 20:41 autofs.fifo-misc “p开头的文件类型”
prw------- 1 root root 0 Dec 9 20:41 autofs.fifo-net
drwxr-xr-x 2 avahi avahi 80 Dec 9 20:41 avahi-daemon
drwxr-xr-x 2 root root 40 Dec 9 20:41 certmonger
drwxr-x--- 2 chrony chrony 80 Dec 9 20:41 chrony
drwxr-xr-x 2 root root 40 Dec 9 20:41 console
-rw-r--r-- 1 root root 5 Dec 9 20:41 crond.pid
---------- 1 root root 0 Dec 9 20:41 cron.reboot
drwx------ 2 root root 40 Dec 9 20:41 cryptsetup
drwxr-xr-x 3 root lp 80 Dec 9 20:41 cups
drwxr-xr-x 2 root root 60 Dec 9 20:41 dbus
-rw-r--r-- 1 root root 4 Dec 9 20:41 dhclient-eth0.pid
prw------- 1 root root 0 Dec 9 20:40 dmeventd-client
prw------- 1 root root 0 Dec 9 20:40 dmeventd-server
drwxr-xr-x 2 root root 40 Dec 9 20:41 faillock
-rw------- 1 root root 4 Dec 9 20:41 gssproxy.pid
srw-rw-rw- 1 root root 0 Dec 9 20:41 gssproxy.sock
drwxr-xr-x 4 root root 100 Dec 9 20:40 initramfs
-rw-r--r-- 1 root root 4 Dec 9 20:41 ksmtune.pid
drwxr-xr-x 8 root root 220 Dec 9 20:41 lock
drwxr-xr-x 3 root root 60 Dec 9 20:40 log
drwxrwxr-x 3 root libstoragemgmt 60 Dec 9 20:41 lsm
drwx------ 2 root root 80 Dec 9 20:40 lvm
-rw-r--r-- 1 root root 4 Dec 9 20:40 lvmetad.pid
drwx--x--- 2 root root 40 Dec 9 20:41 mdadm
drwxr-xr-x 2 root root 40 Dec 9 20:40 mount
drwxrwxr-x 2 root root 40 Dec 9 20:41 netreport
drwxr-xr-x 3 root root 120 Dec 9 20:41 NetworkManager
drwxr-xr-x 2 root root 40 Dec 9 20:41 plymouth
drwxr-xr-x 2 root root 40 Dec 9 20:41 ppp
drwxr-xr-x 2 radvd radvd 40 Dec 9 20:41 radvd
drwx------ 2 rpc rpc 40 Dec 9 20:41 rpcbind
-r--r--r-- 1 root root 0 Dec 9 20:41 rpcbind.lock
srw-rw-rw- 1 root root 0 Dec 9 20:41 rpcbind.sock
-rw-r--r-- 1 rpcuser rpcuser 5 Dec 9 20:41 rpc.statd.pid
3.相关概念
3.1显示当前工作目录
-
每个shell和系统进程都有一个当前的工作目录 CWD:current work directory
-
pwd命令: printing working directory,
-
显示当前shell CWD的绝对路径
-P 显示真实物理路径
-L 显示链接路径(默认)
2.2 绝对和相对路径
-
绝对路径
以正斜杠/ 即根目录开始
完整的文件的位置路径
可用于任何想指定一个文件名的时候 -
相对路径名
不以斜线开始
一般情况下,是指相对于当前工作目录的路径,特殊场景下,是相对于某目录的位置
可以作为一个简短的形式指定一个文件名
基名:basename,只取文件名而不要路径
目录名:dirname,只取路径,不要文件名
范例:
[root@centos8 bin]#basename /etc/sysconfig/network
network
[root@centos8 bin]#dirname /etc/sysconfig/network
/etc/sysconfig
[root@centos8 ~]#dirname /etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts
[root@centos8 ~]#basename /etc/sysconfig/network-scripts/ifcfg-eth0
ifcfg-eth0
4.相关命令
cd
-
用于切换当前工作目录
-
可以使用绝对路径或者相对路径、;
[root@centos7[ data]# [root@centos7[ ~]#cd /etc/sysconfig “cd 后面跟绝对路径/etc/sysconfig,切换到ETC目录下的sysconfig目录” [root@centos7[ sysconfig]#pwd “显示当前路径” /etc/sysconfig [root@centos7[ sysconfig]#cd ../../data “cd 后面跟相对路径../../data,切换到返回上一级目录再返回到上一级目录再到data目录里面” [root@centos7[ data]#pwd “显示当前路径” /data [root@centos7[ data]#
-
切换至父目录: cd ..
[root@centos7[ /]# [root@centos7[ /]#cd /etc/sysconfig “切换到etc目录中的sysconfig目录” [root@centos7[ sysconfig]#pwd “显示当前路径” /etc/sysconfig [root@centos7[ sysconfig]#cd .. "切换到上级目录也就是父目录" [root@centos7[ etc]#pwd “显示当前路径” /etc [root@centos7[ etc]#
-
切换至当前用户主目录: cd, cd~ 切换到当前用户的家目录,使用 # cd~username 切换到其他的用户家的目录
[root@centos7[ etc]# [root@centos7[ etc]#cd "从etc目录中返回到当前用户目录中" [root@centos7[ ~]# [root@centos7[ bin]#cd ~yang “切换到yang用户的家的目录” [root@centos7[ yang]#pwd /home/yang [root@centos7[ yang]#
-
切换至上次所在的工作目录: cd - (该命令只能返回一次目录)
[root@centos7[ ~]#cd /etc “切换到etc目录下” [root@centos7[ etc]#cd - “返回以前的工作目录” /root [root@centos7[ ~]#cd - “返回以前的工作目录” /etc [root@centos7[ etc]#
-
选项:-P 切换至物理路径,而非软链接目录(软连接:一个符号链接指向另一个文件。
♦软链接本质上不是同一个文件,链接文件依赖原文件
) -
[17:55:22 root@centos7[ etc]#cd /bin “切换到bin目录中” [17:55:26 root@centos7[ bin]#pwd “显示当前路径” /bin [17:55:27 root@centos7[ bin]#cd -P /bin “切换至物理地址bin目录下” [17:55:41 root@centos7[ bin]#pwd “显示当前路径” /usr/bin [17:55:43 root@centos7[ bin]#
-
cd / 切换到根目录
[root@centos7[ ~]#cd / [root@centos7[ /]#pwd / [root@centos7[ /]#
-
相关的环境变量:
PWD:当前目录路径
OLDPWD:上一次目录路径[root@centos7[ sysconfig]#cd /etc/sysconfig “切换到/etc/sysconfig路径” [18:13:39 root@centos7[ sysconfig]#cd “返回家目录” [18:13:40 root@centos7[ ~]#echo $OLDPWD “打印上一个路径的变量值” /etc/sysconfig [18:13:44 root@centos7[ ~]#cd - “返回上一次的路径” /etc/sysconfig [18:14:01 root@centos7[ sysconfig]#pwd “显示当前路径” /etc/sysconfig [18:14:04 root@centos7[ sysconfig]#
pwd
- printing working directory
- -P 显示真实物理路径
软链接的时候有用到
。如果当前的工作路径是链接的话,显示链接的原始路径,也就是实际路径,如ls -l /proc/self 显示的一样。 - -L 显示链接路径(默认)
[root@centos7[ /]#cd /etc/rc.d/init.d
[root@centos7[ init.d]#pwd
/etc/rc.d/init.d
[root@centos7[ init.d]#pwd -P
/etc/rc.d/init.d
[root@centos7[ init.d]#pwd -L
/etc/rc.d/init.d
[root@centos7[ init.d]#
ls
- 列出当前的文件项,然后是每一个目录中的“可显示”文件(可理解为ls命令将本目录向下展开两级)
2.4 相关命令
2.7 文件通配符模式 wildcard pattern
文件通配符可以用来匹配符合条件的多个文件,方便批量管理文件
通配符采有特定的符号,表示特定的含义,此特符号称为元 meta 字符
常见的通配符如下:
- 匹配零个或多个字符,但不匹配 "." 开头的文件,即隐藏文件
? 匹配任何单个字符
~ 当前用户家目录
~mage 用户mage家目录
~+和. 当前工作目录
~- 前一个工作目录
[0-9] 匹配数字范围
[a-z] 字母
[A-Z] 字母
[wang] 匹配列表中的任何的一个字符
[^wang] 匹配列表中的所有字符以外的字符
别外还有在Linux系统中预定义的字符类:man 7 glob
[:digit:]:任意数字,相当于0-9
[:lower:]:任意小写字母,表示 a-z
[:upper:]: 任意大写字母,表示 A-Z
[:alnum:]:任意数字或字母
[:blank:]:水平空白字符
[:space:]:水平或垂直空白字符
[:punct:]:标点符号
[:print:]:可打印字符
[:cntrl:]:控制(非打印)字符
[:graph:]:图形字符
[:xdigit:]:十六进制字符
范例:
[root@centos8 wang]#echo $OLDPWD
/data
[root@centos8 wang]#ll -/linux.txt
ls: invalid option -- '/'
Try 'ls --help' for more information.
[root@centos8 wang]#ll ~-/linux.txt
-rw-r--r-- 1 root root 6 Mar 23 09:48 /data/linux.txt
范例:
[root@centos8 data]#touch file{a..z}.txt file{A..Z}.txt file{0.9}.txt
[root@centos8 data]#ll file{0..9}.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file0.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file1.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file2.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file3.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file4.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file5.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file6.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file7.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file8.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file9.txt
[root@centos8 data]#ll file[0-9].txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file0.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file1.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file2.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file3.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file4.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file5.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file6.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file7.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file8.txt
-rw-r--r-- 1 root root 0 Mar 23 10:46 file9.txt
[root@centos8 data]#ls file[a-c].txt
filea.txt fileA.txt fileb.txt fileB.txt filec.txt
[root@centos8 data]#ls file[C-E].txt
fileC.txt filed.txt fileD.txt filee.txt fileE.txt
[root@centos8 data]#ls file[wang].txt
filea.txt fileg.txt filen.txt filew.txt
[root@centos8 data]#ls file[^wang].txt
范例:
[root@centos8 data]#ls file[:lower:].txt
filee.txt filel.txt fileo.txt filer.txt filew.txt
[root@centos8 data]#ls file[[:lower:]].txt
filea.txt filed.txt fileg.txt filej.txt filem.txt filep.txt files.txt
filev.txt filey.txt
fileb.txt filee.txt fileh.txt filek.txt filen.txt fileq.txt filet.txt
filew.txt filez.txt
filec.txt filef.txt filei.txt filel.txt fileo.txt filer.txt fileu.txt
filex.txt
范例:比较有无的功能区别
[root@centos8 data]#ls -a *
[root@centos8 data]#ls -a
范例:
[root@centos8 ~]#ls -d /etc//
[root@centos8 ~]#l.
范例:
[root@centos8 data]#touch file.log
[root@centos8 data]#touch file1.log
[root@centos8 data]#ls file.log
file1.log 'file.log'
[root@centos8 data]#ls 'file.log'
'file.log'
[root@centos8 data]#ls 'file.log'
范例:
1、显示/etc目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数字的文件或目录列表
2、显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
3、显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表
4、显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符的文件或目录列表
5、显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录列表
6、只显示/root下的隐藏文件和目录列表
7、只显示/etc下的非隐藏目录列表
2.8 创建空文件和刷新时间
touch命令可以用来创建空文件或刷新文件的时间
格式:
touch [OPTION]... FILE...
选项说明:
-a 仅改变 atime和ctime
-m 仅改变 mtime和ctime
-t [[CC]YY]MMDDhhmm[.ss] 指定atime和mtime的时间戳
-c 如果文件不存在,则不予创建
范例:
[root@centos8 data]#touch f1.txt
[root@centos8 data]#ll
total 0
-rw-r--r-- 1 root root 0 Mar 23 11:30 f1.txt
[root@centos8 data]#ll /etc/issue
-rw-r--r--. 1 root root 23 Jan 2 23:21 /etc/issue
[root@centos8 data]#touch /etc/issue
[root@centos8 data]#ll /etc/issue
-rw-r--r--. 1 root root 23 Mar 23 11:31 /etc/issue
[root@centos8 data]#stat /etc/issue
File: /etc/issue
Size: 23 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 134345998 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-03-23 11:31:20.030291332 +0800
Modify: 2020-03-23 11:31:20.030291332 +0800
Change: 2020-03-23 11:31:20.030291332 +0800
Birth: -
[root@centos8 data]#date
Fri Dec 13 16:11:55 CST 2019
[root@centos8 data]#touch date -d "-1 day" +%F_%T
.log
[root@centos8 data]#ls
2019-12-12_16:11:48.log
[root@centos8 data]#touch $(date -d "1 year" +%F_%T).log
[root@centos8 data]#ls
2019-12-12_16:11:48.log 2020-12-13_16:13:11.log
2.9 复制文件和目录
利用 cp(copy)命令可以实现文件或目录的复制
格式:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
常用选项
-i 如果目标已存在,覆盖前提示是否覆盖
-n 不覆盖,注意两者顺序
-r, -R 递归复制目录及内部的所有内容
-a 归档,相当于-dR --preserv=all,常用于备份功能
-d --no-dereference --preserv=links 不复制原文件,只复制链接名
--preserv[=ATTR_LIST]
mode: 权限
ownership: 属主属组
timestamp:
links
xattr
context
all
-p 等同--preserv=mode,ownership,timestamp
-v --verbose
-f --force
-u --update 只复制源比目标更新文件或目标不存在的文件
-b 目标存在,覆盖前先备份,默认形式为 filename~ ,只保留最近的一个备份
--backup=numbered 目标存在,覆盖前先备份加数字后缀,形式为filename.# ,可以保留多
个版本
源
\目
标
不存在存在且为文件存在且为目录
一个
文件
新建DEST,并将SRC中
内容填充至DEST中
将SRC中的内容覆盖至
DEST中 注意数据丢失风
险! 建议用 –i 选项
在DEST下新建与原文件同
名的文件,并将SRC中内容
填充至新文件中
多个
文件
提示错误提示错误
在DEST下新建与原文件同
名的文件,并将原文件内容
复制进新文件中
目录
须使
用-r
选项
创建指定DEST同名目
录,复制SRC目录中所
有文件至DEST下
提示错误
在DEST下新建与原目录同
名的目录,并将SRC中内容
复制至新目录中
范例:
[16:21:08 root@centos8 ~]#cp ~wang/issue /data/issue_wang.bak
-rw-r--r--. 1 root root 86 Dec 13 16:21 /data/issue_wang.bak
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 /home/wang/issue
[16:21:44 root@centos8 ~]#cp -p ~wang/issue /data/issue_wang2.bak
[16:22:07 root@centos8 ~]#ll ~wang/issue /data/issue_wang2.bak
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 /data/issue_wang2.bak
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 /home/wang/issue
[16:23:19 root@centos8 ~]#cp /etc/sysconfig/ /data/
cp: -r not specified; omitting directory '/etc/sysconfig/'
[16:23:40 root@centos8 ~]#cp -r /etc/sysconfig/ /data/
[16:23:45 root@centos8 ~]#ll /data
total 12
-rw-r--r--. 1 root root 0 Dec 13 16:11 2019-12-12_16:11:48.log
-rw-r--r--. 1 root root 0 Dec 13 16:13 2020-12-13_16:13:11.log
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 issue_wang2.bak
-rw-r--r--. 1 root root 86 Dec 13 16:21 issue_wang.bak
drwxr-xr-x. 7 root root 4096 Dec 13 16:23 sysconfig
[16:24:03 root@centos8 ~]#cp -r /etc/sysconfig/ /data/sysconfig_bak
[16:24:34 root@centos8 ~]#ll /data
total 16
-rw-r--r--. 1 root root 0 Dec 13 16:11 2019-12-12_16:11:48.log
-rw-r--r--. 1 root root 0 Dec 13 16:13 2020-12-13_16:13:11.log
-rw-r--r--. 1 wang wang 86 Dec 13 16:18 issue_wang2.bak
-rw-r--r--. 1 root root 86 Dec 13 16:21 issue_wang.bak
drwxr-xr-x. 7 root root 4096 Dec 13 16:23 sysconfig
drwxr-xr-x. 7 root root 4096 Dec 13 16:24 sysconfig_bak
[16:32:43 root@centos8 ~]#cp -b /etc/motd /data/issue
cp: overwrite '/data/issue'? y
[16:33:09 root@centos8 ~]#ll /data
total 8
-rw-r--r--. 1 root root 1 Dec 13 16:33 issue
-rw-r--r--. 1 root root 2610 Dec 13 16:32 issue~
[16:33:11 root@centos8 ~]#cp -b /etc/fstab /data/issue
cp: overwrite '/data/issue'? y
[16:33:55 root@centos8 ~]#ll /data
total 8
-rw-r--r--. 1 root root 709 Dec 13 16:33 issue
-rw-r--r--. 1 root root 1 Dec 13 16:33 issue~
[16:33:56 root@centos8 ~]#cp --backup=numbered /etc/fstab /data/issue
cp: overwrite '/data/issue'? y
[16:34:32 root@centos8 ~]#cp --backup=numbered /etc/shadow /data/issue
cp: overwrite '/data/issue'? y
[16:34:41 root@centos8 ~]#cp --backup=numbered /etc/os-release /data/issue
cp: overwrite '/data/issue'? y
[16:34:48 root@centos8 ~]#ll /data
total 20
-rw-r--r--. 1 root root 420 Dec 13 16:34 issue
-rw-r--r--. 1 root root 1 Dec 13 16:33 issue~
-rw-r--r--. 1 root root 709 Dec 13 16:33 issue.1
-rw-r--r--. 1 root root 709 Dec 13 16:34 issue.2
----------. 1 root root 1349 Dec 13 16:34 issue.3
范例:
[root@centos8 ~]#cp /dev/zero /data/zero.bak
练习
1、每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为 backupYYYYmm-
dd,备份过程可见
[root@centos8 ~]#cp -av /etc/ /data/backupdate +%F
2、创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限
[root@centos8 ~]#cp -a /root /data/rootdir
2.10 移动和重命名文件
mv 命令可以实现文件或目录的移动和改名
同一分区移动数据,速度很快:数据位置没有变化
不同分区移动数据,速度相对慢:数据位置发生了变化
格式:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...
常用选项:
-i 交互式
-f 强制
-b 目标存在,覆盖前先备份
利用rename 可以批量修改文件名
格式:
rename [options]
范例:
为所有的conf文件加上.bak后缀:
rename 'conf' 'conf.bak' f*
去掉所有的bak后缀:
rename '.bak' '' .bak
2.11 删除文件
使用 rm 命令可以删除文件
注意:此命令非常危险,慎重使用,建议使用mv 代替 rm
格式:
rm [OPTION]... FILE...
常用选项:
-i 交互式
-f 强制删除
-r 递归
--no-preserve-root 删除/
范例:
[root@centos8 ~]#rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe
rm -rf /
范例:删除特殊文件
将名为 “/data/-f” 的文件删除
[root@centos8 data]#rm -f -f #此方式错误
[root@centos8 data]#rm -- -f
[root@centos8 data]#rm ./-f
[root@centos8 data]#rm /data/-f
[root@centos8 data]#touch '~'
[root@centos8 data]#ls
'~'
[root@centos8 data]#rm -f ~
rm: cannot remove '/root': Is a directory
[root@centos8 data]#rm -- ~
rm: cannot remove '/root': Is a directory
[root@centos8 data]#rm -f ./~
rm虽然删除了文件,但是被删除的文件仍然可能被恢复,在安全要求较高的场景下,可以使用shred安
全删除文件
shred格式
shred [OPTION]... FILE...
常见选项:
-z 最后一次覆盖添加0,以隐藏覆盖操作
-v 能够显示操作进度
-u 覆盖后截断并删除文件
-n # 指定覆盖文件内容的次数(默认值是3次)
范例:
[root@centos8 ~]#shred -zvun 5 passwords.txt
shred: passwords.txt: pass 1/6 (random)...
shred: passwords.txt: pass 2/6 (000000)...
shred: passwords.txt: pass 3/6 (random)...
shred: passwords.txt: pass 4/6 (ffffff)...
shred: passwords.txt: pass 5/6 (random)...
shred: passwords.txt: pass 6/6 (000000)...
shred: passwords.txt: removing
shred: passwords.txt: renamed to 0000000000000
shred: 0000000000000: renamed to 000000000000
shred: 000000000000: renamed to 00000000000
shred: 00000000000: renamed to 0000000000
shred: 0000000000: renamed to 000000000
shred: 000000000: renamed to 00000000
shred: 00000000: renamed to 0000000
shred: 0000000: renamed to 000000
shred: 000000: renamed to 00000
shred: 00000: renamed to 0000
shred: 0000: renamed to 000
shred: 000: renamed to 00
shred: 00: renamed to 0
shred: passwords.txt: removed
[root@centos8 ~]#ls passwords.txt
ls: cannot access 'passwords.txt': No such file or directory
2.12 目录操作
2.12.1 显示目录树 tree
常见选项:
-d: 只显示目录
-L level:指定显示的层级数目
-P pattern: 只显示由指定wild-card pattern匹配到的路径
2.12.2 创建目录 mkdir
常见选项:
-p: 存在于不报错,且可自动创建所需的各目录
-v: 显示详细信息
-m MODE: 创建目录时直接指定权限
2.11.3 删除空目录rmdir
常见选项:
-p 递归删除父空目录
-v 显示详细信息
注意:rmdir只能删除空目录,如果想删除非空目录,可以使用rm -r 命令,递归删除目录树
范例:
alias rm='DIR=/data/backupdate +%F%T
;mkdir $DIR;mv -t $DIR'
练习
(1) 如何创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a,
/testdir/dir1/y/b
(2) 如何创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b
(3) 如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7
3 文件元数据和节点表结构
3.1 inode表结构
每个文件的属性信息,比如:文件的大小,时间,类型等,称为文件的元数据(meta data)。这此元数
据是存放在node(index node)表中。node 表中有很多条记录组成,第一条记录对应的存放了一个文
件的元数据信息
第一个node表记录对应的保存了以下信息:
inode number 节点号
文件类型
权限
UID
GID
链接数(指向这个文件名路径名称个数)
该文件的大小和不同的时间戳
指向磁盘上文件的数据块指针
有关文件的其他数据
目录
目录是个特殊文件,目录文件的内容保存了此目录中文件的列表及inode number对应关系
文件引用一个是 inode号
人是通过文件名来引用一个文件
一个目录是目录下的文件名和文件inode号之间的映射
inode表和目录
cp和inode
cp 命令:
分配一个空闲的inode号,在inode表中生成新条目
在目录中创建一个目录项,将名称与inode编号关联
拷贝数据生成新的文件
rm和inode
rm 命令:
链接数递减,从而释放的inode号可以被重用
把数据块放在空闲列表中
删除目录项
数据实际上不会马上被删除,但当另一个文件使用数据块时将被覆盖
mv和inode
如果mv命令的目标和源在相同的文件系统,作为mv 命令
用新的文件名创建对应新的目录项
删除旧目录条目对应的旧的文件名
不影响inode表(除时间戳)或磁盘上的数据位置:没有数据被移动!
如果目标和源在一个不同的文件系统, mv相当于cp和rm
范例:
[root@centos8 ~]#df -i /boot
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 65536 310 65226 1% /boot
[root@centos8 ~]#mkdir /boot/test
[root@centos8 ~]#touch /boot/test/file{1..65226}
[root@centos8 ~]#touch /boot/test/new.txt
touch: cannot touch 'new.txt': No space left on device
[root@centos8 ~]#df /boot
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 999320 133800 796708 15% /boot
[root@centos8 ~]#df -i /boot
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 65536 65536 0 100% /boot
范例:删除大文件
[root@centos8 ~]#cat /dev/null > /var/log/huge.log
3.2 硬(hard)链接
硬链接本质上就给一个文件起一个新的名称,实质是同一个文件
硬链接特性
创建硬链接会在对应的目录中增加额外的记录项以引用文件
对应于同一文件系统上一个物理文件
每个目录引用相同的inode号
创建时链接数递增
删除文件时:rm命令递减计数的链接,文件要存在,至少有一个链接数,当链接数为零时,该文
件被删除
不能跨越驱动器或分区
不支持对目录创建硬链接
格式:
ln filename [linkname ]
3.3 符号 symbolic (或软 soft)链接
一个符号链接指向另一个文件,就像 windows 中快捷方式,软链接文件和原文件本质上不是同一个文件
软链接特点
一个符号链接的内容是它引用文件的名称
可以对目录创建软链接
可以跨分区的文件实现
指向的是另一个文件的路径;其大小为指向的路径字符串的长度;不增加或减少目标文件inode的
引用计数
软链接如果使用相对路径,是相对于原文件的路径,而非相对于当前目录
格式:
ln -s filename [linkname]
范例:
绝对路径
ln -s /data/dir /data/dirlink
相对路径
cd /data
ln -s ../data/dir /root/dirlink
rm -rf /data/dirlink #删除软链接本身,不会删除源目录内容
rm -rf /data/dirlink/ #删除源目录的文件,不会删除链接文件
查看链接文件指向的原文件
readlink /data/dirlink
3.4 硬链接和软链接区别总结
-
本质:
硬链接:本质是同一个文件
软链接:本质不是同一个文件 -
跨设备
硬链接:不支持
软链接:支持 -
inode
硬链接:相同
软链接:不同 -
链接数
硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少
软链接:创建或删除,链接数不会变化 -
文件夹
硬链接:不支持
软链接:支持 -
相对路径
硬链接:原始文件相对路径是相对于当前工作目录
软链接:原始文件的相对路径是相对于链接文件的相对路径 -
删除源文件
硬链接:只是链接数减一,但链接文件的访问不受影响
软链接:链接文件将无法访问 -
文件类型
硬链接:和源文件相同
软链接:链接文件,和源文件无关[root@centos8 ~]#pwd "" /root [root@centos8 ~]#cd /data [root@centos8 data]#pwd /data [14:38:41 root@centos8 sysconfig]#cd /bin [14:38:52 root@centos8 bin]#pwd /bin [14:38:54 root@centos8 bin]#ll / total 28 lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin dr-xr-xr-x. 6 root root 4096 Dec 11 11:21 boot drwxr-xr-x. 2 root root 142 Dec 13 14:26 data drwxr-xr-x. 19 root root 3280 Dec 13 11:21 dev drwxr-xr-x. 142 root root 8192 Dec 13 14:26 etc drwxr-xr-x. 3 root root 18 Dec 11 11:19 home lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 May 11 2019 media drwxr-xr-x. 3 root root 18 Dec 11 11:15 mnt drwxr-xr-x. 2 root root 6 May 11 2019 opt dr-xr-xr-x. 259 root root 0 Dec 13 10:34 proc dr-xr-x---. 15 root root 4096 Dec 13 14:26 root drwxr-xr-x. 42 root root 1200 Dec 13 11:03 run lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 May 11 2019 srv dr-xr-xr-x. 13 root root 0 Dec 13 10:34 sys drwxrwxrwt. 20 root root 4096 Dec 13 14:14 tmp drwxr-xr-x. 12 root root 144 Dec 11 11:12 usr drwxr-xr-x. 22 root root 4096 Dec 11 11:21 var [14:38:57 root@centos8 bin]#pwd -P /usr/bin