Linux 文件管理(完成优先于完美)

一、描述 Linux 发行版的系统目录名称命名规则以及用途。node

文件和目录被组织成一个单根倒置树结构
文件系统从根目录下开始,用“/”表示
路径分隔的 /
文件名最长 255 个字节
包括路径在内文件名称最长 4095 个字节
除了斜杠和 NUL,全部字符都有效。但使用特殊字符的目录名和文件不推荐使用,有些字符须要用引号来引用mysql

/boot:引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader,grub)都存放于此目录
/bin:全部用户使用的基本命令;不能关联至独立分区,OS 启动即会用到的程序
/sbin:管理类的基本命令;不能关联至独立分区,OS 启动即会用到的程序
/lib:启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules)
/lib64:专用于 x86_64 系统上的辅助共享库文件存放位置
/etc:配置文件目录
/home/USERNAME:普通用户家目录
/root:管理员的家目录
/media:便携式移动设备挂载点
/mnt:临时文件系统挂载点
/dev:设备文件及特殊文件存储位置
b:block device,随机访问
c:character device,线性访问
/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/loacl 下的应用程序存储可变数据
lock:锁文件
log:日志目录及文件
opt:专用于为 /opt 下的应用程序存储可变数据
run:运行中的进程相关数据,一般用于存储进程 pid 文件
spool:应用程序数据池
tmp:保存系统两次重启之间产生的临时数据
/proc:用于输出内核与进程信息相关的虚拟文件系统
/sys:用于输出当前系统上硬件设备相关信息虚拟文件系统
/selinux:security enhanced Linux,selinux 相关的安全策略等信息的存放位置linux

二、描述文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息?git

inode number 节点号、文件类型、权限、UID、GID、连接数(指向这个文件名路径名称个数)、改文件的大小和不一样的时间戳、指向磁盘上文件的数据块指针、有关文件的其余数据sql

[root@centos8 ~]# stat /tmp/test.txt
File: /tmp/test.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 1403409 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ wang) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2020-10-06 18:51:36.000631508 +0800
Modify: 2020-10-06 18:48:36.833631914 +0800
Change: 2020-10-06 18:48:36.833631914 +0800
Birth: -shell

touch [OPTION]... FILE...
-a 仅改变 atime 和 ctime
-m 仅改变 mtime 和 ctime
-t [[CC]YY]MMDDhhmm[.ss] 指定 atime 和 mtime 的时间戳
-c 若是文件不存在,则不予建立vim

三、总结软连接和硬连接区别,并用实例操做说明。windows

硬连接本质是同一个文件
软连接本质是不一样一个文件
硬连接不支持文件夹
软连接:原始文件的相对路径是相对于连接文件的相对路径centos

[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt
drwxr-xr-x. 2 root root 6 Oct 7 19:44 zhl
[root@centos8 data]# ln file.txt zhl/file.txt
[root@centos8 data]# ll file.txt zhl/file.txt
-rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt(连接数变为 2)
-rw-r--r--. 2 root root 0 Oct 7 15:34 zhl/file.txt
[root@centos8 data]# ll -i file.txt zhl/file.txt
132 -rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt
132 -rw-r--r--. 2 root root 0 Oct 7 15:34 zhl/file.txt
[root@centos8 data]# rm file.txt
rm: remove regular empty file 'file.txt'? y
[root@centos8 data]# ll -i file.txt zhl/file.txt
ls: cannot access 'file.txt': No such file or directory
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 zhl/file.txt(文件还能够访问,连接数减1,不依赖于原文件。删文件,是指删名)
硬连接不能跨文件系统(分区),本质是给一个文件起多个名。跨了分区,节点号有可能冲突
不容许给文件夹建立硬连接,避免存在嵌套的问题(文件夹套子文件,子文件又套自身,造成死循环)缓存

[root@centos8 zhl]# ln -s file.txt f1.txt
[root@centos8 zhl]# ll -i
total 0
134 lrwxrwxrwx. 1 root root 8 Oct 8 17:50 f1.txt -> file.txt
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt
[root@centos8 zhl]# ln -s file.txt f2.txt
[root@centos8 zhl]# ll -i
total 0
134 lrwxrwxrwx. 1 root root 8 Oct 8 17:50 f1.txt -> file.txt(大小是连接的信息内容,8 个字符数)
135 lrwxrwxrwx. 1 root root 8 Oct 8 17:52 f2.txt -> file.txt
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt(文件连接数没变,节点号不同,是不一样的文件。删除文件,连接做废)
root@centos8 data]# ln -s f1.txt dir1/f1.txt.link(此处绕,它认为 f1.txt.link 和 f1.txt 在同一个目录里,现实却不是。此时 f1.txt 的相对路径应该是相对于软连接 dir1/f1.txt.link 的相对路径,而不是相对于 data 的。正确的应该是ln -s ../f1.txt dir1/f1.txt.link 。看这个“/data/dir1/f1.txt.link -> /data/f1.txt”,它是从前日后去找到你,因此你不能是相对的)
[root@centos8 data]# ll -i !*
ll -i -s f1.txt dir1/f1.txt.link
33555841 0 lrwxrwxrwx. 1 root root 6 Oct 8 18:07 dir1/f1.txt.link -> f1.txt(此时系统提示 f1.txt 不存在,闪亮。)
132 0 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
[root@centos8 data]# mkdir mysql5.6.10
[root@centos8 data]# ln -s mysql5.6.10/ mysql
[root@centos8 data]# ll -i
total 0
33555840 drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1
132 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
133 lrwxrwxrwx. 1 root root 12 Oct 8 18:22 mysql -> mysql5.6.10/
67161792 drwxr-xr-x. 2 root root 6 Oct 8 18:21 mysql5.6.10
[root@centos8 data]# rm -f mysql
[root@centos8 data]# mkdir mysql5.6.11
[root@centos8 data]# ln -s mysql5.6.11/ mysql
[root@centos8 data]# ll -i
total 0
33555840 drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1
132 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
133 lrwxrwxrwx. 1 root root 12 Oct 8 18:23 mysql -> mysql5.6.11/(软件升级的方式)
67161792 drwxr-xr-x. 2 root root 6 Oct 8 18:21 mysql5.6.10
100664640 drwxr-xr-x. 2 root root 6 Oct 8 18:22 mysql5.6.11
[root@centos8 data]# rm -rf mysql/(多 / 会删文件,没有 / 只是删软连接。多 / 表明进入了,场景变了)(基础不牢,地动山摇。弄清楚区别)

[root@centos8 data]# ll
total 0
drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1(天生就是 2,. 用的与其相同的节点编号。能不能变成 3 呢?能够,建个子目录,子目录的..就是它了,因此能够是 3。以此类推)

四、Linux 上的文件管理类命令都有哪些,其经常使用的使用方法及其相关示例演示。

pwd

cd
-P:切换至物理路径
echo $PWD
echo $OLDPWD

ls
-a
-l
-R:目录递归
-ld:目录、符号连接
-1
-S:大到小
-t:mtime
-u:配合 -t,atime
-U
-X
/etc/DIR_COLORS
LS_COLORS
[root@centos8 ~]# ls /boot
System.map-4.18.0-147.el8.x86_64 initramfs-4.18.0-147.el8.x86_64.img
config-4.18.0-147.el8.x86_64 loader
efi lost+found
grub2 vmlinuz-0-rescue-7650fa24cc7e4d12acb58cba4449720f
initramfs-0-rescue-7650fa24cc7e4d12acb58cba4449720f.img vmlinuz-4.18.0-147.el8.x86_64
[root@centos8 ~]# ls -R /boot
[root@centos8 ~]# ls -ld /boot
dr-xr-xr-x. 6 root root 4096 Oct 1 19:46 /boot
mtime(modify) ctime(change) atime(access)
[root@centos8 ~]# ll /tmp/test.txt
-rw-r--r--. 1 root root 5 Oct 3 16:01 /tmp/test.txt
[root@centos8 ~]# ll --time=ctime /tmp/test.txt
-rw-r--r--. 1 root root 5 Oct 3 16:01 /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 root root 5 Oct 3 16:02 /tmp/test.txt
[root@centos8 ~]# chown wang /tmp/test.txt
[root@centos8 ~]# ll /tmp/test.txt
-rw-r--r--. 1 wang root 5 Oct 3 16:01 /tmp/test.txt
[root@centos8 ~]# ll --time=ctime /tmp/test.txt
-rw-r--r--. 1 wang root 5 Oct 6 18:47 /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 wang root 5 Oct 3 16:02 /tmp/test.txt
[root@centos8 ~]# > /tmp/test.txt
[root@centos8 ~]# ll /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 6 18:48 /tmp/test.txt
[root@centos8 ~]# ll --time=ctime /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 6 18:48 /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 3 16:02 /tmp/test.txt
[root@centos8 ~]# cat /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 6 18:51 /tmp/test.txt

stat
[root@centos8 ~]# stat /tmp/test.txt
File: /tmp/test.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 1403409 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ wang) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2020-10-06 18:51:36.000631508 +0800
Modify: 2020-10-06 18:48:36.833631914 +0800
Change: 2020-10-06 18:48:36.833631914 +0800
Birth: -

file
magic number
[root@centos8 ~]# file /etc
/etc: directory
[root@centos8 ~]# file /boot/vmlinuz-4.18.0-147.el8.x86_64
/boot/vmlinuz-4.18.0-147.el8.x86_64: Linux kernel x86 boot executable bzImage, version 4.18.0-147.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) #1 SMP Wed Dec 4 21:51:45 UTC 2019, RO-rootFS, swap_dev 0x7, Normal VGA
[root@centos8 ~]# which file
/usr/bin/file
[root@centos8 ~]# file /usr/bin/file
/usr/bin/file: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=1aaef7e4e7e253a9191d2e7e43d5e7dd8c8ab1e8, stripped
[root@centos8 ~]# file /dev/zero
/dev/zero: character special (1/5)
[root@centos8 ~]# file /dev/sda
/dev/sda: block special (8/0)
[root@centos8 ~]# file /run/autofs.fifo-net
/run/autofs.fifo-net: fifo (named pipe)
[root@centos8 ~]# hexdump -C /tmp/test.txt(区分 Linux、Windows 文件)
Linux 回车换行(继承 Unix,存储节约空间,没了回车字符)
Windows 写的文件,在 Linux 中有回车也有换行
[root@centos8 ~]# dnf -y install dos2unix
Last metadata expiration check: 0:01:09 ago on Tue 06 Oct 2020 10:47:47 PM CST.
Package dos2unix-7.4.0-3.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@centos8 ~]# dos2unix win.txt
[root@centos8 ~]# file win.txt
[root@centos8 ~]# hexdump -C win.txt
[root@centos8 ~]# unix2dos linux.txt
早期 Windows 用的 ANSI 字符集,不是 UTF-8,传到 Linux 中查看不了,编码机制不同,Linux 用的 UTF-8。
解决办法,在 Windows 上把文件另存为 Unicode,而后传入 Linux 可查看,大小比 ANSI 的大。
[root@centos8 ~]# iconv -l
[root@centos8 ~]# file windows.txt
[root@centos8 ~]# iconv -f gb2312 windows.txt -o windows1.txt
[root@centos8 ~]# iconv -f utf8 -t gb2312 windows1.txt -o windows11.txt(转换编码机制)

touch
-a
-m
-t
-c
[root@centos8 ~]# ll /etc/issue
-rw-r--r--. 1 root root 23 Jan 2 2020 /etc/issue
[root@centos8 ~]# touch /etc/issue
[root@centos8 ~]# stat /etc/issue
File: /etc/issue
Size: 23 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 134341451 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:etc_t:s0
Access: 2020-10-07 09:23:55.889008789 +0800
Modify: 2020-10-07 09:23:55.889008789 +0800
Change: 2020-10-07 09:23:55.889008789 +0800
Birth: -

cp
-i
-n
-r,-R
-d
cp -p(保留权限、全部者、时间戳)
cp -r
拷贝的快捷方式,目标是文件
cp -d不复制原文件,只复制连接名
cp -a(经常使用于备份,保留全部内容,包含r)
cp -av(v 显示进程)
cp -f(普通用户在本身家里,能够删除 root 的文件,此处跟权限有关。先删后拷。if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used))
cp -u(防止覆盖新文件)
cp -b
cp -a(普通用户拷贝别人的文件时,属性是本身,没有保留别人,这是合乎逻辑的)
[root@centos8 data]# cp /etc/issue ./issue.bak
[root@centos8 data]# ll !*
ll /etc/issue ./issue.bak
-rw-r--r--. 1 root root 23 Oct 7 09:23 /etc/issue
-rw-r--r--. 1 root root 23 Oct 7 10:07 ./issue.bak
[root@centos8 data]# cat /etc/issue
\S
Kernel \r on an \m

[root@centos8 data]# cat issue.bak
\S
Kernel \r on an \m

[root@centos8 data]# alias cp(root cp 是别名,别的用户不是)
alias cp='cp -i'
[root@centos8 data]# cp --backup=numbered /etc/passwd f1.txt
[root@centos8 data]# ll
total 4
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt
[root@centos8 data]# cp --backup=numbered /etc/passwd f1.txt
cp: overwrite 'f1.txt'? y
[root@centos8 data]# ll
total 8
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt.~1~
[root@centos8 data]# cp --backup=numbered /etc/passwd f1.txt(先用过 --backup 后,-b 就用这种模式了)
cp: overwrite 'f1.txt'? y
[root@centos8 data]# ll
total 12
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt.~1~
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt.~2~
[root@centos8 data]# cp -r /etc/ /data/etcbackup
[root@centos8 data]# ll
total 12
drwxr-xr-x. 136 root root 8192 Oct 7 10:48 etcbackup
[root@centos8 data]# cp -r /etc/ /data/etcbackup
[root@centos8 data]# ll
total 12
drwxr-xr-x. 137 root root 8192 Oct 7 10:48 etcbackup
[root@centos8 data]# ls -d ./etcbackup/etc
./etcbackup/etc
[root@centos8 data]# cp -r /etc/ /data/etcbackup
cp: overwrite '/data/etcbackup/etc/xdg/menus/applications.menu'? y
[root@centos8 data]# ll /dev/zero
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 /dev/zero
[root@centos8 data]# cp /dev/zero zero.bak
^C
[root@centos8 data]# ll !
ll /dev/zero zero.bak
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 /dev/zero
-rw-r--r--. 1 root root 415825920 Oct 7 11:05 zero.bak
[root@centos8 data]# ll -h
total 397M
-rw-r--r--. 1 root root 397M Oct 7 11:05 zero.bak
[root@centos8 data]# hexdump -C zero.bak
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|

18c90000
[root@centos8 data]# rm -f zero.bak
[root@centos8 data]# cp -a /dev/zero zero.bak
[root@centos8 data]# ll
total 0
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 zero.bak
[root@centos8 data]# cp -p /dev/zero zero.bak2
^C
[root@centos8 data]# ll
total 527800
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 zero.bak
-rw-------. 1 root root 540467200 Oct 7 11:07 zero.bak2

mv
-i
-f
-b
[root@centos8 ~]# alias mv(root 下也是别名)
alias mv='mv -i'
[root@centos8 data]# touch file{1..10}.conf
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 12:31 file10.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file1.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file2.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file3.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file4.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file5.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file6.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file7.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file8.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file9.conf
[root@centos8 data]# rename conf conf.bak
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 12:31 file10.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file1.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file2.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file3.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file4.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file5.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file6.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file7.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file8.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file9.conf.bak
[root@centos8 data]# rename .bak ''

[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 12:31 file10.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file1.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file2.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file3.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file4.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file5.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file6.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file7.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file8.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file9.conf

rm
[root@centos8 data]# alias rm
alias rm='rm -i'
[root@centos8 data]# rm file2.conf
rm: remove regular empty file 'file2.conf'? y
[root@centos8 data]# \rm file3.conf
[root@centos8 data]# rm -f file4.conf
[root@centos8 data]# ls -a
. .. file10.conf file1.conf file5.conf file6.conf file7.conf file8.conf file9.conf .txt
[root@centos8 data]# rm -rf *
[root@centos8 data]# ls -a
. .. .txt
[root@centos8 data]# cp -av . /opt/
思考:将 rm 定义成 mv 的别名,实现 rm file = mv file /tmp

tree
-d
-L
-P
[root@centos8 ~]# tree
.
├── anaconda-ks.cfg
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

8 directories, 2 files
[root@centos8 ~]# tree -d /etc/sysconfig/
/etc/sysconfig/
├── cbq
├── console
├── modules
├── network-scripts
└── rhn
├── allowed-actions
│ ├── configfiles
│ └── script
└── clientCaps.d

9 directories

mkdir
mkdir -p

rmdir

ln
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt
drwxr-xr-x. 2 root root 6 Oct 7 19:44 zhl
[root@centos8 data]# ln file.txt zhl/file.txt
[root@centos8 data]# ll
total 0
-rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt
drwxr-xr-x. 2 root root 22 Oct 7 19:44 zhl
[root@centos8 data]# ll file.txt zhl/file.txt
-rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt(连接数变为 2)
-rw-r--r--. 2 root root 0 Oct 7 15:34 zhl/file.txt
[root@centos8 data]# ll -i file.txt zhl/file.txt
132 -rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt
132 -rw-r--r--. 2 root root 0 Oct 7 15:34 zhl/file.txt
[root@centos8 data]# rm file.txt
rm: remove regular empty file 'file.txt'? y
[root@centos8 data]# ll -i file.txt zhl/file.txt
ls: cannot access 'file.txt': No such file or directory
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 zhl/file.txt(文件还能够访问,连接数减 1,不依赖于原文件。删文件,是指删名)
硬连接不能跨文件系统(分区),本质是给一个文件起多个名。跨了分区,节点号有可能冲突
不容许给文件夹建立硬连接,避免存在嵌套的问题(文件夹套子文件,子文件又套自身,造成死循环)
[root@centos8 zhl]# ln -s file.txt f1.txt
[root@centos8 zhl]# ll -i
total 0
134 lrwxrwxrwx. 1 root root 8 Oct 8 17:50 f1.txt -> file.txt()
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt
[root@centos8 zhl]# ln -s file.txt f2.txt
[root@centos8 zhl]# ll -i
total 0
134 lrwxrwxrwx. 1 root root 8 Oct 8 17:50 f1.txt -> file.txt(大小是连接的信息内容,8 个字符数)
135 lrwxrwxrwx. 1 root root 8 Oct 8 17:52 f2.txt -> file.txt
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt(文件连接数没变,节点号不同,是不一样的文件。删除文件,连接做废)
[root@centos8 zhl]#
root@centos8 data]# ln -s f1.txt dir1/f1.txt.link(此处绕,它认为 f1.txt.link 和 f1.txt 在同一个目录里,现实却不是。此时 f1.txt 的相对路径应该是相对于软连接 dir1/f1.txt.link 的相对路径,而不是相对于 data 的。正确的应该是 ln -s ../f1.txt dir1/f1.txt.link。看这个“/data/dir1/f1.txt.link -> /data/f1.txt”,它是从前日后去找到你,因此你不能是相对的)
[root@centos8 data]# ll -i !*
ll -i -s f1.txt dir1/f1.txt.link
33555841 0 lrwxrwxrwx. 1 root root 6 Oct 8 18:07 dir1/f1.txt.link -> f1.txt(此时系统提示 f1.txt 不存在,闪亮。)
132 0 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
[root@centos8 data]# mkdir mysql5.6.10
[root@centos8 data]# ln -s mysql5.6.10/ mysql
[root@centos8 data]# ll -i
total 0
33555840 drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1
132 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
133 lrwxrwxrwx. 1 root root 12 Oct 8 18:22 mysql -> mysql5.6.10/
67161792 drwxr-xr-x. 2 root root 6 Oct 8 18:21 mysql5.6.10
[root@centos8 data]# rm -f mysql
[root@centos8 data]# mkdir mysql5.6.11
[root@centos8 data]# ln -s mysql5.6.11/ mysql
[root@centos8 data]# ll -i
total 0
33555840 drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1
132 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
133 lrwxrwxrwx. 1 root root 12 Oct 8 18:23 mysql -> mysql5.6.11/(软件升级的方式)
67161792 drwxr-xr-x. 2 root root 6 Oct 8 18:21 mysql5.6.10
100664640 drwxr-xr-x. 2 root root 6 Oct 8 18:22 mysql5.6.11
[root@centos8 data]# rm -rf mysql/(多 / 会删文件,没有 / 只是删软连接。多 / 表明进入了,场景变了)(基础不牢,地动山摇。弄清楚区别)

五、复制 /etc/profile 至 /tmp/ 目录,用查找替换命令删除 /tmp/profile 文件中的行首的空白字符

[root@centos8 ~]# cp /etc/profile /tmp/
[root@centos8 ~]# vim /tmp/profile
:%s@^[[:blank:]]+@@g

六、在 vim 中设置 tab 缩进为 4 个字符

:set tabstop=4

文件管理
内容概述
1 文件系统目录结构

bin 二进制文件
bin -> usr/bin
boot 启动内核
[root@centos8 ~]# systemctl status autofs
Unit autofs.service could not be found.
[root@centos8 ~]# yum -y install autofs
[root@centos8 ~]# systemctl start autofs
[root@centos8 ~]# systemctl status autofs
● autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2020-10-06 15:49:17 CST; 7s ago
Main PID: 5181 (automount)
Tasks: 5 (limit: 12364)
Memory: 2.7M
CGroup: /system.slice/autofs.service
└─5181 /usr/sbin/automount --systemd-service --dont-check-daemon

Oct 06 15:49:17 centos8.magedu.org systemd[1]: Starting Automounts filesystems on demand...
Oct 06 15:49:17 centos8.magedu.org automount[5181]: setautomntent: lookup(sss): setautomntent: No such file or d>
Oct 06 15:49:17 centos8.magedu.org automount[5181]: setautomntent: lookup(sss): setautomntent: No such file or d>
Oct 06 15:49:17 centos8.magedu.org automount[5181]: setautomntent: lookup(sss): setautomntent: No such file or d>
Oct 06 15:49:17 centos8.magedu.org systemd[1]: Started Automounts filesystems on demand.
[root@centos8 ~]# ll / | grep misc
drwxr-xr-x. 2 root root 0 Oct 6 15:49 misc
[root@centos8 ~]# lsblk
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]<br/>sr0 11:0 1 7G 0 rom<br/>[root@centos8 ~]# cd /misc/cd<br/>[root@centos8 cd]# lsblk<br/>NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT<br/>sda 8:0 0 200G 0 disk<br/>|-sda1 8:1 0 1G 0 part /boot<br/>|-sda2 8:2 0 100G 0 part /<br/>|-sda3 8:3 0 50G 0 part /data<br/>|-sda4 8:4 0 1K 0 part<br/>-sda5 8:5 0 4G 0 part [SWAP]
sr0 11:0 1 7G 0 rom /misc/cd
[root@centos8 cd]# systemctl enable autofs(使下次开机可使用 autofs)
opt 第三方软件
proc 对应内存数据
run 正在用的程序文件
sbin 系统管理员用的可执行文件
var 可变数据,日志等

1.1 文件系统的目录结构

文件名称大小写敏感
文件名最长 255 个字节
包括路径在内文件名称最长 4095 个字节
文件色彩标识慢慢适应
不使用特殊字符来命名
ext4
[root@centos8 ~]# ll /etc/DIR_COLORS
-rw-r--r--. 1 root root 4536 May 12 2019 /etc/DIR_COLORS
元数据 metadata:数据的属性
数据 data:数据自己,内容

1.2 常见的文件系统目录功能

/boot:引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader,grub)
/bin:全部用户使用的基本命令;不能关联至独立分区,OS 启动即用
/sbin:管理类的基本命令;不能关联至独立分区,OS 启动即用
/lib
/lib64
/etc:配置文件目录
/home/USERNAME
/root
/media
/mnt
/dev:b 随机访问、c 线性访问
/opt:第三方应用程序安装位置
/srv:系统运行的服务用的数据
/tmp
/usr:universal shared, read-only data
/var
/proc:内核与进程虚拟文件
/sys:硬件设备虚拟文件
/selinux

1.3 应用程序的组成部分

二进制程序:bin、sbin
库文件:lib、lib64
配置文件:etc
帮助文件

1.4 CentOS 7 之后版本目录结构变化

[root@centos8 ~]# ll /bin /sbin /lib /lib64
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

1.5 Linux 下的文件类型

c 字符文件,逻辑设备文件,一个字符一个字符写,顺序写
b 块文件,以块的方式读写,有大小的容器,容量,例如磁盘,有缓存空间,随机写
p 管道文件,两个应用程序互联互通,单项传输(网络的单工)
l 连接文件
s 套接字文件,网络服务,双向的(双工)
[root@centos8 ~]# ll /dev/zero
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 /dev/zero
[root@centos8 ~]# dd if=/dev/zero of=f2.txt bs=1 count=1(if=input file,of=output file)

2 文件操做命令
2.1 显示当前工做目录

所在目录 dirname
文件名 basename
[root@centos8 ~]# type dirname
dirname is /usr/bin/dirname
[root@centos8 ~]# type basename
basename is /usr/bin/basename

2.2 绝对和相对路径

相对路径名通常指相对于当前工做目录的路径。有个特殊场景,是相对于某目录的位置 ln
[root@centos8 ~]# ls /boot/vmlinuz-4.18.0-147.el8.x86_64
/boot/vmlinuz-4.18.0-147.el8.x86_64
[root@centos8 ~]# ll -h /boot/vmlinuz-4.18.0-147.el8.x86_64
-rwxr-xr-x. 1 root root 7.8M Dec 5 2019 /boot/vmlinuz-4.18.0-147.el8.x86_64
[root@centos8 ~]# echo $OLDPWD
/root

2.3 更改目录

-P:切换至物理路径
echo $PWD
echo $OLDPWD

2.4 列出目录内容

-a
-l
-R:目录递归
-ld:目录、符号连接
-1
-S:大到小
-t:mtime
-u:配合 -t,atime
-U
-X
/etc/DIR_COLORS
LS_COLORS
[root@centos8 ~]# ls /boot
System.map-4.18.0-147.el8.x86_64 initramfs-4.18.0-147.el8.x86_64.img
config-4.18.0-147.el8.x86_64 loader
efi lost+found
grub2 vmlinuz-0-rescue-7650fa24cc7e4d12acb58cba4449720f
initramfs-0-rescue-7650fa24cc7e4d12acb58cba4449720f.img vmlinuz-4.18.0-147.el8.x86_64
[root@centos8 ~]# ls -R /boot
[root@centos8 ~]# ls -ld /boot
dr-xr-xr-x. 6 root root 4096 Oct 1 19:46 /boot
mtime(modify) ctime(change) atime(access)
[root@centos8 ~]# ll /tmp/test.txt
-rw-r--r--. 1 root root 5 Oct 3 16:01 /tmp/test.txt
[root@centos8 ~]# ll --time=ctime /tmp/test.txt
-rw-r--r--. 1 root root 5 Oct 3 16:01 /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 root root 5 Oct 3 16:02 /tmp/test.txt
[root@centos8 ~]# chown wang /tmp/test.txt
[root@centos8 ~]# ll /tmp/test.txt
-rw-r--r--. 1 wang root 5 Oct 3 16:01 /tmp/test.txt
[root@centos8 ~]# ll --time=ctime /tmp/test.txt
-rw-r--r--. 1 wang root 5 Oct 6 18:47 /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 wang root 5 Oct 3 16:02 /tmp/test.txt
[root@centos8 ~]# > /tmp/test.txt
[root@centos8 ~]# ll /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 6 18:48 /tmp/test.txt
[root@centos8 ~]# ll --time=ctime /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 6 18:48 /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 3 16:02 /tmp/test.txt
[root@centos8 ~]# cat /tmp/test.txt
[root@centos8 ~]# ll --time=atime /tmp/test.txt
-rw-r--r--. 1 wang root 0 Oct 6 18:51 /tmp/test.txt

2.5 查看文件状态 stat

atime 读取文件内容
mtime 改变文件内容(数据)
ctime 元数据改变

[root@centos8 ~]# stat /tmp/test.txt
File: /tmp/test.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 802h/2050d Inode: 1403409 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ wang) Gid: ( 0/ root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2020-10-06 18:51:36.000631508 +0800
Modify: 2020-10-06 18:48:36.833631914 +0800
Change: 2020-10-06 18:48:36.833631914 +0800
Birth: -

2.6 肯定文件内容

magic number
[root@centos8 ~]# file /etc
/etc: directory
[root@centos8 ~]# file /boot/vmlinuz-4.18.0-147.el8.x86_64
/boot/vmlinuz-4.18.0-147.el8.x86_64: Linux kernel x86 boot executable bzImage, version 4.18.0-147.el8.x86_64 (mockbuild@kbuilder.bsys.centos.org) #1 SMP Wed Dec 4 21:51:45 UTC 2019, RO-rootFS, swap_dev 0x7, Normal VGA
[root@centos8 ~]# which file
/usr/bin/file
[root@centos8 ~]# file /usr/bin/file
/usr/bin/file: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=1aaef7e4e7e253a9191d2e7e43d5e7dd8c8ab1e8, stripped
[root@centos8 ~]# file /dev/zero
/dev/zero: character special (1/5)
[root@centos8 ~]# file /dev/sda
/dev/sda: block special (8/0)
[root@centos8 ~]# file /run/autofs.fifo-net
/run/autofs.fifo-net: fifo (named pipe)
[root@centos8 ~]# hexdump -C /tmp/test.txt(区分 Linux、Windows 文件)
Linux 回车换行(继承 Unix,存储节约空间,没了回车字符)
Windows 写的文件,在 Linux 中有回车也有换行
[root@centos8 ~]# dnf -y install dos2unix
Last metadata expiration check: 0:01:09 ago on Tue 06 Oct 2020 10:47:47 PM CST.
Package dos2unix-7.4.0-3.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@centos8 ~]# dos2unix win.txt
[root@centos8 ~]# file win.txt
[root@centos8 ~]# hexdump -C win.txt
[root@centos8 ~]# unix2dos linux.txt
早期 Windows 用的 ANSI 字符集,不是 UTF-8,传到 Linux 中查看不了,编码机制不同,Linux 用的 UTF-8。
解决办法,在 Windows 上把文件另存为 Unicode,而后传入 Linux 可查看,大小比 ANSI 的大。
[root@centos8 ~]# iconv -l
[root@centos8 ~]# file windows.txt
[root@centos8 ~]# iconv -f gb2312 windows.txt -o windows1.txt
[root@centos8 ~]# iconv -f utf8 -t gb2312 windows1.txt -o windows11.txt(转换编码机制)

2.7 文件通配符模式 wildcard pattern

通配符采有特定的符号,表示特定的含义,此特符号称为元 meta 字符

  • 不匹配.开头的文件,即隐藏文件
    ? 匹配任何单个字符
    ~
    ~mage
    ~+或 .
    ~-
    [0-9]
    [a-z]是小大小大的次序写的,aA-z(没有 Z)
    [:digit:]
    [:lower:]
    [:upper:]
    [:alpha:]
    [:alnum:]
    [:blank:]
    [:space:]
    [:punct:]
    [:print:]
    [:cntrl:]
    [:graph:]
    [:xdigit:]
    [root@centos8 data]# ls file[[:lower:]].txt([:lower:]表明 a-z,[[:lower:]]才等于[a-z],表明取一个)
    [wang]四取一
    [^wang]除 wang 四个意外的
    特列:( 不包含隐藏文件)
    [root@centos8 data]# touch .txt
    [root@centos8 data]# ls
    .txt
    ls: cannot access '.txt': No such file or directory
    [root@centos8 data]# ls -a
    .txt
    ls: cannot access '.txt': No such file or directory
    [root@centos8 data]# ls -a
    . .. .txt
    [root@centos8 data]# ls

    ls: cannot access '': No such file or directory
    [root@centos8 data]# rm -f
    (不包括删除隐藏文件)
    比较下
    [root@centos8 data]# ls -a
    [root@centos8 data]# ls -a
    [root@centos8 ~]# ls -d .
    (列出目录自己的,不进去目录)
    . .bash_history .bash_profile .cache .cshrc .esd_auth .local .tcshrc
    .. .bash_logout .bashrc .config .dbus .ICEauthority .pki .Xauthority
    [root@centos8 ~]# ls .*
    .bash_history .bash_logout .bash_profile .bashrc .cshrc .esd_auth .ICEauthority .tcshrc .Xauthority

.:
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos

..:
bin data etc lib media mnt opt root sbin sys usr
boot dev home lib64 misc net proc run srv tmp var

.cache:
dconf gnome-shell libgweather
event-sound-cache.tdb.7650fa24cc7e4d12acb58cba4449720f.x86_64-redhat-linux-gnu gnome-software tracker
evolution gstreamer-1.0 yelp

.config:
dconf gconf gnome-session gtk-3.0 pulse user-dirs.locale
evolution gnome-initial-setup-done goa-1.0 ibus user-dirs.dirs yelp

.dbus:
session-bus

.local:
share

.pki:
nssdb
[root@centos8 ~]# l.
. .bash_history .bash_profile .cache .cshrc .esd_auth .local .tcshrc
.. .bash_logout .bashrc .config .dbus .ICEauthority .pki .Xauthority
[root@centos8 ~]# ls -d .
. .bash_history .bash_profile .cache .cshrc .esd_auth .local .tcshrc
.. .bash_logout .bashrc .config .dbus .ICEauthority .pki .Xauthority
[root@centos8 ~]# alias l.
alias l.='ls -d .
--color=auto'
[root@centos8 ~]# ls -A
anaconda-ks.cfg .bash_profile .config Desktop .esd_auth .local .pki Templates
.bash_history .bashrc .cshrc Documents .ICEauthority Music Public Videos
.bash_logout .cache .dbus Downloads initial-setup-ks.cfg Pictures .tcshrc .Xauthority
[root@centos8 ~]# ls -d /(偏门小技巧,列文件夹待有 /)
Desktop/ Documents/ Downloads/ Music/ Pictures/ Public/ Templates/ Videos/
[root@centos8 data]# touch file[[:upper:]].txt(touch 建立不支持通配符,你让它怎么建。只能查看是在现有状况下去匹配)
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 09:16 'file[[:upper:]].txt'
[root@centos8 data]# touch file
.log
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 09:17 'file*.log'(单引号去掉通配符功能)
-rw-r--r--. 1 root root 0 Oct 7 09:16 'file[[:upper:]].txt'
通配符是过滤现有文件的

2.8 建立空文件和刷新时间

-a
-m
-t
-c
[root@centos8 ~]# ll /etc/issue
-rw-r--r--. 1 root root 23 Jan 2 2020 /etc/issue
[root@centos8 ~]# touch /etc/issue
[root@centos8 ~]# stat /etc/issue
File: /etc/issue
Size: 23 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 134341451 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:etc_t:s0
Access: 2020-10-07 09:23:55.889008789 +0800
Modify: 2020-10-07 09:23:55.889008789 +0800
Change: 2020-10-07 09:23:55.889008789 +0800
Birth: -

2.9 复制文件和目录

-i
-n
-r,-R
-d
cp -p(保留权限、全部者、时间戳)
cp -r
拷贝的快捷方式,目标是文件
cp -d 不复制原文件,只复制连接名
cp -a(经常使用于备份,保留全部内容,包含 r)
cp -av(v 显示进程)
cp -f(普通用户在本身家里,能够删除 root 的文件,此处跟权限有关。先删后拷。if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used))
cp -u(防止覆盖新文件)
cp -b
cp -a(普通用户拷贝别人的文件时,属性是本身,没有保留别人,这是合乎逻辑的)
[root@centos8 data]# cp /etc/issue ./issue.bak
[root@centos8 data]# ll !*
ll /etc/issue ./issue.bak
-rw-r--r--. 1 root root 23 Oct 7 09:23 /etc/issue
-rw-r--r--. 1 root root 23 Oct 7 10:07 ./issue.bak
[root@centos8 data]# cat /etc/issue
\S
Kernel \r on an \m

[root@centos8 data]# cat issue.bak
\S
Kernel \r on an \m

[root@centos8 data]# alias cp(root cp 是别名,别的用户不是)
alias cp='cp -i'
[root@centos8 data]# cp --backup=numbered /etc/passwd f1.txt
[root@centos8 data]# ll
total 4
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt
[root@centos8 data]# cp --backup=numbered /etc/passwd f1.txt
cp: overwrite 'f1.txt'? y
[root@centos8 data]# ll
total 8
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt.~1~
[root@centos8 data]# cp --backup=numbered /etc/passwd f1.txt(先用过 --backup 后,-b 就用这种模式了)
cp: overwrite 'f1.txt'? y
[root@centos8 data]# ll
total 12
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt.~1~
-rw-r--r--. 1 root root 2424 Oct 7 10:38 f1.txt.~2~
[root@centos8 data]# cp -r /etc/ /data/etcbackup
[root@centos8 data]# ll
total 12
drwxr-xr-x. 136 root root 8192 Oct 7 10:48 etcbackup
[root@centos8 data]# cp -r /etc/ /data/etcbackup
[root@centos8 data]# ll
total 12
drwxr-xr-x. 137 root root 8192 Oct 7 10:48 etcbackup
[root@centos8 data]# ls -d ./etcbackup/etc
./etcbackup/etc
[root@centos8 data]# cp -r /etc/ /data/etcbackup
cp: overwrite '/data/etcbackup/etc/xdg/menus/applications.menu'? y
[root@centos8 data]# ll /dev/zero
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 /dev/zero
[root@centos8 data]# cp /dev/zero zero.bak
^C
[root@centos8 data]# ll !
ll /dev/zero zero.bak
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 /dev/zero
-rw-r--r--. 1 root root 415825920 Oct 7 11:05 zero.bak
[root@centos8 data]# ll -h
total 397M
-rw-r--r--. 1 root root 397M Oct 7 11:05 zero.bak
[root@centos8 data]# hexdump -C zero.bak
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|

18c90000
[root@centos8 data]# rm -f zero.bak
[root@centos8 data]# cp -a /dev/zero zero.bak
[root@centos8 data]# ll
total 0
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 zero.bak
[root@centos8 data]# cp -p /dev/zero zero.bak2
^C
[root@centos8 data]# ll
total 527800
crw-rw-rw-. 1 root root 1, 5 Oct 6 10:17 zero.bak
-rw-------. 1 root root 540467200 Oct 7 11:07 zero.bak2

2.10 移动和重命名文件

同一分区快
不一样分区慢
-i
-f
-b
[root@centos8 ~]# alias mv(root 下也是别名)
alias mv='mv -i'
[root@centos8 data]# touch file{1..10}.conf
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 12:31 file10.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file1.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file2.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file3.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file4.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file5.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file6.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file7.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file8.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file9.conf
[root@centos8 data]# rename conf conf.bak
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 12:31 file10.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file1.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file2.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file3.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file4.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file5.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file6.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file7.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file8.conf.bak
-rw-r--r--. 1 root root 0 Oct 7 12:31 file9.conf.bak
[root@centos8 data]# rename .bak ''

[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 12:31 file10.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file1.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file2.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file3.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file4.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file5.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file6.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file7.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file8.conf
-rw-r--r--. 1 root root 0 Oct 7 12:31 file9.conf

2.11 删除文件

shred
[root@centos8 data]# alias rm
alias rm='rm -i'
[root@centos8 data]# rm file2.conf
rm: remove regular empty file 'file2.conf'? y
[root@centos8 data]# \rm file3.conf
[root@centos8 data]# rm -f file4.conf
[root@centos8 data]# ls -a
. .. file10.conf file1.conf file5.conf file6.conf file7.conf file8.conf file9.conf .txt
[root@centos8 data]# rm -rf *
[root@centos8 data]# ls -a
. .. .txt
[root@centos8 data]# cp -av . /opt/
思考:将 rm 定义成 mv 的别名,实现 rm file = mv file /tmp

2.12 目录操做
2.12.1 显示目录树 tree

-d
-L
-P
[root@centos8 ~]# tree
.
├── anaconda-ks.cfg
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

8 directories, 2 files
[root@centos8 ~]# tree -d /etc/sysconfig/
/etc/sysconfig/
├── cbq
├── console
├── modules
├── network-scripts
└── rhn
├── allowed-actions
│ ├── configfiles
│ └── script
└── clientCaps.d

9 directories

2.12.2 建立目录 mkdir

mkdir -p

2.12.3 删除空目录 rmdir

3 文件元数据和节点表结构

文件的元数据存放在 node(index node)表中
连接数(指向这个文件名路径名称个数)
通常 iNode 表会占用文件系统磁盘空间的 1%
cp 和 inode:分配一个空闲的 inode 号,在 inode 表中生成新条目;在目录中建立一个目录项,将名称与 iNode 编号关联;拷贝数据生成新的文件
rm 和 inode:连接数递减,从而释放的 iNode 号能够被重用;把数据块放在空闲列表中;删除目录项;数据实际上不会立刻被删除
mv 和 iNode:若是 mv 命令的目标和源在相同的文件系统,做为 mv 命令,用新的文件名建立对应新的目录项,删除旧目录条目对应的旧的文件名;不影响 iNode 表(除时间戳)或磁盘上的数据位置:没有数据被移动!若是目标和源在不一样的文件系统,mv 至关于 cp+rm
指向磁盘上文件的数据块指针
直接指针 12 个,124K=48K
直接块指针
间接块指针:指向指针块,一个指针占 4 个字节,一个指针块放 1024 个指针,4K
1024=4M
双重间接块指针:102410244K=4G
三重间接块指针:4T
目录:存放的文件列表和 iNode 对应关系
同一分区移动文件,文件的 iNode 号不变。移动到别的分区,文件 iNode 号变
每一个分区的节点编号是惟一的
[root@centos8 data]# stat file.txt
File: file.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 132 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-10-07 15:34:18.780029999 +0800
Modify: 2020-10-07 15:34:18.780029999 +0800
Change: 2020-10-07 15:34:18.780029999 +0800
Birth: -
[root@centos8 data]# ls -i
132 file.txt
[root@centos8 data]# ll -i
total 0
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt

3.1 inode 表结构

[root@centos8 data]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
devtmpfs 247297 397 246900 1% /dev
tmpfs 251531 1 251530 1% /dev/shm
tmpfs 251531 681 250850 1% /run
tmpfs 251531 17 251514 1% /sys/fs/cgroup
/dev/sda2 52428800 119210 52309590 1% /
/dev/sda3 26214400 5 26214395 1% /data
/dev/sda1 65536 308 65228 1% /boot
tmpfs 251531 11 251520 1% /run/user/0

3.2 硬(hard)连接

硬连接的本质就是给文件起个新名,实质是同一文件。
建立硬连接会在对应的目录增长额外记录项以引用文件
每一个目录引用相同的 iNode 号
建立时连接数递增
不一样分区的两个不一样文件,节点编号可能同样
同一分区,两个不一样的文件,不能用相同的节点编号
同一个文件,不容许有多个不一样的节点编号
[root@centos8 data]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt
drwxr-xr-x. 2 root root 6 Oct 7 19:44 zhl
[root@centos8 data]# ln file.txt zhl/file.txt
[root@centos8 data]# ll
total 0
-rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt
drwxr-xr-x. 2 root root 22 Oct 7 19:44 zhl
[root@centos8 data]# ll file.txt zhl/file.txt
-rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt(连接数变为 2)
-rw-r--r--. 2 root root 0 Oct 7 15:34 zhl/file.txt
[root@centos8 data]# ll -i file.txt zhl/file.txt
132 -rw-r--r--. 2 root root 0 Oct 7 15:34 file.txt
132 -rw-r--r--. 2 root root 0 Oct 7 15:34 zhl/file.txt
[root@centos8 data]# rm file.txt
rm: remove regular empty file 'file.txt'? y
[root@centos8 data]# ll -i file.txt zhl/file.txt
ls: cannot access 'file.txt': No such file or directory
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 zhl/file.txt(文件还能够访问,连接数减 1,不依赖于原文件。删文件,是指删名)
硬连接不能夸文件系统(分区),本质是给一个文件起多个名。夸了分区,节点号有可能冲突
不容许给文件夹建立硬连接,避免存在嵌套的问题(文件夹套子文件,子文件又套自身,造成死循环)

drop 文件,就是在每一个数据块上 mark free。drop 大文件的话,会瞬间产生大量 IO。有大量用户正在访问的话,就会出问题。
解决办法
先建立个硬连接,数据有了两个名
而后再 drop 文件,此时只是少了个名,空间还在用,就不会去 mark
系统空闲的时候,再清理文件
yum 安装就是待两次,第一次失败,第二次正常

3.3 符号 symbolic(或软 soft)连接

一个符号连接指向另外一个文件,就像 Windows 快捷方式,软连接文件和原文件本质不是同一文件
一个符号连接的内容是它引用文件的名称
指向的是另外一个文件的路径;其大小为指向的路径字符串的长度;不增长或减小目标文件 iNode 的引用计数
软连接若是使用相对路径,是相对于原文件的路径,而非相对于当前目录
readlink
[root@centos8 zhl]# ln -s file.txt f1.txt
[root@centos8 zhl]# ll -i
total 0
134 lrwxrwxrwx. 1 root root 8 Oct 8 17:50 f1.txt -> file.txt()
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt
[root@centos8 zhl]# ln -s file.txt f2.txt
[root@centos8 zhl]# ll -i
total 0
134 lrwxrwxrwx. 1 root root 8 Oct 8 17:50 f1.txt -> file.txt(大小是连接的信息内容,8 个字符数)
135 lrwxrwxrwx. 1 root root 8 Oct 8 17:52 f2.txt -> file.txt
132 -rw-r--r--. 1 root root 0 Oct 7 15:34 file.txt(文件连接数没变,节点号不同,是不一样的文件。删除文件,连接做废)
[root@centos8 zhl]#
root@centos8 data]# ln -s f1.txt dir1/f1.txt.link(此处绕,它认为 f1.txt.link 和 f1.txt 在同一个目录里,现实却不是。此时 f1.txt 的相对路径应该是相对于软连接 dir1/f1.txt.link 的相对路径,而不是相对于 data 的。正确的应该是 ln -s ../f1.txt dir1/f1.txt.link。看这个“/data/dir1/f1.txt.link -> /data/f1.txt”,它是从前日后去找到你,因此你不能是相对的)
[root@centos8 data]# ll -i !*
ll -i -s f1.txt dir1/f1.txt.link
33555841 0 lrwxrwxrwx. 1 root root 6 Oct 8 18:07 dir1/f1.txt.link -> f1.txt(此时系统提示 f1.txt 不存在,闪亮。)
132 0 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
[root@centos8 data]# mkdir mysql5.6.10
[root@centos8 data]# ln -s mysql5.6.10/ mysql
[root@centos8 data]# ll -i
total 0
33555840 drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1
132 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
133 lrwxrwxrwx. 1 root root 12 Oct 8 18:22 mysql -> mysql5.6.10/
67161792 drwxr-xr-x. 2 root root 6 Oct 8 18:21 mysql5.6.10
[root@centos8 data]# rm -f mysql
[root@centos8 data]# mkdir mysql5.6.11
[root@centos8 data]# ln -s mysql5.6.11/ mysql
[root@centos8 data]# ll -i
total 0
33555840 drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1
132 -rw-r--r--. 1 root root 0 Oct 8 18:06 f1.txt
133 lrwxrwxrwx. 1 root root 12 Oct 8 18:23 mysql -> mysql5.6.11/(软件升级的方式)
67161792 drwxr-xr-x. 2 root root 6 Oct 8 18:21 mysql5.6.10
100664640 drwxr-xr-x. 2 root root 6 Oct 8 18:22 mysql5.6.11
[root@centos8 data]# rm -rf mysql/(多 / 会删文件,没有 / 只是删软连接。多 / 表明进入了,场景变了)(基础不牢,地动山摇。弄清楚区别)

3.4 硬连接和软连接区别总结

硬连接本质是同一个文件
软连接本质是不一样一个文件
硬连接不支持文件夹
软连接:原始文件的相对路径是相对于连接文件的相对路径
[root@centos8 data]# ll
total 0
drwxr-xr-x. 2 root root 25 Oct 8 18:07 dir1(天生就是 2,. 用的与其相同的节点编号。能不能变成 3 呢?能够,建个子目录,子目录的 .. 就是它了,因此能够是 3。以此类推)

3.5 生产案例
3.5.1 案例1:提示空间满 No space left on device,但 df 能够看到空间不少,为何?

[root@centos8 testzhl]# df -i /boot
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 65536 309 65227 1% /boot
[root@centos8 testzhl]# touch file{1..65228}
touch: cannot touch 'file65228': No space left on device
[root@centos8 testzhl]# df -h /boot
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 976M 125M 785M 14% /boot
[root@centos8 testzhl]#

3.5.2 案列2:提示空间快满,使用 rm 删除了很大的无用文件后,df 仍然看到空间不足,为何?如何解决?

[root@centos8 testzhl]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 967M 0 967M 0% /dev
tmpfs 983M 0 983M 0% /dev/shm
tmpfs 983M 9.1M 974M 1% /run
tmpfs 983M 0 983M 0% /sys/fs/cgroup
/dev/sda2 100G 4.8G 96G 5% /
/dev/sda3 50G 390M 50G 1% /data
/dev/sda1 976M 125M 785M 14% /boot
tmpfs 197M 4.0K 197M 1% /run/user/0
[root@centos8 testzhl]# dd if=/dev/zero of=/boot/testzhl/bigfile bs=1M count=700
700+0 records in
700+0 records out
734003200 bytes (734 MB, 700 MiB) copied, 2.49676 s, 294 MB/s
[root@centos8 testzhl]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 967M 0 967M 0% /dev
tmpfs 983M 0 983M 0% /dev/shm
tmpfs 983M 9.1M 974M 1% /run
tmpfs 983M 0 983M 0% /sys/fs/cgroup
/dev/sda2 100G 4.8G 96G 5% /
/dev/sda3 50G 390M 50G 1% /data
/dev/sda1 976M 825M 85M 91% /boot
tmpfs 197M 4.0K 197M 1% /run/user/0
[root@centos8 testzhl]# vi /boot/testzhl/bigfile
[root@centos8 testzhl]# ll
total 716804
-rw-r--r--. 1 root root 734003200 Oct 7 18:10 bigfile
[root@centos8 testzhl]# rm bigfile
rm: remove regular file 'bigfile'? y
[root@centos8 testzhl]# du -h /boot
2.5M /boot/grub2/fonts
3.0M /boot/grub2/i386-pc
5.5M /boot/grub2
4.0K /boot/efi/EFI/centos
8.0K /boot/efi/EFI
12K /boot/efi
1.9M /boot/testzhl
16K /boot/lost+found
12K /boot/loader/entries
16K /boot/loader
122M /boot
[root@centos8 testzhl]# df -h /boot
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 976M 825M 85M 91% /boot
[root@centos8 testzhl]# ls bigfile
ls: cannot access 'bigfile': No such file or directory
[root@centos8 testzhl]# lsof | grep delete | grep bigfile
vi 17008 root 5r REG 8,1 734003200 308 /boot/testzhl/bigfile (deleted)
[root@centos8 testzhl]#
退出
[root@centos8 testzhl]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 967M 0 967M 0% /dev
tmpfs 983M 0 983M 0% /dev/shm
tmpfs 983M 9.1M 974M 1% /run
tmpfs 983M 0 983M 0% /sys/fs/cgroup
/dev/sda2 100G 4.8G 96G 5% /
/dev/sda3 50G 390M 50G 1% /data
/dev/sda1 976M 125M 785M 14% /boot
tmpfs 197M 4.0K 197M 1% /run/user/0
正确释放大文件的步骤(不用 >,下面的办法是通用办法,> 有的 sh 不支持)
[root@centos8 testzhl]# cat /dev/null > /boot/testzhl/bigfile(先清,后删。此法不影响应用程序,有些程序只是写文件)
[root@centos8 testzhl]# rm -rf /boot/testzhl/bigfile
yum 安装两次 csh,第一次报错,第二次成功,两次安装没区别

已学:aliasbasenamebccalcat /ect/motd /etc/issuecdchownclearclockcpdatedddf -idirnamednfecho $PS1 $SHELLenableexitexportfilefreegedit 图形化halthashhelphelphexdumphistoryhostname hostnamectliconvinfoinit 0 3 5 6lnlogoutlsblklscpulsofmanmandb mkwhatismkdir -pmvnano /etc/motdpoweroffps auxrebootrenamermrmdirrunlevelshredshutdownsleepsosreportsource = .sreenstatsudo -isystemctltmuxtouchtreettytypeunaliasuptimewhatiswhereiswhichwhoamiwho who am iyum

相关文章
相关标签/搜索