Skip to content

4. 文件操作

4.1 浏览与导航

ls — 列出目录内容

bash
# 基本列出
ls

# 详细信息 + 显示隐藏文件
ls -la

# 人类可读的文件大小
ls -lh

# 按时间排序(最新在前)
ls -lt

# 按大小排序(最大在前)
ls -lS

# 递归列出子目录内容
ls -R

# 显示文件类型标识
ls -F
bash
$ ls -lah
total 56K
drwxr-xr-x 5 user user 4.0K Jun 20 10:00 .
drwxr-xr-x 3 root root 4.0K Jun 15 08:30 ..
-rw------- 1 user user  220 Jun 15 08:30 .bash_history
-rw-r--r-- 1 user user 3.7K Jun 15 08:30 .bashrc
drwxr-xr-x 2 user user 4.0K Jun 18 14:20 Documents
drwxr-xr-x 2 user user 4.0K Jun 19 09:15 Downloads
-rw-r--r-- 1 user user  807 Jun 15 08:30 .profile
drwxr-xr-x 2 user user 4.0K Jun 20 10:00 projects
-rw-r--r-- 1 user user   64 Jun 20 09:30 readme.txt

cd — 切换目录

bash
# 进入指定目录
cd /var/log

# 返回上一次所在的目录
cd -

# 返回上两级
cd ../..
bash
$ cd /var/log
$ pwd
/var/log
$ cd -
/home/user
$ pwd
/home/user

4.2 创建与删除

mkdir — 创建目录

bash
# 创建单个目录
mkdir myproject

# 创建多层目录(-p 递归创建)
mkdir -p projects/web/frontend/src

# 创建时设置权限
mkdir -m 700 private_dir

touch — 创建空文件 / 更新时间戳

bash
# 创建空文件
touch newfile.txt

# 同时创建多个文件
touch file1.txt file2.txt file3.txt

# 批量创建(花括号展开)
touch report_{01..12}.csv

cp — 复制文件/目录

bash
# 复制文件
cp source.txt dest.txt

# 复制到目标目录
cp report.txt /backup/

# 递归复制目录(-r 或 -a)
cp -r myproject/ /backup/

# 保留权限和时间戳
cp -a source_dir/ dest_dir/

# 覆盖前确认
cp -i important.conf /etc/

# 显示复制过程
cp -v *.txt /backup/
bash
$ cp -v *.txt /backup/
'file1.txt' -> '/backup/file1.txt'
'file2.txt' -> '/backup/file2.txt'
'readme.txt' -> '/backup/readme.txt'

mv — 移动 / 重命名

bash
# 重命名文件
mv oldname.txt newname.txt

# 移动文件到目录
mv report.txt /archive/

# 移动并覆盖前确认
mv -i file.txt /existing_dir/

# 移动多个文件
mv *.log /var/log/backup/

rm — 删除文件/目录

bash
# 删除文件
rm file.txt

# 删除前确认
rm -i *.txt

# 递归删除目录及其内容
rm -r old_project/

# 强制递归删除(慎用!)
rm -rf /tmp/cache/

# 删除空目录
rmdir empty_dir/

⚠️ 注意: ⚠️ 致命警告:rm -rf /会删除整个系统!永远不要运行这个命令。Linux 有回收站吗?命令行下没有!删除就真的没了。

4.3 查看文件内容

bash
# cat — 一次性显示全部内容(适合小文件)
cat /etc/hostname

# 带行号显示
cat -n /etc/passwd | head -5

# less — 分页浏览(大文件必备)
less /var/log/syslog
# 按 q 退出,/keyword 搜索,n 下一个,N 上一个

# head — 查看文件开头(默认前 10 行)
head -20 /etc/passwd

# tail — 查看文件末尾
tail -20 /var/log/syslog

# 实时查看日志更新(-f = follow)
tail -f /var/log/syslog
bash
$ cat -n /etc/passwd | head -5
     1	root:x:0:0:root:/root:/bin/bash
     2	daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
     3	bin:x:2:2:bin:/bin:/usr/sbin/nologin
     4	sys:x:3:3:sys:/dev:/usr/sbin/nologin
     5	sync:x:4:65534:sync:/bin:/bin/sync

4.4 搜索文件

find — 强大的文件搜索工具

bash
# 按名称搜索(不区分大小写)
find / -name "*.conf" 2>/dev/null
find /home -iname "readme*"

# 按大小搜索(大于 100M 的文件)
find / -size +100M 2>/dev/null

# 按修改时间搜索(7天内修改过的)
find /home/user -mtime -7

# 按类型搜索
find /tmp -type f -name "*.log"

# 搜索并执行操作(删除7天前的日志)
find /var/log -name "*.log" -mtime +7 -exec rm {} \;

# 搜索并列出详细信息
find /home -name "*.txt" -exec ls -lh {} \;
bash
$ find /home/user -name "*.txt" -exec ls -lh {} \;
-rw-r--r-- 1 user user  64 Jun 20 09:30 /home/user/readme.txt
-rw-r--r-- 1 user user 1.2K Jun 19 11:45 /home/user/notes.txt
-rw-r--r-- 1 user user  256 Jun 18 16:30 /home/user/Documents/todo.txt

locate — 快速搜索(基于数据库)

bash
# 先更新数据库(需要 sudo)
sudo updatedb

# 快速搜索(比 find 快很多)
locate nginx.conf

# 限制结果数量
locate -l 5 "*.log"
bash
$ locate nginx.conf
/etc/nginx/nginx.conf
/usr/share/doc/nginx/examples/nginx.conf
/usr/share/man/man5/nginx.conf.5.gz