Linux文件管理
1.文件系统目录结构(上图)
1.1 文件系统的目录结构
文件和目录被组织成一个单根倒置树结构
文件系统从根目录下开始,用“/”表示
根文件系统(rootfs):root filesystem
标准Linux文件系统(如:ext4),文件名称大小写敏感,例如:MAIL, Mail, mail, mAiL以 . 开头的文件为隐藏文件
路径分隔的 /
文件名最长255个字节
包括路径在内文件名称最长4095个字节
蓝色-->目录 绿色-->可执行文件 红色-->压缩文件 浅蓝色-->链接文件 灰色-->其他文件
除了斜杠和NUL,所有字符都有效.但使用特殊字符的目录名和文件不推荐使用,有些字符需要用引号来引用
每个文件都有两类相关数据:元数据:metadata,即属性, 数据:data,即文件内容
1.2 常见的文件系统目录功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /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/local 下的应用程序存储可变数据 lock: 锁文件 log: 日志目录及文件 opt: 专用于为 /opt 下的应用程序存储可变数据 run: 运行中的进程相关数据,通常用于存储进程pid文件 spool: 应用程序数据池 tmp: 保存系统两次重启之间产生的临时数据 /proc : 用于输出内核与进程信息相关的虚拟文件系统 /sys :用于输出当前系统上硬件设备相关信息虚拟文件系统 /selinux : security enhanced Linux,selinux相关的安全策略等信息的存储位置 |
1.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
1.4 CentOS 7 以后版本目录结构变化
/bin 和 /usr/bin
/sbin 和 /usr/sbin
/lib 和/usr/lib
/lib64 和 /usr/lib64
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [root@centos7 ~] #ls -ld /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 [root@centos6 ~] #ls -ld /bin /sbin /lib /lib64 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 -d /bin /usr/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/ |
1.5 Linux下的文件类型
1 2 3 4 5 6 7 8 9 10 11 12 13 | - 普通文件 d 目录文件directory b 块设备block c 字符设备character l 符号链接文件link p 管道文件pipe s 套接字文件socket |
案例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@centos7 ~] # ls -l /run/ drwxr-xr-x 2 root root 100 Dec 9 20:41 abrt prw------- 1 root root 0 Dec 9 20:41 autofs.fifo-misc srw-rw-rw- 1 root root 0 Dec 9 20:41 rpcbind.sock ... [root@centos7 ~] # ls -l /bin lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr /sbin ... |
2 文件操作命令
2.1 显示当前工作目录
pwd命令: printing working directory
-P 显示真实物理路径
-L 显示链接路径(默认)
例:
[root@centos7 ~]#cd /data
[root@centos7 data]#pwd
/data
2.2 绝对和相对路径
绝对路径
以正斜杠/ 即根目录开始
完整的文件的位置路径
可用于任何想指定一个文件名的时候
相对路径名
不以斜线开始
一般情况下,是指相对于当前工作目录的路径,特殊场景下,是相对于某目录的位置
可以作为一个简短的形式指定一个文件名
基名:basename,只取文件名而不要路径
目录名:dirname,只取路径,不要文件名
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@centos7 bin] #basename /etc/sysconfig/network network [root@centos7 bin] #dirname /etc/sysconfig/network /etc/sysconfig [root@centos7 ~] #dirname /etc/sysconfig/network-scripts/ifcfg-eths33 /etc/sysconfig/network-scripts [root@centos7 ~] #basename /etc/sysconfig/network-scripts/ifcfg-eths33 ifcfg-eths33 |
2.3 更改目录
命令 cd : change directory 改变目录
选项:-P 切换至物理路径,而非软链接目录
可以使用绝对或相对路径
切换至父目录: cd ..
切换至当前用户主目录: cd
切换至以前的工作目录: cd -
例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@centos7 ~] #cd /etc/sysconfig [root@centos7 sysconfig] #pwd /etc/sysconfig [root@centos7 sysconfig] #cd ../../usr [root@centos7 usr] #pwd /usr [root@centos7 usr ] #cd /bin [root@centos7 bin] #pwd /bin [root@centos7 bin] #cd -P /bin [root@centos7 bin] #pwd /usr/bin |
相关的环境变量:
PWD:当前目录路径
OLDPWD:上一次目录路径
案例:
1 2 3 4 5 6 7 8 9 10 11 | [root@centos7 ~] #echo $OLDPWD /etc/sysconfig [root@centos7 ~] #cd - /etc/sysconfig [root@centos7 sysconfig] #pwd /etc/sysconfig |
2.4 列出目录内容
ls 命令可以列出当前目录的内容或指定目录
用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | ls [options] [files_or_dirs] 常见选项: -a 包含隐藏文件 -l 显示额外的信息 -R 目录递归 -ld 目录和符号链接信息 -1 文件分行显示 -S 按从大到小排序 -t 按mtime排序 -u 配合-t选项,显示并按atime从新到旧排序 -U 按目录存放顺序显示 -X 按文件后缀排序 -F 对不同类型文件显示时附加不同的符号:*/=>@| -C 文件多时,以多列的方式显示文件,默认是一列(标准输出) |
注意:
ls 查看不同后缀文件时的颜色由 /etc/DIR_COLORS 和@LS_COLORS变量定义
ls -l 看到文件的大小,不一定是实际文件真正占用空间的大小
例:
1 2 3 4 5 6 7 8 9 | [root@centos7 ~] # vim /etc/DIR_COLORS .jpg 01;31 #修改此行,保存 [root@centos7 ~] # echo $LS_COLORS [root@centos7 ~] # ls test.jpg #可以看到颜色是红色的 test .jpg |
2.5 查看文件状态 stat
文件相关信息:metadata, data
每个文件有三个时间戳:
access time 访问时间,atime,读取文件内容
modify time 修改时间,mtime,改变文件内容(数据)
change time 改变时间,ctime,元数据发生改变
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@centos7 ~] #stat /etc/passwd File: /etc/passwd Size: 1306 Blocks: 8 IO Block: 4096 regular file Device: 802h /2050d Inode: 134792556 Links: 1 Access: (0644 /-rw-r--r-- ) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2021-12-09 20:37:12.830991350 +0800 Modify: 2021-12-09 20:37:12.826991351 +0800 Change: 2021-12-09 20:37:12.826991351 +0800 |
2.6 确定文件内容
文件可以包含多种类型的数据,使用file命令检查文件的类型,然后确定适当的打开命令或应用程序使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 格式: file [options] <filename>... 常用选项: -b 列出文件辨识结果时,不显示文件名称 -f filelist 列出文件filelist中文件名的文件类型 -F 使用指定分隔符号替换输出文件名后默认的”:”分隔符 -L 查看对应软链接对应文件的文件类型 --help 显示命令在线帮助 |
范例:windows的文本格式和Linux的文本格式的区别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [root@centos7 data] #cat linux.txt a b c [root@centos7 data] #cat win.txt a b c c[root@centos7 data] #file win.txt linux.txt win.txt: ASCII text, with CRLF line terminators linux.txt: ASCII text [root@centos7 data] #hexdump -C linux.txt 00000000 61 0a 62 0a 63 0a |a.b.c.| 00000006 [root@centos7 data] #hexdump -C win.txt 00000000 61 0d 0a 62 0d 0a 63 |a..b..c| 00000007 |
centos8将Windows的文本格式转换成的Linux文本格式
#安装转换工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@centos8 data] #dnf -y install dos2unix [root@centos8 data] #dos2unix win.txt [root@centos8 data] #file win.txt win.txt: ASCII text #将Linux的文本格式转换成Windows的文本格式 [root@centos8 data] #unix2dos win.txt unix2dos: converting file win.txt to DOS format ... [root@centos8 data] #file win.txt win.txt: ASCII text, with CRLF line terminators |
例:转换文件字符集编码
#显示支持字符集编码列表
[root@centos8 ~]#iconv -l
#windows7上文本默认的编码ANSI(GB2312)
[root@centos8 data]#file windows.txt
windows.txt: ISO-8859 text, with no line terminators
#查看当前使用字符集
[root@centos8 data]#echo $LANG
en_US.UTF-8
#默认在linux无法正常显示文本内容,乱码
#将windows7上文本默认的编码ANSI(GB2312)转换成UTF-8
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@centos8 data] #iconv -f gb2312 windows.txt -o windows1.txt [root@centos8 data] #cat windows1.txt 测试 [root@centos8 data] #ll windows1.txt -rw-r--r-- 1 root root 12 Mar 23 10:13 windows1.txt [root@centos8 data] #file windows1.txt windows1.txt: UTF-8 Unicode text, with no line terminators |
#将UTF-8转换成windows10上文本默认的编码ANSI(GB2312)
1 2 3 4 5 | [root@centos8 data] #iconv -f utf8 -t gb2312 windows1.txt -o windows2.txt [root@centos8 data] #file windows2.txt windows2.txt: ISO-8859 text, with no line terminators |
例2:
1 2 3 4 5 6 7 | [root@centos7 ~] #file linux.txt linux.txt: ASCII text [root@centos7 ~] #file windows.txt windows.txt: ASCII text, with CRLF line terminators |
#将windows的文本格式转换成Linux的文本格式
1 2 3 4 5 6 7 | [root@centos7 ~] #dos2unix windows.txt dos2unix: converting file windows.txt to Unix format ... [root@centos7 ~] #file windows.txt windows.txt: ASCII text |
例3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@centos7 ~] #cat list.txt /etc/ /bin /etc/issue [root@centos7 ~] #file -f list.txt /etc/ : directory /bin : symbolic link to usr /bin /etc/issue : ASCII text |
2.7 文件通配符模式 wildcard pattern
文件通配符可以用来匹配符合条件的多个文件,方便批量管理文件
通配符采有特定的符号,表示特定的含义,此特符号称为元 meta 字符
常见的通配符如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | * 匹配零个或多个字符,但不匹配 "." 开头的文件,即隐藏文件 ? 匹配任何单个字符 ~ 当前用户家目录 ~mage 用户mage家目录 ~+和. 当前工作目录 ~- 前一个工作目录 [0-9] 匹配数字范围 [a-z] 一个字母 [A-Z] 一个字母 [ test ] 匹配列表中的任何的一个字符 [^ test 匹配列表中的所有字符以外的字符 [^a-z] 匹配列表中的所有字符以外的字符 |
另外还有在Linux系统中预定义的字符类:man 7 glob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [:digit:]:任意数字,相当于0-9 [:lower:]:任意小写字母,表示 a-z [:upper:]: 任意大写字母,表示 A-Z [:alpha:]: 任意大小写字母 [:alnum:]:任意数字或字母 [:blank:]:水平空白字符 [:space:]:水平或垂直空白字符 [:punct:]:标点符号 [:print:]:可打印字符 [:cntrl:]:控制(非打印)字符 [:graph:]:图形字符 [:xdigit:]:十六进制字符 |
例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [root@centos7 data] #ll f{a..c} -rw-r--r-- 1 root root 0 Nov 7 09:46 fa -rw-r--r-- 1 root root 0 Nov 7 09:46 fb -rw-r--r-- 1 root root 0 Nov 7 09:46 fc [root@centos8 data] #ll f[a-c] -rw-r--r-- 1 root root 0 Nov 7 09:46 fa -rw-r--r-- 1 root root 0 Nov 7 09:46 fA -rw-r--r-- 1 root root 0 Nov 7 09:46 fb -rw-r--r-- 1 root root 0 Nov 7 09:46 fB -rw-r--r-- 1 root root 0 Nov 7 09:46 fc |
例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | [root@centos7 data] # touch file{a..z}.txt file{A..Z}.txt file{0.9}.txt [root@centos7 data] # ls file{0..9}.txt file0.txt file2.txt file4.txt file6.txt file8.txt file1.txt file3.txt file5.txt file7.txt file9.txt [root@centos7 data] # ls file{a..c}* filea.txt fileb.txt filec.txt [root@centos7data] #ls file[a-c].txt filea.txt fileA.txt fileb.txt fileB.txt filec.txt [root@centos7 data] #ls file[C-E].txt fileC.txt filed.txt fileD.txt filee.txt fileE.txt [root@centos7 data] #ls file[tesd].txt filed.txt filee.txt files.txt filet.txt [root@centos7 data] #ls file[^tesd].txt |
例3:
1 2 3 4 5 6 7 8 9 10 11 | [root@centos7 data] #ls file[:lower:].txt filee.txt filel.txt fileo.txt filer.txt filew.txt [root@centos7 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 |
2.8 创建空文件和刷新时间
touch命令可以用来创建空文件或刷新文件的时间
1 2 3 4 5 6 7 8 9 10 11 12 13 | 语法: touch [OPTION]... FILE... 选项说明: -a 仅改变 atime和ctime -m 仅改变 mtime和ctime -t [[CC]YY]MMDDhhmm[.ss] 指定atime和mtime的时间戳 -c 如果文件不存在,则不予创建 |
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [root@centos7 data] #touch f1.txt [root@centos7 data] #ll total 0 -rw-r--r-- 1 root root 0 Mar 23 11:30 f1.txt [root@centos7 data] #ll /etc/issue -rw-r--r--. 1 root root 23 Jan 2 23:21 /etc/issue [root@centos7 data] #touch /etc/issue [root@centos7 data] #ll /etc/issue -rw-r--r--. 1 root root 23 Mar 23 11:31 /etc/issue [root@centos7 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: 2021-03-23 11:31:20.030291332 +0800 Modify: 2021-03-23 11:31:20.030291332 +0800 Change: 2021-03-23 11:31:20.030291332 +0800 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@centos7 data] #date Fri Dec 13 16:11:55 CST 2019 [root@centos7 data] #touch `date -d "-1 day" +%F_%T`.log [root@centos7 data] #ls 2019-12-12_16:11:48.log [root@centos7 data] #touch $(date -d "1 year" +%F_%T).log [root@centos7 data] #ls 2019-12-12_16:11:48.log 2020-12-13_16:13:11.log |
2.9 复制文件和目录
使用cp(copy)命令可以实现文件或目录的复制
格式:
1 2 3 4 5 | cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 常用选项 -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.~ #~ ,可以保留多 个版本 |
源目录 | 不存在 | 存在且为文件 | 存在且为目录 |
一个文件 |
新建EDST,并将SRC中内容填充到EDST中 | 将SRC中的内容覆盖到EDST中注意数据丢失风险!建议使用-i选项 |
在DEST下新建与原文件同名的文件,并将SRC中内容填充至新文件中 |
多个文件 | 提示错误 | 错误 |
在DEST下新建与原文件同名的文件,并将原文件内容复制进新文件中 |
目录需使用-r选项 |
创建指定DEST同名目录,复制SRC目录中所有文件至DEST下 |
提示错误 |
在DEST下新建与原目录同名的目录,并将SRC中内容复制至新目录中 |
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | [root@centos7 ~] #cp ~test/issue /data/issue_test.bak -rw-r--r--. 1 root root 86 Dec 13 16:21 /data/issue_test .bak -rw-r--r--. 1 test test 86 Dec 13 16:18 /home/test/issue [root@centos7 ~] #cp -p ~test/issue /data/issue_test2.bak [root@centos7 ~] #ll ~test/issue /data/issue_test2.bak -rw-r--r--. 1 test test 86 Dec 13 16:18 /data/issue_test2 .bak -rw-r--r--. 1 test test 86 Dec 13 16:18 /home/test/issue [root@centos7 ~] #cp /etc/sysconfig/ /data/ cp : -r not specified; omitting directory '/etc/sysconfig/' [root@centos7 ~] #cp -r /etc/sysconfig/ /data/ [root@centos7 ~] #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 test test 86 Dec 13 16:18 issue_test2.bak -rw-r--r--. 1 root root 86 Dec 13 16:21 issue_test.bak drwxr-xr-x. 7 root root 4096 Dec 13 16:23 sysconfig [root@centos7 ~] #cp -r /etc/sysconfig/ /data/sysconfig_bak [root@centos7 ~] #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 test test 86 Dec 13 16:18 issue_test2.bak -rw-r--r--. 1 root root 86 Dec 13 16:21 issue_test.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 [root@centos7 ~] #cp -b /etc/motd /data/issue cp : overwrite '/data/issue' ? y [root@centos7 ~] #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~ [root@centos7 ~] #cp -b /etc/fstab /data/issue cp : overwrite '/data/issue' ? y [root@centos7 ~] #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~ [root@centos7 ~] #cp --backup=numbered /etc/fstab /data/issue cp : overwrite '/data/issue' ? y [root@centos7 ~] #cp --backup=numbered /etc/shadow /data/issue cp : overwrite '/data/issue' ? y [root@centos7 ~] #cp --backup=numbered /etc/os-release /data/issue cp : overwrite '/data/issue' ? y [root@centos7 ~] #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~l |
例:
[root@centos7 ~]#cp /dev/zero /data/zero.bak
每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为 backupYYYYmm-
dd,备份过程可见
1 2 | [root@centos8 ~] #cp -av /etc/ /data/backup`date +%F` [root@centos8 ~] #cp -av /etc/ /data/etc-`date +%F_%H-%M-%S` |
创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限
1 | [root@centos8 ~] #cp -a /root /data/rootdir |
2.10 移动和重命名文件
mv 命令可以实现文件或目录的移动和改名
同一分区移动数据,速度很快:数据位置没有变化
不同分区移动数据,速度相对慢:数据位置发生了变化
格式:
1 2 3 | mv [OPTION]... [-T] SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY mv [OPTION]... -t DIRECTORY SOURCE... |
常用选项:
-i 交互式
-f 强制
-b 目标存在,覆盖前先备份
利用 rename 可以批量修改文件名
格式
1 | rename [options] <expression> <replacement> < file >... |
1 2 3 4 | #为所有的conf文件加上.bak后缀: rename 'conf' 'conf.bak' f* #去掉所有的bak后缀: rename '.bak' '' *.bak |
2.11 删除文件
使用 rm 命令可以删除文件
注意:此命令非常危险,慎重使用,建议使用 mv 代替 rm
格式:
1 | rm [OPTION]... FILE... |
常用选项
1 2 3 4 | -i 交互式 -f 强制删除 -r 递归 --no-preserve-root 删除/ |
案例1
1 2 3 4 5 6 7 | [root@centos8 ~] #rm -rf / rm : it is dangerous to operate recursively on '/' rm : use --no-preserve-root to override this failsafe #思考删除/ 文件,如何验证 [root@centos8 ~] #rm -rf /* [root@centos8 ~] #/2TAB [root@centos8 ~] #echo *a |
案例2,删除特殊文件
1 2 3 4 5 6 7 8 9 10 11 12 | #将名为 “/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格式
1 | shred [OPTION]... FILE...c |
常用选项
1 2 3 4 | -z 最后一次覆盖添加0,以隐藏覆盖操作 - v 能够显示操作进度 -u 覆盖后截断并删除文件 -n # 指定覆盖文件内容的次数(默认值是3次) |
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@centos7 ~] #shred -zvun 5 passwd.txt shred: passwd .txt: pass 1 /6 (random)... shred: passwd .txt: pass 2 /6 (000000)... shred: passwd .txt: pass 3 /6 (random)... shred: passwd .txt: pass 4 /6 (ffffff)... shred: passwd .txt: pass 5 /6 (random)... shred: passwd .txt: pass 6 /6 (000000)... shred: passwd .txt: removing shred: passwd .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: passwd .txt: removed [root@centos8 ~] #ls passwd.txt ls : cannot access 'passwd.txt' : No such file or directory |
2.12 目录操作
2.12.1 显示目录树 tree
常用选项
1 2 3 | -d: 只显示目录 -L level:指定显示的层级数目 -P pattern: 只显示由指定wild-card pattern匹配到的路径 |
2.12.2 创建目录 mkdir
常用选项
1 2 3 | -p: 存在于不报错,且可自动创建所需的各目录 - v : 显示详细信息 -m MODE: 创建目录时直接指定权限 |
2.11.3 删除空目录 rmdir
常见选项
1 2 | -p 递归删除父空目录 - v 显示详细信息 |
注意:rmdir只能删除空目录,如果想删除非空目录,可以使用rm -r 命令,递归删除目录树
例
1 | alias rm = 'DIR=/data/backup`date +%F%T`;mkdir $DIR;mv -t $DIR' |
3 文件元数据和节点表结构
3.1 inode 表结构
每个文件的属性信息,比如:文件的大小,时间,类型等,称为文件的元数据(meta data)。这此元数据是存放在inode(index node)表中。inode 表中有很多条记录组成,第一条记录对应的存放了一个文件的元数据信息
每一个inode表记录对应的保存了以下信息:
1 2 3 4 5 6 7 8 9 | inode number 节点号 文件类型 权限 UID GID 链接数(指向这个文件名路径名称个数) 该文件的大小和不同的时间戳 指向磁盘上文件的数据块指针 有关文件的其他数据 |
目录
目录是个特殊文件,目录文件的内容保存了此目录中文件的列表及inode number对应关系
1 2 3 | 文件引用一个是 inode号 人是通过文件名来引用一个文件 一个目录是目录下的文件名和文件inode号之间的映射 |
inode表和目录(如下图)
cp和inode
cp命令:
1 2 3 | 分配一个空闲的inode号,在inode表中生成新条目 在目录中创建一个目录项,将名称与inode编号关联 拷贝数据生成新的文件 |
rm和inode
rm命令
1 2 3 4 | 链接数递减,从而释放的inode号可以被重用 把数据块放在空闲列表中 删除目录项 数据实际上不会马上被删除,但当另一个文件使用数据块时将被覆盖 |
mv和inode
1 2 3 4 5 | 如果 mv 命令的目标和源在相同的文件系统,作为 mv 命令 用新的文件名创建对应新的目录项 删除旧目录条目对应的旧的文件名 不影响inode表(除时间戳)或磁盘上的数据位置:没有数据被移动! 如果目标和源在一个不同的文件系统, mv 相当于 cp 和rma |
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@centos7 ~] #df -i /boot Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 65536 310 65226 1% /boot [root@centos7 ~] #mkdir /boot/test [root@centos7 ~] #touch /boot/test/file{1..65226} [root@centos7 ~] #touch /boot/test/new.txt touch : cannot touch 'new.txt' : No space left on device [root@centos7 ~] #df /boot Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 999320 133800 796708 15% /boot [root@centos7 ~] #df -i /boot Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 65536 65536 0 100% /boot |
案例:删除大文件
1 | [root@centos7 ~] #cat /dev/null > /var/log/huge.log |
3.2 硬链接in
硬链接本质上就给一个文件起一个新的名称,实质是同一个文件
硬链接特性
1 2 3 4 5 6 7 | 创建硬链接会在对应的目录中增加额外的记录项以引用文件 对应于同一文件系统上一个物理文件 每个目录引用相同的inode号 创建时链接数递增 删除文件时: rm 命令递减计数的链接,文件要存在,至少有一个链接数,当链接数为零时,该文件被删除 不能跨越驱动器或分区 不支持对目录创建硬链接 |
格式
1 | ln filename [linkname ] |
3.3 符号 symbolic (或软 soft)链接
一个符号链接指向另一个文件,就像 windows 中快捷方式,软链接文件和原文件本质上不是同一个文件
软链接特点
1 2 3 4 5 | 一个符号链接的内容是它引用文件的名称 可以对目录创建软链接 可以跨分区的文件实现 指向的是另一个文件的路径;其大小为指向的路径字符串的长度;不增加或减少目标文件inode的引用计数 在创建软链接时, 如果源文件使用相对路径,是相对于软链接文件的路径,而非相对于当前工作目录,但是软链接的路径如果是相对路径,则是相对于当前工作目录 |
格式
1 | ln -s filename [linkname] |
案例
1 2 3 4 5 6 7 8 9 10 11 | #绝对路径 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 [root@centos8 ~] #readlink /proc/$$/exe /usr/bin/bash |
3.4 硬链接和软链接区别总结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 1. 本质: 硬链接:本质是同一个文件 软链接:本质不是同一个文件 2. 跨设备 硬链接:不支持 软链接:支持 3. inode 硬链接:相同 软链接:不同 4. 链接数 硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少 软链接:创建或删除,链接数不会变化 5. 文件夹 硬链接:不支持 软链接:支持 6. 相对路径 硬链接:原始文件相对路径是相对于当前工作目录 软链接:原始文件的相对路径是相对于链接文件的相对路径 7. 删除源文件 硬链接:只是链接数减一,但链接文件的访问不受影响 软链接:链接文件将无法访问 8. 文件类型 硬链接:和源文件相同 软链接:链接文件,和源文件无关 9. 文件大小 硬链接: 和源文件相同 软链接: 源文件的路径的长度 |
3.5 生产案例
3.5.1 案例1:提示空间满 No space left on device,但 df 可以看到空间很多,为什么?
3.5.2 案例2:提示空间快满,使用 rm 删除了很大的无用文件后,df仍然看到空间不足,为什么?如何解决?
案例一解决办法
1 2 3 | 使用“ df -ih”命令查看 inodes 使用情况: 查看IUse是否达到100% 用“ rm -rf”清理掉那些小而多的垃圾文件(如一些 log 日志文件等) |
案例二解决办法
原因:如果程序正在被使用,删除文件后,空间不会释放,原因可能是rm的文件正在被使用,所以不能删除
第一种:删除大文件方法,在上面的案例有删除操作
1 | 大文件清零 cat /dev/null > /var/log/haug .log <br> rm -rf /var/log/haug .log |
第二种:使用lsof或者ps -ef查看是否正在被使用,如果进程不重要,可以kill杀死进程
1 | [root@host /] # lsof |grep delete[root@host /]# w #查看正在执行的程序[root@host /]# kill -9 19245 #彻底删除进程 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)