绝对路径 :路径的写法'一定由根目录/写起',如:/usr/share/doc
相对路径:路径的写法'不是由根目录/写起',指相对于目录工作目录的路径。
在所有目录底下都会存在的两个目录,分别是'.'与'..' 分别代表此层与上层目录的意思。
pwd[root@localhost data]# pwd
/data
[root@localhost data]# ls -la
总用量 2233656
drwxr-xr-x 5 root root 409611月 1014:55 .
dr-xr-xr-x. 21 root root 40964月 112025 ..
drwxr-xr-x 733 tape 40969月 172025 betterdocs
-rw-r--r--1 root root 1261549610月 272025 betterdocs.zip
-rw-------1 root root 54544435211月 1014:53 debian12.tar
-rw-------1 root root 40188467210月 272025 mariadb.10.7.tar
-rw-------1 root root 58377932810月 272025 phpmyadmin.tar
drwxr-xr-x 3 root root 40964月 152025 .temp
drwxr-xr-x 2 root root 40969月 172025 wordpress-kb
-rw-------1 root root 74349772810月 272025 wordpress.php8.1-apache.tar
cd:切换目录change dirctory
[root@localhost data]# pwd
/data
[root@localhost data]# cd ~
#[回到家目录,root用户的家目录是/root,普通用户的家目录是“/home/对应用户名”]
[root@localhost ~]# pwd [显示当前目录]
/root
[root@localhost ~]# cd - [回到刚刚的目录]
/data
[root@localhost data]# pwd
/data
[root@localhost data]# cd .. [回到上层目录]
[root@localhost /]# pwd
/
[root@localhost /]# cd /usr/local/java/ [使用绝对路径]
[root@localhost java]# pwd
/usr/local/java
[root@localhost java]# cd ../src/ [使用相对路径]
[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# pwd -P
/usr/local/src
[root@localhost src]# ln -s ../src/ src.link
[root@localhost src]# ls
src.link
[root@localhost src]# ls -l
总用量 0
lrwxrwxrwx 1 root root 74月 3011:22 src.link -> ../src/
[root@localhost src]# cd src.link/
[root@localhost src.link]# pwd
/usr/local/src/src.link
[root@localhost src.link]# pwd -P
#[如果当前目录是一个软链接文件,链接到一个目录,-P选项会显示真实的路径,而不是软链接的路径]
/usr/local/src
mkdir:创建新目录
命令常用选项:
-m 设置权限,如不配置则按umask配置的权限创建
-p 创建多级目录,如果父目录不存在,则逐级创建
man mkdir 原文
-m, --mode=MODE
set file mode (as in chmod), not a=rwx - umask
-p, --parents
no error if existing, make parent directories as needed
[root@localhost src.link]# umask
0022
[root@localhost src.link]# cd /home/zhaohong/
[root@localhost zhaohong]# mkdir test
[root@localhost zhaohong]# ls -ld test/
drwxr-xr-x 2 root root 64月 3015:01 test/
[root@localhost zhaohong]# mkdir test1/test2/test3
mkdir: 无法创建目录 “test1/test2/test3”: 没有那个文件或目录
[root@localhost zhaohong]# mkdir -p test1/test2/test3
[root@localhost zhaohong]# ls -l
drwxr-xr-x 2 root root 64月 3015:01 test
drwxr-xr-x 3 root root 194月 3015:03 test1
drwxr-xr-x. 2 zhaohong zhaohong 64月 22025 thinclient_drives
[root@localhost zhaohong]# mkdir -m 700 test2 [指定权限为700]
[root@localhost zhaohong]# ls -ld test2/
drwx------ 2 root root 64月 3015:03 test2/
[root@localhost zhaohong]#
rmdir:删除空目录
[root@localhost zhaohong]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 test test1 test2 thinclient_drives
[root@localhost zhaohong]# rmdir test2
[root@localhost zhaohong]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 test test1 thinclient_drives
[root@localhost zhaohong]# rmdir -p test1/test2/test3/
[root@localhost zhaohong]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 test thinclient_drives
[root@localhost zhaohong]# cd test/
[root@localhost test]# touch tet.txt
[root@localhost test]# cd ..
[root@localhost zhaohong]# rmdir test
rmdir: 删除 'test' 失败: 目录非空
ls 命令的绝对路径 为:/bin/ls ,但是我们可以在任何地方 执行ls命令,这就是因为环境变量PATH的帮助来实现的。PATH(一定是大写)这个变量由一堆目录所组成,每个目录中间用冒号来隔开。
切换用户,使用su - username,与su username,两种切换方式后,环境变量是不一样的,建议使用带-命令进行切换用户。
[root@localhost zhaohong]# echo $PATH
/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/java/jdk1.8.0_311/bin
[root@localhost zhaohong]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost zhaohong]# su zhaohong
[zhaohong@localhost ~]$ echo$PATH
/home/zhaohong/.local/bin:/home/zhaohong/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/java/jdk1.8.0_311/bin
[zhaohong@localhost ~]$ su- zhaohong
密码:
[zhaohong@localhost ~]$ echo$PATH
/home/zhaohong/.local/bin:/home/zhaohong/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/java/jdk1.8.0_311/bin
选项与参数:
-a:列出所有文件,包括隐藏文件(以点 . 开头的文件),常用
-A:列出所有文件,包括隐藏文件,但不包括 .(当前目录)与 ..(上级目录)这两个目录项
-d:仅列出目录本身,而不是列出目录内的文件数据,常用
-f:直接列出结果,不进行排序(ls 默认会按文件名排序)
-F:根据文件、目录等信息,附加表示其类型的符号,例如:
* 代表可执行文件
/ 代表目录
= 代表 socket 文件
| 代表 FIFO 文件
-h:将文件容量以人类较易读的格式显示(如 GB、KB 等)
-i:列出 inode 号码
-l:以长格式列出,显示文件的属性与权限等信息,常用
-n:列出 UID 与 GID,而不是用户与群组的名称
-r:将排序结果反向输出,例如原本文件名从小到大排序,反向则变为从大到小
-R:连同子目录内容一起列出,即显示该目录下的所有文件(包括子目录中的)
-S:按文件容量大小排序,而不是按文件名排序
-t:按修改时间(内容改变时间)排序,而不是按文件名
--color=never:不根据文件特性给予颜色显示
--color=always:总是显示颜色
--color=auto:让系统自行根据设置判断是否给予颜色
--full-time:以完整时间模式输出(包含年、月、日、时、分)
--time={atime,ctime}:输出 access 时间(访问时间)或改变权限属性时间(ctime),而不是内容变更时间(modification time)
命令演示示例:
[root@localhost test]# ls
test.fifo test.txt
[root@localhost test]# pwd
/home/zhaohong/test
[root@localhost test]# mkdir -p test1/test2
[root@localhost test]# touch test1/test2/test.file
[root@localhost test]# ls -a
. .. test1 test.fifo test.txt
[root@localhost test]# ls -al
总用量 4
drwxr-xr-x 3 root root 524月 3016:51 .
drwx------. 17 zhaohong zhaohong 40964月 3015:27 ..
drwxr-xr-x 3 root root 194月 3016:51 test1
prw-r--r-- 1 root root 04月 3015:32 test.fifo
-rw-r--r--1 root root 04月 3015:07 test.txt
[root@localhost test]# ls -ld test1
drwxr-xr-x 3 root root 194月 3016:51 test1
[root@localhost test]# ls -ld .
drwxr-xr-x 3 root root 524月 3016:51 .
[root@localhost test]# ls -alF
总用量 4
drwxr-xr-x 3 root root 524月 3016:51 ./
drwx------. 17 zhaohong zhaohong 40964月 3015:27 ../
drwxr-xr-x 3 root root 194月 3016:51 test1/
prw-r--r-- 1 root root 04月 3015:32 test.fifo|
-rw-r--r--1 root root 04月 3015:07 test.txt
[root@localhost test]# ls -alFn
总用量 4
drwxr-xr-x 300524月 3016:51 ./
drwx------. 171000100040964月 3015:27 ../
drwxr-xr-x 300194月 3016:51 test1/
prw-r--r-- 10004月 3015:32 test.fifo|
-rw-r--r--10004月 3015:07 test.txt
[root@localhost test]# ls -alFnR
.:
总用量 4
drwxr-xr-x 300524月 3016:51 ./
drwx------. 171000100040964月 3015:27 ../
drwxr-xr-x 300194月 3016:51 test1/
prw-r--r-- 10004月 3015:32 test.fifo|
-rw-r--r--10004月 3015:07 test.txt
./test1:
总用量 0
drwxr-xr-x 300194月 3016:51 ./
drwxr-xr-x 300524月 3016:51 ../
drwxr-xr-x 200234月 3016:51 test2/
./test1/test2:
总用量 0
drwxr-xr-x 200234月 3016:51 ./
drwxr-xr-x 300194月 3016:51 ../
-rw-r--r--10004月 3016:51 test.file
[root@localhost test]# ls -alFn --full-time
总用量 4
drwxr-xr-x 300522026-04-3016:51:10.298511042 +0800 ./
drwx------. 171000100040962026-04-3015:27:22.569328686 +0800 ../
drwxr-xr-x 300192026-04-3016:51:10.298511042 +0800 test1/
prw-r--r-- 10002026-04-3015:32:58.666572770 +0800 test.fifo|
-rw-r--r--10002026-04-3015:07:22.933812166 +0800 test.txt
[root@localhost test]# ls -alFn --full-time --time=atime
总用量 4
drwxr-xr-x 300522026-04-3016:51:15.374318398 +0800 ./
drwx------. 171000100040962026-04-3015:27:31.613985414 +0800 ../
drwxr-xr-x 300192026-04-3016:51:17.752228149 +0800 test1/
prw-r--r-- 10002026-04-3015:32:58.666572770 +0800 test.fifo|
-rw-r--r--10002026-04-3015:44:24.110558083 +0800 test.txt
[root@localhost test]# ls -alFn --full-time --time=ctime
总用量 4
drwxr-xr-x 300522026-04-3016:51:10.298511042 +0800 ./
drwx------. 171000100040962026-04-3015:27:22.569328686 +0800 ../
drwxr-xr-x 300192026-04-3016:51:10.298511042 +0800 test1/
prw-r--r-- 10002026-04-3015:32:58.666572770 +0800 test.fifo|
-rw-r--r--10002026-04-3015:39:34.922533647 +0800 test.txt
[root@localhost test]# ls -alFnt --full-time
总用量 4
drwxr-xr-x 300522026-04-3016:51:10.298511042 +0800 ./
drwxr-xr-x 300192026-04-3016:51:10.298511042 +0800 test1/
prw-r--r-- 10002026-04-3015:32:58.666572770 +0800 test.fifo|
drwx------. 171000100040962026-04-3015:27:22.569328686 +0800 ../
-rw-r--r--10002026-04-3015:07:22.933812166 +0800 test.txts
| 缩写 | 中文 | 英文 | 记录什么 | 会影响对应时间的命令 |
|---|---|---|---|---|
| atime | 访问时间 | Access time | 最后一次读取文件 | cat, less, head, tail, more,cp source dest,vim -R file,./script.sh |
| mtime | 内容修改时间 | Modify time | 文件内容被修改 | echo "abc" >> file # 写入vim file # 修改内容cp new file # 覆盖写truncate -s 0 file # 清空文件 |
| ctime | 属性变更时间 | Change time | inode 元数据变化 | chmod 777 file # 改权限chown user file # 改属主ln file link # 新增硬链接rm link # 删除硬链接mv file newname # 改文件名echo hi >> file # 修改内容(inode大小变化) |
| crtime / btime | 创建时间 | Creation/Birth time | 文件创建时间 | 不能修改,创建时固定 |
四个时间关系图
创建文件
↓
crtime = ctime = mtime = atime
读取文件
↓
atime 更新
修改内容
↓
mtime + ctime 更新
修改权限/重命名
↓
ctime 更新
查看文件的四个时间命令:stat
[root@localhost test]# stat test.txt
文件:test.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963786 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-04-30 15:44:24.110558083 +0800#[access time]
最近更改:2026-04-30 15:07:22.933812166 +0800#[modify time内容变更]
最近改动:2026-04-30 15:39:34.922533647 +0800#[ctime改变权限属性时间]
创建时间:2026-04-30 15:07:22.933812166 +0800#[创建时间]
复制文件(或目录),本质上就是创建新的文件或目录,因此,在执行cp命令前,需要确保操作用户对要拷贝的文件(或目录)所在的目录拥有写权限,对要拷贝的文件(或目录)有读权限。拷贝后的文件权限由操作用户决定,与原文件没有关系,即使使用了-a选项,除非是使用root用户进行操作。
[zhaohong@localhost test]$ ll
总用量 0
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt
[zhaohong@localhost test]$ ls-ld .
drwxr-xr-x 2 zhaohong zhaohong 215月 615:10 .
[zhaohong@localhost test]$ chmod-w .
[zhaohong@localhost test]$ ls-ld . #[取消test目录的写权限]
dr-xr-xr-x 2 zhaohong zhaohong 215月 615:10 .
[zhaohong@localhost test]$ cp-av123.txt 123.txt.bak #[拷贝时报权限不足]
'123.txt'-> '123.txt.bak'
cp: 无法创建普通文件'123.txt.bak': 权限不够
[zhaohong@localhost test]$ chmod u+w . #[增加test目录的写权限]
[zhaohong@localhost test]$ ls-ld .
drwxr-xr-x 2 zhaohong zhaohong 215月 615:10 .
[zhaohong@localhost test]$ cp-av123.txt 123.txt.bak #[拷贝时不报权限不足]
'123.txt'-> '123.txt.bak'
[zhaohong@localhost test]$ ll
总用量 0
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt.bak
[root@localhost test]# ll
总用量 0
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt.bak
[root@localhost test]# ls -ld .
drwxr-xr-x 2 zhaohong zhaohong 405月 615:11 .
[root@localhost test]# chmod o+w .
[root@localhost test]# ls -ld .
drwxr-xrwx 2 zhaohong zhaohong 405月 615:11 .
[root@localhost test]# su test
[test@localhost test]$ cp-av123.txt 123.txt.testuser.bak #[使用test用户拷贝所有者为zhaohong的文件]
'123.txt'-> '123.txt.testuser.bak'
[test@localhost test]$ ll
总用量 0
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt
-rw-r--r--1 zhaohong zhaohong 05月 615:10 123.txt.bak
-rw-r--r--1 test test 05月 615:10 123.txt.testuser.bak #[拷贝后的文件所有者和属组是操作用户test]
命令格式:
[root@study ~]# cp [-adfilprsu] 源文件或目录(source) 目标文件或目录(destination)| 选项 | 全称 | 作用 | 典型场景 |
|---|---|---|---|
-r | recursive | 递归复制目录 | 复制目录 |
-R | recursive | 同 -r,更规范 | 复制目录 |
-a同-dpr | archive | 原样复制(最重要)递归复制、保留权限、保留软链接、保留时间、保留属主 | 备份目录 |
-p | preserve | 保留权限/时间/属主 | 保持文件属性 |
-f | force | 强制覆盖 | 自动化脚本 |
-i | interactive | 覆盖前询问 | 手动操作防误删 |
-n | no-clobber | 不覆盖已有文件 | 防止误覆盖 |
-v | verbose | 显示复制过程 | 排错/查看进度 |
-d | no-dereference | 复制符号链接本身,不复制链接指向的文件,没有-d时,复制的是链接指向的文件 | 复制软链接 |
-L | dereference | 复制链接指向文件 | 跟随软链接 |
-x | one-file-system | 不跨文件系统 | 备份系统 |
-l | link | 创建硬链接 | 去重备份 |
-s | symbolic-link | 创建符号链接 | 创建软链接 |
-t DIR | target-dir | 指定目标目录,复制多个文件到指定目录cp -t /backup file1 file2 file3 | 批量复制 |
高频组合命令(最实用)
| 命令 | 含义 |
|---|---|
cp file1 file2 | 复制文件 |
cp -r dir1 dir2 | 复制目录 |
cp -a src dst | 完整备份复制 |
cp -av src dst | 备份并显示过程 |
cp -ax / /backup | 备份系统根目录 |
cp -i file /tmp | 覆盖前询问 |
cp -n file /tmp | 不覆盖已有文件 |
[root@localhost study]# touch 123.txt [准备一个文件123.txt]
[root@localhost study]# echo 123 > 123.txt [123.txt文件中写入内容123]
[root@localhost study]# cat 123.txt [查看文件内容]
123
[root@localhost study]# ls -ld 123.txt --full-time [查看文件属性]
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt
[root@localhost study]# mkdir test [准备一个目录]
[root@localhost study]# ls
123.txt test
[root@localhost study]# cd test/ [进入目录test]
[root@localhost test]# touch 234.txt [test目录中有一个234.txt的文件]
[root@localhost test]# echo 234 > 234.txt
[root@localhost test]# cat 234.txt
234
[root@localhost test]# ls -ld 234.txt --full-time
-rw-r--r--1 root root 42026-05-0611:27:57.830628917 +0800234.txt
[root@localhost test]# cd ..
[root@localhost study]# ls -l --full-time
总用量 4
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt
drwxr-xr-x 2 root root 212026-05-0611:27:50.149954624 +0800 test
[root@localhost study]# cp -a 123.txt 123.txt.bak [拷贝文件至文件,前提是123.txt.bak这个文件不存在]
[root@localhost study]# ls -l --full-time
总用量 8
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt.bak
drwxr-xr-x 2 root root 212026-05-0611:27:50.149954624 +0800 test
[root@localhost study]# cat 123.txt.bak
123
[root@localhost study]# cp -a test/ test.bak [拷贝test目录为test.bak,前提是test.bak不存在]
[root@localhost study]# ls -l --full-time
总用量 8
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt.bak
drwxr-xr-x 2 root root 212026-05-0611:27:50.149954624 +0800 test
drwxr-xr-x 2 root root 212026-05-0611:27:50.149954624 +0800 test.bak
[root@localhost study]# cp -a 123.txt test [如果test是一个存在的目录,拷贝文件至目录。如果test文件不存在,创建一个名为test的文件,内容与属性与123.txt一样。如果test是一个存在的文件,则会提示是否覆盖test的内容为123.txt]
[root@localhost study]# ls -l test --full-time
总用量 8
-rw-r--r--1 root root 42026-05-0611:26:03.006498115 +0800123.txt
-rw-r--r--1 root root 42026-05-0611:27:57.830628917 +0800234.txt
[root@localhost study]# cp -a test test.bak/ [拷贝目录至另一个目录内,前提是test.bak这个目录已经存在]
[root@localhost study]# ls -l test.bak/ --full-time
总用量 4
-rw-r--r--1 root root 42026-05-0611:27:57.830628917 +0800234.txt
drwxr-xr-x 2 root root 362026-05-0611:30:17.381814771 +0800 test
[root@localhost study]# ls -il [查看文件的inode]
总用量 16
52963784-rw-r--r--1 root root 45月 611:26 123.txt
52963786-rw-r--r--1 root root 45月 611:26 123.txt.bak
2538405 drwxr-xr-x 2 root root 365月 611:30 test
52963787-rw-r--r--1 root root 45月 611:26 test10
52365131-rw-r--r--1 root root 45月 611:26 test11
22176856 drwxr-xr-x 3 root root 335月 611:30 test.bak
[root@localhost study]# cp -l 123.txt 123.txt.hardlink [创建硬链接文件]
[root@localhost study]# ls -il
总用量 20
52963784-rw-r--r--2 root root 45月 611:26 123.txt #与123.txt.hardlink硬链接文件的inode号一样
52963786-rw-r--r--1 root root 45月 611:26 123.txt.bak
52963784-rw-r--r--2 root root 45月 611:26 123.txt.hardlink
2538405 drwxr-xr-x 2 root root 365月 611:30 test
52963787-rw-r--r--1 root root 45月 611:26 test10
52365131-rw-r--r--1 root root 45月 611:26 test11
22176856 drwxr-xr-x 3 root root 335月 611:30 test.bak
[root@localhost study]# cp -s 123.txt 123.txt.softlink [创建软链接文件]
[root@localhost study]# ls -il
总用量 20
52963784-rw-r--r--2 root root 45月 611:26 123.txt
52963786-rw-r--r--1 root root 45月 611:26 123.txt.bak
52963784-rw-r--r--2 root root 45月 611:26 123.txt.hardlink
53421985 lrwxrwxrwx 1 root root 75月 611:51 123.txt.softlink -> 123.txt #软链接文件指向实际的文件
2538405 drwxr-xr-x 2 root root 365月 611:30 test
52963787-rw-r--r--1 root root 45月 611:26 test10
52365131-rw-r--r--1 root root 45月 611:26 test11
22176856 drwxr-xr-x 3 root root 335月 611:30 test.bak
[root@localhost study]# ls -al test [目录下有一个隐藏文件.config]
总用量 8
drwxr-xr-x 2 root root 515月 612:00 .
drwxr-xr-x 4 root root 1445月 612:02 ..
-rw-r--r--1 root root 45月 611:26 123.txt
-rw-r--r--1 root root 45月 611:27 234.txt
-rw-r--r--1 root root 05月 612:00 .config
[root@localhost study]# mkdir testdir
[root@localhost study]# mkdir testdir1
[root@localhost study]# cp -av test/. testdir [复制目录中的文件至另一目录,包含隐藏文件,为什么.可以代表目录中的所有内容,因为.表示当前目录对象]
'test/./.config'-> 'testdir/./.config'
'test/./234.txt'-> 'testdir/./234.txt'
'test/./123.txt'-> 'testdir/./123.txt'
[root@localhost study]# cp -av test/* testdir1 [复制目录中的文件至另一目录,不包含隐藏文件]
'test/123.txt'-> 'testdir1/123.txt'
'test/234.txt'-> 'testdir1/234.txt'
删除文件或目录,语法:
rm [选项] 文件/目录| 选项 | 全称 | 作用 | 说明 |
|---|---|---|---|
-r | recursive | 递归删除目录 | 删除目录必备 |
-f | force | 强制删除 | 不提示、不报错 |
-i | interactive | 删除前询问 | 防误删 |
-v | verbose | 显示删除过程 | 排错/确认 |
[root@localhost test]# ls
123.txt 123.txt.bak 123.txt.testuser.bak
[root@localhost test]# rm -i 123.txt
rm:是否删除普通空文件 '123.txt'?y
[root@localhost test]# ls
123.txt.bak 123.txt.testuser.bak
[root@localhost test]# rm -iv 123.txt.bak
rm:是否删除普通空文件 '123.txt.bak'?y
已删除 '123.txt.bak'
[root@localhost test]# cd ..
[root@localhost zhaohong]# rm -rf test/
[root@localhost zhaohong]# ls
公共 模板 视频 图片 文档 下载 音乐 桌面 study thinclient_drives
[root@localhost zhaohong]# ls -l test/
总用量 0
-rw-r--r--1 root root 05月 615:29 123.txt
[root@localhost zhaohong]# rm -rfv test/
已删除 'test/123.txt'
已删除目录 'test/'
mv = move 用于:
移动文件/目录
重命名文件/目录
mv file1 file2 # 重命名
mv file dir/ # 移动
| 选项 | 全称 | 作用 | 说明 |
|---|---|---|---|
-i | interactive | 覆盖前询问 | 防误覆盖 |
-f | force | 强制覆盖 | 不提示 |
-n | no-clobber | 不覆盖已有文件 | 安全移动 |
-v | verbose | 显示移动过程 | 排错/确认 |
-u | update | 只移动较新的文件 | 增量移动 |
-b | backup | 覆盖前创建备份 | 自动备份 |
[root@localhost zhaohong]# ls -ld 123.txt
-rw-r--r--1 root root 05月 615:34 123.txt
[root@localhost zhaohong]# ls -l study/123.txt --full-time [这个文件更新]
-rw-r--r--1 root root 42026-05-0615:36:06.702341782 +0800 study/123.txt
[root@localhost zhaohong]# ls -ld 123.txt --full-time [这个文件较旧]
-rw-r--r--1 root root 02026-05-0615:34:57.647695091 +0800123.txt
[root@localhost zhaohong]# mv -u 123.txt study/ [-u移动时,由于移动的文件较旧,不移动]
[root@localhost zhaohong]# ls -l study/123.txt --full-time
-rw-r--r--1 root root 42026-05-0615:36:06.702341782 +0800 study/123.txt
[root@localhost zhaohong]# mv -b 123.txt study/ [覆盖前自动备份]
mv:是否覆盖'study/123.txt'? y
[root@localhost zhaohong]# ls study/ [自动备份的文件名在原文件名后加~]
123.txt 123.txt~
[root@localhost zhaohong]# mv study/123.txt study/234.txt [改名]
[root@localhost zhaohong]# ls study/
123.txt~ 234.txt
命令:cat 文件名
-A :相当于 -vET 的整合选项,可列出一些特殊字符而不是空白而已;
-b :列出行号,仅针对非空白行做行号显示,空白行不标行号!
-E :将结尾的换行字符 $ 显示出来;
-n :打印出行号,连同空白行也会有行号,与 -b 选项不同;
-T :将 [tab] 键以 ^I 显示出来;
-v :列出一些看不出来的特殊字符
[root@localhost zhaohong]# cat /etc/issue
\S
Kernel \r on an \m
[root@localhost zhaohong]# cat -b /etc/issue
1 \S
2 Kernel \r on an \m
[root@localhost zhaohong]# cat -A /etc/issue
\S$
Kernel \r on an \m$
$
命令:less 选项 文件名
空格键 :向下翻动一页;
[pagedown]:向下翻动一页;
[pageup] :向上翻动一页;
/字符串 :向下搜索『字串』的功能;
?字符串 :向上搜索『字串』的功能;
n :重复前一个搜索 (与 / 或 ? 有关! )
N :反向的重复前一个搜索 ( 与 / 或 ? 有关! )
g :前进到这个数据的第一行去;
G :前进到这个数据的最后一行去 (注意大小写);
q :离开 less 这个程序;
| 选项 | 作用 | 使用频率 | 说明 |
|---|---|---|---|
-N | 显示行号 | 查看配置文件必开 | |
-S | 长行不换行 | 查看日志/代码 | |
-i | 搜索忽略大小写 | 配合 / 搜索 | |
-I | 强制忽略大小写 | 全忽略 | |
-F | 内容一屏显示则自动退出 | 适合小文件 | |
-R | 显示颜色 | 查看带颜色日志 | |
-c | 整屏刷新 | 减少闪烁 | |
-p pattern | 打开时定位到关键字 | 快速定位 | |
+行号 | 打开到指定行 | 定位报错行 | |
+G | 打开到文件末尾 | 查看最新日志 |
less 使用的正则类型
less 使用 POSIX 正则(基础正则)
支持:
. 任意字符
* 重复
^ 行首
$ 行尾
[] 字符集合
\{n,m\} 次数
| 或(需转义)
最常用的正则搜索
同时搜索多个关键字
查 error 或 warn:
/error|warn查 fail 或 denied:
/fail|denied搜索整行匹配
行首匹配 ^
查以 ERROR 开头:
/^ERROR查以时间开头:
/^[0-9]行尾匹配 $
查以 .conf 结尾:
/\.conf$匹配数字
查 HTTP 状态码:
/[0-9][0-9][0-9]查 500 错误:
/ 500 查 4xx 或 5xx:
/ [45][0-9][0-9]匹配IP地址
/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)只显示匹配行
在 less 里输入:
&关键字例:
&error效果:只显示包含 error 的行, 其他行全部隐藏!
head 常用选项表
| 选项 | 作用 | 示例 |
|---|---|---|
-n 行数 | 指定显示行数 | head -n 20 file |
-c 字节数 | 按字节显示 | head -c 100 file |
-q | 不显示文件名 | 多文件时 |
-v | 强制显示文件名 | 多文件时 |
查看前20行
head -n 20 log.txt同时查看多个文件
head file1 file2[root@localhost zhaohong]# cp -av /var/log/messages /var/log/messages.bak
'/var/log/messages'-> '/var/log/messages.bak'
[root@localhost zhaohong]# cd /var/log/
[root@localhost log]# head -v messages messages.bak
==> messages <==
192.168.1.1
Apr 3009:33:15 localhost kernel: The list of certified hardware and cloud instances for Red Hat Enterprise Linux 9 can be viewed at the Red Hat Ecosystem Catalog, https://catalog.redhat.com.
Apr 3009:33:15 localhost kernel: Command line: BOOT_IMAGE=(hd0,msdos1)/vmlinuz-5.14.0-432.el9.x86_64 root=/dev/mapper/cs-root ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap rhgb quiet
Apr 3009:33:15 localhost kernel: [Firmware Bug]: TSC doesn't count with P0 frequency!
Apr 3009:33:15 localhost kernel: x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
Apr 3009:33:15 localhost kernel: x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
Apr 3009:33:15 localhost kernel: x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
Apr 3009:33:15 localhost kernel: x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
Apr 3009:33:15 localhost kernel: x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'compacted' format.
Apr 3009:33:15 localhost kernel: signal: max sigframe size: 1776
==> messages.bak <==
192.168.1.1
Apr 3009:33:15 localhost kernel: The list of certified hardware and cloud instances for Red Hat Enterprise Linux 9 can be viewed at the Red Hat Ecosystem Catalog, https://catalog.redhat.com.
Apr 3009:33:15 localhost kernel: Command line: BOOT_IMAGE=(hd0,msdos1)/vmlinuz-5.14.0-432.el9.x86_64 root=/dev/mapper/cs-root ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap rhgb quiet
Apr 3009:33:15 localhost kernel: [Firmware Bug]: TSC doesn't count with P0 frequency!
Apr 3009:33:15 localhost kernel: x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
Apr 3009:33:15 localhost kernel: x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
Apr 3009:33:15 localhost kernel: x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
Apr 3009:33:15 localhost kernel: x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
Apr 3009:33:15 localhost kernel: x86/fpu: Enabled xstate features 0x7, context size is 832 bytes, using 'compacted' format.
Apr 3009:33:15 localhost kernel: signal: max sigframe size: 1776
tail 常用选项表
| 选项 | 作用 | 说明 |
|---|---|---|
-n 行数 | 显示最后 N 行 | 最常用 |
-f | 实时跟踪文件 | 日志神器 |
-F | 文件轮转也跟踪 | 日志轮转必用 |
-c 字节数 | 按字节显示 | |
--pid=PID | 进程结束停止 | 脚本用 |
tail 最重要功能:实时看日志
tail -f /var/log/messages查看最后50行
tail -n 50 log.txt实时查看最后100行
tail -n 100 -f log.txt最常用:
tail -f -n 100 log.txttouch 用于:
创建空文件
修改文件时间戳
基本语法:
touch [选项] 文件默认修改文件的atime + mtime,由于atime + mtime也是元数据的一部分,所以ctime会改变。
| 选项 | 作用 | 说明 | 命令示例 |
|---|---|---|---|
| 无选项 | 创建空文件 | 文件不存在就创建,文件存在则是修改文件是atime + mtime | 创建单个文件:touch file.txt创建多个文件:touch file1 file2 file3批量创建文件:touch log{1..5}.txt 生成log1.txt log2.txt log3.txt log4.txt log5.txt |
-a | 只修改访问时间(atime) | touch -a file.txt | |
-m | 只修改修改时间(mtime) | 内容修改时间 | touch -m file.txt |
-t | 指定时间戳 | 最常用 | 格式:[[CC]YY]MMDDhhmm[.ss]示例:touch -t 202501011200 file.txt 表示修改atime + mtime为2025-01-01 12:00 |
-d | 使用日期字符串 | 更直观 | touch -d "2025-01-01 12:00" file.txttouch -d "yesterday" file.txttouch -d "2 days ago" file.txttouch -d "next monday" file.txt |
-c | 不创建文件 | 如果文件不存在,不要自动创建,如果文件存在,仍然会修改时间。 | touch -c file.txt |
-r | 参考文件时间 | 时间复制 | touch -r file1 file2 让 file2 时间 = file1注意 atime/mtime 是一个合理的时间(不早于它的 birth time 且不晚于当前时间) |
[root@localhost study]# touch file.txt [文件不存在,创建文件,时间为当前时间]
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:18:22.099636896 +0800
最近更改:2026-05-07 10:18:22.099636896 +0800
最近改动:2026-05-07 10:18:22.099636896 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# touch file.txt
#[文件存在,修改时间atime+mtime,由于atime和mtime也是元数据的一分部,所以ctime也改变了]#
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:19:04.584804159 +0800
最近更改:2026-05-07 10:19:04.584804159 +0800
最近改动:2026-05-07 10:19:04.584804159 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# touch -a file.txt [只修改atime,同时也会修改ctime]
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:24:17.498192723 +0800
最近更改:2026-05-07 10:19:04.584804159 +0800
最近改动:2026-05-07 10:24:17.498192723 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# touch -m file.txt [只修改mtime,同时也会修改ctime]
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:24:17.498192723 +0800
最近更改:2026-05-07 10:25:01.387652382 +0800
最近改动:2026-05-07 10:25:01.387652382 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# touch -t 202601011200 file.txt [指定时间atime+mtime,ctime会变成当前时间]
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-01-01 12:00:00.000000000 +0800
最近更改:2026-01-01 12:00:00.000000000 +0800
最近改动:2026-05-07 10:26:05.818391131 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# touch -d "2025-02-02 13:00" file.txt [指定时间atime+mtime,ctime会变成当前时间]
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2025-02-02 13:00:00.000000000 +0800
最近更改:2025-02-02 13:00:00.000000000 +0800
最近改动:2026-05-07 10:27:22.016716886 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# stat file.txt
文件:file.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963781 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:39:44.942243522 +0800
最近更改:2026-05-07 10:39:44.942243522 +0800
最近改动:2026-05-07 10:39:44.942243522 +0800
创建时间:2026-05-07 10:18:22.099636896 +0800
[root@localhost study]# stat 234.txt
文件:234.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963786 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:28:29.148360843 +0800
最近更改:2026-05-07 10:28:29.148360843 +0800
最近改动:2026-05-07 10:28:51.250585149 +0800
创建时间:2026-05-06 15:34:57.647695091 +0800
[root@localhost study]# touch -r file.txt 234.txt [修改234.txt文件的时间为file.txt的时间,如果file.txt的时间被手动修改为比创建时间更早的时间,touch -r命令不生效]
[root@localhost study]# stat 234.txt
文件:234.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:52963786 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
最近访问:2026-05-07 10:39:44.942243522 +0800
最近更改:2026-05-07 10:39:44.942243522 +0800
最近改动:2026-05-07 10:40:32.477190826 +0800
创建时间:2026-05-06 15:34:57.647695091 +0800
umask = 创建文件时“去掉哪些权限”
文件默认最大权限666, 为什么不是777,因为文件默认不能有执行权限。如果默认有执行权限,任何新文件能直接执行,对于系统来说不安全。Linux的设计理念,是默认最小权限。
目录默认最大权限777, 因为目录需要x才能进入。
文件:666 - umask
目录:777 - umask
[root@localhost study]# umask
0022
[root@localhost study]# touch 123.txt
[root@localhost study]# ls -ld 123.txt
-rw-r--r--1 root root 05月 715:07 123.txt
[root@localhost study]# mkdir testdir
[root@localhost study]# ls -ld testdir/
drwxr-xr-x 2 root root 65月 715:07 testdir/
[root@localhost study]#
Linux 权限体系全景
| 类型 | 示例 | 解释 | 示例 | 应用场景 |
|---|---|---|---|---|
| 普通权限 | rwx | |||
| 特殊权限 | SUID / SGID / Sticky | SUID,显示为rws,普通用户执行程序时,临时拥有文件owner权限。SGID,目录中新文件,自动继承目录所属组。对于文件基本不用。Sticky,目录中只能删除自己的文件,root不受控制。 | SUID配置:chmod u+s file.txt SGID配置:chmod g+s dir(取消把+换成-,无x权限,显示为大写S,有x权限显示为小写s)sticky配置:chmod +t dir(或者chmod 1xxx dir)取消把+换成-,无x权限,显示为大写T,有x权限显示为小写t | SUID应用:/usr/bin/passwd 普通用户可修改自己的密码SGID应用:团队协作常用。Sticky应用:/tmp目录下的临时文件,只能删除自己的文件 |
| ACL权限 | setfacl | 普通rwx不够细时,单独给某个用户权限 | 配置:setfacl -m u:user1:rwx test.file取消:setfacl -m u:user1:--- test.file | 普通rwx不够细时,单独给某个用户权限 |
| 文件属性(隐藏权限) | chattr | i属性,设置后,文件变成只读锁定状态,不允许修改内容、删除、重命名、修改权限。a属性,追加模式,设置后,可以追加内容,不能删除和覆盖修改。 | 设置:chattr +i/a xxxx查看:lsattr xxxx取消:chattr -i/a xxxx | i属性:防止误删日志文件,只能追加,防止清空日志 |
SUID配置命令演示:
-----------------------------------------------------------------------------------------------------------
[root@localhost study]# ls -l
总用量 0
-rw-r--r--1 root root 05月 1416:00 test.file
[root@localhost study]# chmod u+s test.file [添加SUID权限,由于没有x权限,显示为大写的S]
[root@localhost study]# ls -l
总用量 0
-rwSr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# chmod u+x test.file [添加SUID权限,添加x权限,显示为小写的s]
[root@localhost study]# ls -l
总用量 0
-rwsr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# chmod u-s test.file [取消s权限]
[root@localhost study]# ls -l
总用量 0
-rwxr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# ls -ld /usr/bin/passwd
-rwsr-xr-x. 1 root root 326488月 102021 /usr/bin/passwd
-----------------------------------------------------------------------------------------------------------
SGID配置命令演示:
-----------------------------------------------------------------------------------------------------------
[root@localhost study]# su zhangsan [切换用户至zhangsan]
[zhangsan@localhost study]$ mkdir testdir #[创建目录,目录的属组也是zhangsan]
[zhangsan@localhost study]$ ls-l
总用量 0
drwxr-xr-x 2 zhangsan zhangsan 65月 1416:27 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
[zhangsan@localhost testdir]$ exit
exit
[root@localhost study]# ll
总用量 0
drwxr-xr-x 2 zhangsan zhangsan 65月 1416:27 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# chmod o+w testdir/ [为目录其他人添加w权限,不角其他用户无法在该目录下创建文件]
[root@localhost study]# ll
总用量 0
drwxr-xrwx 2 zhangsan zhangsan 65月 1416:27 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# su lisi
[lisi@localhost study]$ ll
总用量 0
drwxr-xrwx 2 zhangsan zhangsan 65月 1416:27 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
[lisi@localhost study]$ cd testdir/
[lisi@localhost testdir]$ ll
总用量 0
[lisi@localhost testdir]$ touch test.file #[lisi创建的文件所有者和属组都是lisi]
[lisi@localhost testdir]$ ll
总用量 0
-rw-r--r--1 lisi lisi 05月 1416:32 test.file
[lisi@localhost testdir]$ exit
exit
[root@localhost study]# ls -ld testdir/
drwxr-xrwx 2 zhangsan zhangsan 235月 1416:32 testdir/
[root@localhost study]# chmod g+s testdir/ [testdir目录增加SGID权限,配置后drwxr-srwx]
[root@localhost study]# ls -ld testdir/
drwxr-srwx 2 zhangsan zhangsan 235月 1416:32 testdir/
[root@localhost study]# su lisi
[lisi@localhost study]$ ls-l
总用量 0
drwxr-srwx 2 zhangsan zhangsan 235月 1416:32 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
[lisi@localhost study]$ cd testdir/
[lisi@localhost testdir]$ ls-l
总用量 0
-rw-r--r--1 lisi lisi 05月 1416:32 test.file
[lisi@localhost testdir]$ touch test.file1 #[目录配置SGID权限后,lisi用户创建的文件属组不是lisi,而是zhangsan]
[lisi@localhost testdir]$ ls-l
总用量 0
-rw-r--r--1 lisi lisi 05月 1416:32 test.file
-rw-r--r--1 lisi zhangsan 05月 1416:35 test.file1
[lisi@localhost testdir]$ exit
exit
[root@localhost study]# ll
总用量 0
drwxr-srwx 2 zhangsan zhangsan 415月 1416:35 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# chmod g-s testdir/ [取消SGID权限]
[root@localhost study]# ll
总用量 0
drwxr-xrwx 2 zhangsan zhangsan 415月 1416:35 testdir
-rwxr--r--1 root root 05月 1416:00 test.file
-----------------------------------------------------------------------------------------------------------
Sticky配置命令演示:
-----------------------------------------------------------------------------------------------------------
[root@localhost study]# ls -ld /tmp
drwxrwxrwt. 19 root root 40965月 1416:22 /tmp
[lisi@localhost tmp]$ ls-ld zhangsan.file
-rwxrwxrwx1 zhangsan zhangsan 05月 1416:40 zhangsan.file
[lisi@localhost tmp]$ rm-rf zhangsan.file
rm: 无法删除 'zhangsan.file': 不允许的操作
-----------------------------------------------------------------------------------------------------------
setfacl配置命令演示:
-----------------------------------------------------------------------------------------------------------
[root@localhost study]# touch test.file [创建文件]
[root@localhost study]# ll
总用量 0
-rw-r--r--1 root root 05月 1416:00 test.file
[root@localhost study]# getfacl test.file [查看test.file这个文件的acl权限,这时跟ls -l看到的结果一样]
# file: test.file
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@localhost study]# useradd user1 [创建用户user1]
[root@localhost study]# setfacl -m u:user1:rwx test.file
#[配置权限,对象是test.file这个文件,用户是user1,权限是rwx]
[root@localhost study]# getfacl test.file [查看acl权限,对比没配置之前,多了`user:user1:rwx`]
# file: test.file
# owner: root
# group: root
user::rw-
user:user1:rwx
group::r--
mask::rwx
other::r--
[root@localhost study]# setfacl -m u:user1:--- test.file [配置user1对test.file的权限为---,也就是无权限]
[root@localhost study]# getfacl test.file
# file: test.file
# owner: root
# group: root
user::rw-
user:user1:---
group::r--
mask::r--
other::r--
[root@localhost study]# setfacl -x u:user1 test.file [删除 user1 的所有权限(恢复为无任何特殊权限)]
[root@localhost study]# getfacl test.file
# file: test.file
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
[root@localhost study]# ls -l
总用量 0
-rw-r--r--+1 root root 05月 1416:00 test.file #[最后的+号,表示:标准权限 + ACL]
[root@localhost study]# setfacl -b test.file [去掉+号]
[root@localhost study]# ls -l
总用量 0
-rw-r--r--1 root root 05月 1416:00 test.file
-----------------------------------------------------------------------------------------------------------
chattr命令演示:
-----------------------------------------------------------------------------------------------------------
[root@localhost study]# ls -l
总用量 0
-rwxr--r--1 root root 05月 1416:00 test.file
[root@localhost study]# lsattr test.file
---------------------- test.file
[root@localhost study]# chattr +i test.file [添加i属性,后不可删除,不可修改权限、属组、所有者]
[root@localhost study]# lsattr test.file
----i----------------- test.file
[root@localhost study]# rm -rf test.file
rm: 无法删除 'test.file': 不允许的操作
[root@localhost study]# chmod 777 test.file
chmod: 正在更改 'test.file' 的权限: 不允许的操作
[root@localhost study]# chown zhangsan test.file
chown: 正在更改'test.file' 的所有者: 不允许的操作
[root@localhost study]# chgrp zhangsan test.file
chgrp: 正在更改'test.file' 的所属组: 不允许的操作
[root@localhost study]# echo 123 > test.file
-bash: test.file: 不允许的操作
[root@localhost study]# chattr -i test.file [取消i属性]
[root@localhost study]# lsattr test.file
---------------------- test.file
[root@localhost study]# lsattr test.file
---------------------- test.file
[root@localhost study]# chattr +a test.file [添加a属性,与i属性差不多,只是多了追加写]
[root@localhost study]# lsattr test.file
-----a---------------- test.file
[root@localhost study]# rm -rf test.file
rm: 无法删除 'test.file': 不允许的操作
[root@localhost study]# echo 123 > test.file
-bash: test.file: 不允许的操作
[root@localhost study]# echo 123 >> test.file
[root@localhost study]# cat test.file
123
which ——在PATH里查找可执行文件,-a选项:显示所有匹配路径
whereis ——查的更全面,含源码、手册
| 命令 | 作用 |
|---|---|
whereis -b ls | 只找二进制 |
whereis -m ls | 只找 man 手册 |
whereis -s ls | 只找源码 |
type——判断命令的“真实身份”,很多命令不是程序,而是:
alias别名
shell内建命令
函数
用type才能看出来。
command -v —— 最标准脚本用法
写 shell 脚本时,官方推荐用它判断命令是否存在。
command -v docker如果存在 → 输出路径如果不存在 → 不输出
以上命令演示:
[root@localhost ~]# which -a ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# which python
/usr/bin/python
[root@localhost ~]# which -a python
/usr/bin/python
[root@localhost ~]# man which
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost ~]# type cd
cd 是 shell 内建
[root@localhost ~]# type ls
ls 是“ls --color=auto”的别名
[root@localhost ~]# type ifconfig
ifconfig 是 /usr/sbin/ifconfig
[root@localhost ~]# command -v docker
/usr/bin/docker
locate工作原理,locate不是实时搜索磁盘,它搜索的是系统里的一个数据库:/var/lib/mlocate/mlocate.db,这个数据库由updatedb 命令生成。locate比find快很多倍。
基本用法:locate 文件名
| 常用选项 | 选项有什么用 |
|---|---|
| -i | 忽略大小写 |
| -N | 显示N条结果 |
| -c | 显示结果数量 |
| -b | 精确匹配(完整路径匹配),默认是模糊匹配。 |
| -r | 使用正则表达式 |
[root@localhost study]# updatedb
[root@localhost study]# locate nginx.conf
/opt/test/test1/nginx.conf
/root/soft/nginx-1.26.2/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.bak
/usr/local/nginx/conf/nginx.conf.default
[root@localhost study]# locate -i NGINX.CONF
/opt/test/test1/nginx.conf
/root/soft/nginx-1.26.2/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.bak
/usr/local/nginx/conf/nginx.conf.default
[root@localhost study]# locate -i -n 3NGINX.CONF
locate: 3NGINX.CONF 有无效的值 `limit'
[root@localhost study]# locate -i -n 3 NGINX.CONF
/opt/test/test1/nginx.conf
/root/soft/nginx-1.26.2/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
[root@localhost study]# locate -i -c NGINX.CONF
5
-----------------------------------------------------------------------------------------------------------
加反斜杠 \ 的作用
反斜杠会转义第一个字符 n,但 n 本身不是特殊字符,所以转义后仍然是 n。关键在于:locate 的一个隐藏规则——如果模式中包含转义字符(如反斜杠),它会禁用通配符模式,转而进行字面字符串匹配。
因此 '\nginx.conf' 的效果是:
将模式当作纯字符串 nginx.conf 进行匹配
只匹配完全等于 nginx.conf 的文件名(因为 -b 只匹配 basename,不匹配路径)
-----------------------------------------------------------------------------------------------------------
[root@localhost study]# locate -b '\nginx.conf'
/opt/test/test1/nginx.conf
/root/soft/nginx-1.26.2/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
[root@localhost study]# locate -b nginx.conf [# 会匹配所有包含 "nginx.conf" 的文件名(如 nginx.conf.bak)]
/opt/test/test1/nginx.conf
/root/soft/nginx-1.26.2/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.bak
/usr/local/nginx/conf/nginx.conf.default
[root@localhost study]# locate -r "/var/log/.*\.log"
/var/lib/docker/overlay2/05fea0ccd1abc9322bb352960231b42b5c0d18aa664ca19e2d8100538b1e3e15/diff/var/log/alternatives.log
/var/lib/docker/overlay2/05fea0ccd1abc9322bb352960231b42b5c0d18aa664ca19e2d8100538b1e3e15/diff/var/log/dpkg.log
/var/lib/docker/overlay2/05fea0ccd1abc9322bb352960231b42b5c0d18aa664ca19e2d8100538b1e3e15/diff/var/log/apt/eipp.log.xz
/var/lib/docker/overlay2/05fea0ccd1abc9322bb352960231b42b5c0d18aa664ca19e2d8100538b1e3e15/diff/var/log/apt/history.log
/var/lib/docker/overlay2/05fea0ccd1abc9322bb352960231b42b5c0d18aa664ca19e2d8100538b1e3e15/diff/var/log/apt/term.log
| 项 | 说明 |
|---|---|
| 基本格式 | find 路径 条件 动作 |
| 常用示例 | find /etc -name nginx.conf |
| 忽略报错 | 2>/dev/null |
| 用法 | 命令示例 | 说明 |
|---|---|---|
| 精确名称 | find / -name "nginx.conf" | 区分大小写 |
| 忽略大小写 | find / -iname "nginx.conf" | 不区分大小写 |
| 模糊匹配 | find /var -name "*.log" | 查所有log |
| 多条件 OR | find / -name "*.log" -o -name "*.txt" | 查log或txt |
| 排除 NOT | find / ! -name "*.log" | 排除log |
| 类型 | 参数 | 示例 |
|---|---|---|
| 普通文件 | -type f | find /etc -type f |
| 目录 | -type d | find /etc -type d |
| 软链接 | -type l | find / -type l |
| 块设备 | -type b | find /dev -type b |
| 字符设备 | -type c | find /dev -type c |
| 条件 | 命令 | 含义 |
|---|---|---|
| 大于100M | find / -size +100M | 大文件 |
| 小于10M | find / -size -10M | 小文件 |
| 等于50M | find / -size 50M | 精确大小 |
| 大于1G文件 | find / -type f -size +1G | 查大文件神器 |
单位:kMG
| 时间类型 | 参数 | 示例 | 含义 |
|---|---|---|---|
| 修改时间 | -mtime | find / -mtime -1 | 1天内修改 |
| 访问时间 | -atime | find / -atime +7 | 7天未访问 |
| 属性时间 | -ctime | find / -ctime -1 | 最近改权限 |
| 写法 | 含义 |
|---|---|
-mtime -1 | 24小时内 |
-mtime +30 | 30天前 |
| 用法 | 命令 | 说明 |
|---|---|---|
| 指定权限 | find / -perm 777 | 危险权限 |
| SUID 文件 | find / -perm -4000 | 安全检查 |
| SGID 文件 | find / -perm -2000 | 安全检查 |
| 用法 | 命令 |
|---|---|
| 按用户 | find / -user nginx |
| 按组 | find / -group root |
| 用法 | 命令 |
|---|---|
| 空文件 | find /tmp -type f -empty |
| 空目录 | find /tmp -type d -empty |
| 逻辑 | 示例 | 含义 |
|---|---|---|
| AND(默认) | find / -type f -name "*.log" | 同时满足 |
| OR | find / -name "*.log" -o -name "*.txt" | 满足其一 |
| NOT | find / ! -name "*.log" | 取反 |
| 功能 | 命令 |
|---|---|
| 删除文件 | find /tmp -name "*.log" -delete |
| 删除30天前日志 | find /var/log -mtime +30 -delete |
| 批量 rm | find /tmp -name "*.tmp" -exec rm -f {} \; |
| 批量 chmod | find . -name "*.sh" -exec chmod +x {} \; |
| 批量 grep | find /etc -name "*.conf" -exec grep nginx {} \; |
{} = 找到的文件\; = 命令结束
| 场景 | 命令 |
|---|---|
| 找大文件 | find / -type f -size +1G 2>/dev/null |
| 清理旧日志 | find /var/log -mtime +30 -delete |
| 查777权限 | find / -perm 777 2>/dev/null |
| 查最近修改 | find / -mtime -1 |
| 查所有log | find / -name "*.log" |
| 批量加执行权限 | find . -name "*.sh" -exec chmod +x {} \; |
| 操作 | 谁可以做 |
|---|---|
| 修改权限 chmod | 文件 所有者 或 root |
| 修改所属组 chgrp | 文件 所有者 或 root(但有限制) |
| 修改属主 chown | 只有 root |
chmod → owner 可改chgrp → owner 可改组(仅限自己的组)chown → 只有 root 能改属主
| 命令 | 文件所有者 | root | 目录权限要求 |
|---|---|---|---|
| chmod | ✅ | ✅ | 被修改文件(或目录)所在目录需要 x |
| chgrp | ✅(仅限自己组) | ✅ | 被修改文件(或目录)所在目录需要 x |
| chown | ❌ | ✅ | 被修改文件(或目录)所在目录需要 x |
| 权限 | 对普通文件 | 对目录 |
|---|---|---|
| r | 读文件内容 | 读目录列表 (ls) 「列出文件名与inode的映射关系] |
| w | 写文件 | 创建/删除文件 「写入或者删除文件名与inode的映射关系] |
| x | 执行程序 | 进入目录 (cd)[改变“当前目录 inode 指针”] |