第六章、Linux 文件与目录管理 6.1 目录与路径 目录的相关操作 特殊目录符号
常见目录操作命令 cd - 切换目录
1 2 3 4 5 cd /etc cd ../.. cd ~ cd - cd /var/log && pwd
pwd - 显示当前路径
mkdir - 创建目录
1 2 3 4 5 mkdir dirname mkdir -p a/b/c/d mkdir -m 700 private mkdir dir {1,2,3} mkdir -v newdir
rmdir - 删除空目录
1 2 rmdir emptydir rmdir -p a/b/c
rm - 删除文件或目录
1 2 3 4 5 rm file.txt rm -r directory rm -rf directory rm -i file.txt rm -f *.log
PATH 环境变量 什么是 PATH PATH 是一个环境变量,指定了系统查找可执行程序的目录列表。当你在命令行输入一个命令时,系统会在 PATH 指定的目录中查找对应的程序。
查看 PATH 1 2 3 echo $PATH printenv PATH env | grep PATH
PATH 输出示例:
1 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PATH 的搜索顺序 系统按照 PATH 中目录的顺序从左到右搜索命令:
1 /usr/local/sbin → /usr/local/bin → /usr/sbin → /usr/bin → /sbin → /bin → ...
第一个匹配的命令将被执行。
修改 PATH 临时修改(当前终端会话):
1 2 3 4 5 6 7 8 export PATH=$PATH :/new/directoryexport PATH=/new/directory:$PATH export PATH=/bin:/usr/bin:/usr/local/bin
永久修改(对所有用户): 编辑 /etc/profile 或 /etc/environment:
1 2 3 4 sudo nano /etc/profileexport PATH=$PATH :/opt/myapp/bin
永久修改(仅对当前用户): 编辑 ~/.bashrc 或 ~/.profile:
1 2 3 4 5 6 7 8 nano ~/.bashrc export PATH=$PATH :$HOME /binexport PATH=$PATH :$HOME /.local/binsource ~/.bashrc
查看命令位置 1 2 3 4 5 6 7 which command which -a command whereis command whereis -b command whatis command type command type -a command
PATH 相关技巧 1 2 3 4 5 6 7 8 9 10 11 echo $PATH | grep -q "/my/dir" && echo "存在" || echo "不存在" export PATH=$(echo $PATH | sed 's|/unwanted/dir:||' )echo $PATH | tr ':' '\n' echo $PATH | tr ':' '\n' | wc -l
6.2 Linux 文件与目录管理 文件与目录的检视:ls ls 命令详解 ls (list) 命令用于列出目录内容,是使用最频繁的命令之一。
基本用法:
1 2 3 4 ls ls /path/to/directory ls file.txt ls file1 file2 dir1/
常用选项
组合使用示例 1 2 3 4 5 6 7 8 ls -la ls -lh ls -ltr ls -lS ls -ld /etc ls -R /path ls -la --color=auto ls -i
输出解析 ls -l 输出格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 -rwxr-xr-x 1 user group 12345 Jan 1 10:00 filename │└┬┘└┬┘└┬┘ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └─ 文件名 │ │ │ │ │ │ │ │ │ └────── 时间 │ │ │ │ │ │ │ │ └────────── 日期 │ │ │ │ │ │ │ └─────────────── 文件大小(字节) │ │ │ │ │ │ └───────────────────── 所属群组 │ │ │ │ │ └────────────────────────── 所有者 │ │ │ │ └───────────────────────────── 硬链接数 │ │ │ └───────────────────────────────── 其他用户权限 │ │ └─────────────────────────────────── 群组权限 │ └────────────────────────────────────── 所有者权限 └──────────────────────────────────────── 文件类型
文件类型标识:
- 普通文件
d 目录
l 符号链接
b 块设备
c 字符设备
s 套接字
p 管道
实用技巧 1. 列出最近修改的文件:
1 2 ls -lt | head -10 ls -ltr | tail -10
2. 按大小排序:
1 2 ls -lS | head -10 ls -lSr | head -10
3. 查找大文件:
1 ls -lhS | grep -E "^-.+[MG]"
4. 统计文件数量:
1 2 3 4 ls | wc -l ls -a | wc -l ls -l | grep "^-" | wc -l ls -l | grep "^d" | wc -l
5. 显示文件大小总和:
1 2 ls -l | awk '{sum+=$5} END {print sum}' ls -l | awk '{sum+=$5} END {printf "%.2f MB\n", sum/1024/1024}'
6. 查找空文件或空目录:
1 2 3 ls -l | grep "^-\s*1\s.*\s0\s" find . -type f -empty find . -type d -empty
7. 使用通配符列出特定文件:
1 2 3 4 5 6 ls *.txt ls file?.txt ls file[0-9].txt ls file[!0-9].txt ls {a,b,c}*.txt ls **/*.txt
8. 结合其他命令使用:
1 2 3 4 5 6 7 8 ls -t /path/*.log | tail -n +8 | xargs rm -fls -t | head -20 | tar -czf recent.tar.gz -T -ls -d *.txt | xargs -I {} cp {} /backup/
复制、删除与移动:cp, rm, mv cp (复制) 基本语法:
1 2 cp [选项] 源文件 目标文件cp [选项] 源文件... 目标目录
常用选项:
示例:
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 cp file.txt /backup/cp file.txt /backup/newname.txtcp -p file.txt /backup/ cp -a dir1/ dir2/ cp -r /source/dir /destination/cp -rv /source/dir /destination/ cp -i file.txt /existing/path/ cp -f file.txt /existing/path/ cp -u /source/file /destination/ cp file1 file2 file3 /destination/cp *.txt /backup/pv source.iso | cp /dev/stdin /dest/backup.iso
rm (删除) 基本语法:
常用选项:
示例:
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 rm file.txtrm file1 file2 file3rm -i file.txtrm -f file.txtrm -r directory/rm -rf directory/ rm -rv directory/rm *.log rm -rf /tmp/*.tmpshred -u file.txt find /path -type d -empty -delete find /path -name "*.log" -mtime +30 -delete
⚠️ 极度危险命令(永远不要执行):
1 2 3 4 5 rm -rf / rm -rf /* rm -rf ~ rm -rf . rm -rf .*
mv (移动/重命名) 基本语法:
1 2 mv [选项] 源文件 目标文件 mv [选项] 源文件... 目标目录
常用选项:
示例:
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 mv oldname.txt newname.txtmv file.txt /destination/mv file.txt /destination/newname.txtmv file1 file2 file3 /destination/mv *.txt /backup/mv old_dir/ /new/location/mv old_dir/ /new/location/new_name/mv -i file.txt /existing/mv -f file.txt /existing/ mv -u /source/file /destination/mv -v file.txt /destination/mv --backup=numbered file.txt /existing/mv --suffix=.bak file.txt /existing/mv -n file.txt /existing/
文件内容查阅 直接检视文件内容 cat (concatenate)
将文件内容输出到标准输出(屏幕)。
1 2 3 4 5 6 7 8 9 10 cat file.txt cat -n file.txt cat -A file.txt cat -b file.txt cat -E file.txt cat -T file.txt cat file1 file2 cat file1 file2 > file3 cat > file.txt cat >> file.txt
⚠️ 注意 :cat 适合查看小文件,大文件应该使用 less 或 more。
tac
反向输出文件内容(从最后一行开始)。
1 2 tac file.txt tac file.txt > reversed.txt
nl (number lines)
添加行号显示文件内容,比 cat -n 更灵活。
1 2 3 4 5 6 7 8 9 10 11 nl file.txt nl -b a file.txt nl -b t file.txt nl -b pBRE file.txt nl -n ln file.txt nl -n rn file.txt nl -n rz file.txt nl -w 5 file.txt nl -s ". " file.txt nl -v 10 file.txt nl -i 2 file.txt
more
分页显示文件内容,适合查看大文件。
1 2 3 4 5 more file.txt more +10 file.txt more -10 file.txt ls -la | more cat file.txt | more
在 more 中的操作:
空格 - 下一页
Enter - 下一行
b - 上一页
/pattern - 搜索
q - 退出
= - 显示当前行号
⚠️ 注意 :more 不能向前翻页(只能用 b),现在一般使用 less。
less (推荐)
更强大的分页显示工具,比 more 功能更丰富。
1 2 3 4 5 6 7 less file.txt less +10 file.txt less -N file.txt less -S file.txt less -i file.txt less +F file.txt ls -la | less
在 less 中的操作:
less 的高级用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 less file1.txt file2.txt file3.txt
head 和 tail head - 显示文件开头部分
1 2 3 4 5 head file.txt head -n 20 file.txt head -20 file.txt head -c 100 file.txt head -n -5 file.txt
tail - 显示文件结尾部分
1 2 3 4 5 6 7 tail file.txt tail -n 20 file.txt tail -20 file.txt tail -c 100 file.txt tail -n +5 file.txt tail -f file.txt tail -F file.txt
tail -f 的典型用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 tail -f /var/log/syslogtail -f /var/log/nginx/access.logtail -f /var/log/apache2/error.logtail -f /var/log/syslog /var/log/auth.logtail -f /var/log/nginx/access.log | grep "500" nohup tail -f /var/log/app.log &
head 和 tail 的组合使用:
1 2 3 4 5 6 7 8 head -10 file.txt | tail -1head -20 file.txt | tail -11sed -n '10,20p' file.txt
文件查找:find find 命令是在目录层次结构中搜索文件的强大工具。
基本语法
默认行为:
如果不指定路径,使用当前目录 .
递归搜索所有子目录
按名称查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 find /path -name "file.txt" find /path -iname "File.txt" find /path -name "*.txt" find /path -name "file?.txt" find /path -name "[ab]*" find /path ! -name "*.tmp" find /path -not -name "*.log" find /path -regex ".*/file[0-9]+\.txt" find /path -iregex ".*\.jpg"
按类型查找 1 2 3 4 5 6 7 8 9 10 11 12 find /path -type f find /path -type d find /path -type l find /path -type b find /path -type c find /path -type s find /path -type p find /path -type f -name "*.log" find /path -type d -name "test*"
按大小查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 find /path -size 10c find /path -size 10k find /path -size 10M find /path -size 10G find /path -size +10M find /path -size -10M find /path -size +1M -size -10M find /path -type f -size +100M find /path -type f -size +1G -exec ls -lh {} \;
按时间查找 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 find /path -atime 7 find /path -atime +7 find /path -atime -7 find /path -amin +60 find /path -mtime 7 find /path -mtime +7 find /path -mtime -7 find /path -mmin -30 find /path -ctime 7 find /path -ctime +7 find /path -ctime -7 find /path -empty find /path -type f -empty find /path -type d -empty find /path -type f -mtime +30 -size +100M find /path -type f -atime +365
按用户和群组查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 find /path -user username find /path -uid 1000 find /path ! -user root find /path -group groupname find /path -gid 1000 find /path -nouser find /path -nogroup find /path -user alice -group developers
按权限查找 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 find /path -perm 644 find /path -perm /u=s find /path -perm /g=s find /path -perm /o=t find /path -perm /222 find /path -perm /111 find /path -perm -444 find /path -perm -755 find /path -perm -4000 find /path -perm -2000 find /path -perm -1000 find /path -perm /6000 find / -perm /6000 -type f 2>/dev/null
对找到的文件执行操作 -exec 选项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 find /path -name "*.log" -exec rm {} \; find /var/log -name "*.log" -mtime +30 -exec rm {} \; find /path -type f -exec chmod 644 {} \; find /path -type d -exec chmod 755 {} \; find /source -name "*.txt" -exec cp {} /backup/ \; find /source -name "*.tmp" -exec mv {} /tmp/ \; find /path -name "*.txt" -exec mv {} {}.bak \;
-exec ... + 选项(更高效):
1 2 3 4 5 6 7 8 9 find /path -name "*.txt" -exec rm {} + find . -name "*.py" -exec wc -l {} + find /path -type f -exec du -ch {} + | grep total
-ok 选项(交互式):
1 2 3 4 5 6 find /path -name "*.log" -ok rm {} \; find /path -type f -ok chmod 600 {} \;
结合管道和 xargs 对于复杂的操作,可以将 find 的结果通过管道传递给 xargs 或其他命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 find /path -name "*.log" | xargs rm find /path -name "*.log" -print0 | xargs -0 rm find /path -name "*.txt" | xargs -n 10 -I {} cp {} /backup/ find /path -name "*.py" | xargs grep "import" find /path -type f | xargs du -h | sort -h find /path -name "*.log" -exec ls -lh {} \; | awk '{print $5, $9}'
find 的优化技巧 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 find /path -maxdepth 2 -name "*.txt" find /path -mindepth 2 -name "*.txt" find /path -name "/path/to/exclude" -prune -o -name "*.txt" -print find / -xdev -name "*.txt" find /path -name "*.log" -mtime +30 -delete find /path -name "*.txt" -ls find /path -name "*.txt" | parallel -j 4 gzip
实用 find 命令合集 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 find /path -type f -empty -delete find /path -type d -empty -delete find /path -mtime +30 -type f -delete find /path -size +1G -type f -delete find /path -type f -exec chmod 644 {} \; find /path -type d -exec chmod 755 {} \; find /path -user olduser -exec chown newuser {} \; find /path -group oldgroup -exec chgrp newgroup {} \; find /path -mtime -1 -type f find /path -type f -exec ls -lh {} \; | sort -k5 -rh | head find /path -type f -exec md5sum {} \; | sort | uniq -d -w32 find /path -name "*.bak" -exec rename 's/\.bak$//' {} \; find /path -name "*.log" -exec gzip {} \; find /path -name "*.txt" -exec iconv -f GBK -t UTF-8 -o {}.utf8 {} \; find /path -name "*.jpg" -exec convert {} -resize 800x600 {}.small.jpg \;
本章重点总结
目录操作 :cd, pwd, mkdir, rmdir, rm
文件操作 :cp, mv, rm
内容查看 :cat, tac, nl, more, less, head, tail
文件查找 :find(按名称、类型、大小、时间、权限、用户等)
执行操作 :-exec, -ok, xargs
PATH 环境变量 :系统查找命令的路径列表
练习题
如何在目录树中快速切换?写出5种cd的用法。
如何创建多级目录结构?如何同时创建多个目录?
解释 rm -rf 的危险性。如何安全地删除文件?
大文件应该用什么命令查看?如何实时监控日志文件?
编写 find 命令:
查找 /var/log 中7天前的日志文件
查找当前目录下大于100MB的文件
查找 /home 中不属于任何用户的文件
查找所有设置了SUID位的文件
如何为自定义脚本添加执行权限并添加到PATH?
使用 find 和 tar 组合备份最近修改的文件。