Linux 文件处理命令指南
文件查看命令
cat
(Concatenate and display files)
bash
# 显示文件内容
cat file.txt
# 显示多个文件的内容
cat file1.txt file2.txt
# 将文件内容合并并输出到新文件
cat file1.txt file2.txt > combined.txt
# 以行号显示文件内容
cat -n file.txt
tac
(Display file contents in reverse)
bash
# 反向显示文件内容
tac file.txt
more
(View file contents one page at a time)
bash
# 分页查看文件内容
more file.txt
# 搜索关键字
more +/keyword file.txt
less
(View file contents interactively)
bash
# 交互式查看文件内容
less file.txt
# 搜索关键字
less file.txt
# 输入 /keyword 并按 Enter 搜索
head
(Display the beginning of a file)
bash
# 查看文件的前 10 行
head file.txt
# 查看文件的前 n 行
head -n 20 file.txt
# 查看文件的前 n 字节
head -c 100 file.txt
tail
(Display the end of a file)
bash
# 查看文件的最后 10 行
tail file.txt
# 查看文件的最后 n 行
tail -n 20 file.txt
# 实时查看文件的变化(如日志文件)
tail -f file.txt
nl
(Number lines of files)
bash
# 显示文件内容并添加行号
nl file.txt
od
(Dump files in octal and other formats)
bash
# 以十六进制格式显示文件内容
od -x file.txt
wc
(Word, line, character, and byte count)
bash
# 统计文件的行数、单词数和字节数
wc file.txt
# 仅统计文件的行数
wc -l file.txt
# 仅统计文件的单词数
wc -w file.txt
# 仅统计文件的字节数
wc -c file.txt
diff
(Compare files line by line)
bash
# 比较两个文件的不同
diff file1.txt file2.txt
# 比较两个文件并以侧边显示差异
diff -y file1.txt file2.txt
# 递归比较两个目录的不同
diff -r dir1/ dir2/
文件操作命令
cp
(Copy files and directories)
bash
# 复制文件
cp source.txt destination.txt
# 复制多个文件到目录
cp file1.txt file2.txt directory/
# 复制目录及其内容
cp -r source_directory/ destination_directory/
mv
(Move or rename files and directories)
bash
# 移动文件
mv source.txt /path/to/destination/
# 重命名文件
mv oldname.txt newname.txt
# 移动目录
mv source_directory/ /path/to/destination/
rm
(Remove files and directories)
bash
# 删除文件
rm file.txt
# 强制删除文件
rm -f file.txt
# 删除目录及其内容
rm -r directory/
# 强制删除目录及其内容
rm -rf directory/
mkdir
(Make directories)
bash
# 创建目录
mkdir new_directory
# 创建多级目录
mkdir -p parent_directory/child_directory
rmdir
(Remove empty directories)
bash
# 删除空目录
rmdir empty_directory
# 递归删除空目录
rmdir -p parent_directory/child_directory
split
(Split a file into pieces)
bash
# 将文件按行分割,每个文件 1000 行
split -l 1000 largefile.txt smallfile_
# 将文件按字节分割,每个文件 1MB
split -b 1M largefile.txt smallfile_
rename
(Rename files)
bash
# 将所有 .txt 文件的扩展名改为 .md
rename 's/\.txt$/\.md/' *.txt
# 批量重命名文件
rename 's/file/newfile/' file*
shred
(Securely delete files)
bash
# 安全删除文件
shred -u file.txt
# 重复覆盖 5 次
shred -n 5 file.txt
文件搜索命令
find
(Search for files in a directory hierarchy)
bash
# 按名称搜索文件
find /path/to/search -name "filename"
# 按类型搜索(例如,查找所有目录)
find /path/to/search -type d
# 按大小搜索(例如,查找大于 1MB 的文件)
find /path/to/search -size +1M
# 按修改时间搜索(例如,查找最近一天修改的文件)
find /path/to/search -mtime -1
# 按权限搜索(例如,查找权限为 755 的文件)
find /path/to/search -perm 755
locate
(Find files by name using a pre-built database)
bash
# 搜索文件
locate filename
# 更新数据库(需要 root 权限)
sudo updatedb
grep
(Search text using patterns)
bash
# 在文件中搜索字符串
grep "search_string" file.txt
# 递归搜索目录中的字符串
grep -r "search_string" /path/to/search
# 显示行号
grep -n "search_string" file.txt
# 搜索时忽略大小写
grep -i "search_string" file.txt
# 仅显示匹配的文件名
grep -l "search_string" /path/to/search/*
which
(Locate a command)
bash
# 查找命令的路径
which ls
whereis
(Locate the binary, source, and manual page files for a command)
bash
# 查找命令的二进制文件、源代码和手册
whereis ls
type
(Describe a command type)
bash
# 查看命令类型
type ls
文件权限管理命令
chmod
(Change file mode bits)
bash
# 为文件添加执行权限
chmod +x script.sh
# 设置文件的权限为 755
chmod 755 file.txt
# 递归修改目录及其内容的权限
chmod -R 755 directory/
chown
(Change file owner and group)
bash
# 更改文件的所有者
chown new_owner file.txt
# 更改文件的所有者和组
chown new_owner:new_group file.txt
# 递归更改目录及其内容的所有者和组
chown -R new_owner:new_group directory/
chgrp
(Change group ownership)
bash
# 更改文件的组
chgrp new_group file.txt
# 递归更改目录及其内容的组
chgrp -R new_group directory/
umask
(Set file mode creation mask)
bash
# 查看当前 umask 值
umask
# 设置默认权限为 755
umask 022
setfacl
和 getfacl
(Set and get file access control lists)
bash
# 设置文件的 ACL 权限
setfacl -m u:username:rwx file.txt
# 查看文件的 ACL 权限
getfacl file.txt
压缩与解压命令
tar
(Archive files)
bash
# 创建 tar 归档文件
tar -cvf archive.tar directory/
# 解压 tar 归档文件
tar -xvf archive.tar
# 创建 gz 压缩的 tar 归档文件
tar -czvf archive.tar.gz directory/
# 解压 gz 压缩的 tar 归档文件
tar -xzvf archive.tar.gz
# 创建 bz2 压缩的 tar 归档文件
tar -cjvf archive.tar.bz2 directory/
# 解压 bz2 压缩的 tar 归档文件
tar -xjvf archive.tar.bz2
zip
和 unzip
(Compress and decompress zip files)
bash
# 压缩文件到 zip
zip archive.zip file1 file2
# 递归压缩目录
zip -r archive.zip directory/
# 解压 zip 文件
unzip archive.zip
gzip
和 gunzip
(Compress and decompress gz files)
bash
# 压缩文件
gzip file.txt
# 解压文件
gunzip file.txt.gz
# 显示压缩文件内容
zcat file.txt.gz
bzip2
和 bunzip2
(Compress and decompress bz2 files)
bash
# 压缩文件
bzip2 file.txt
# 解压文件
bunzip
2 file.txt.bz2
xz
和 unxz
(Compress and decompress xz files)
bash
# 压缩文件
xz file.txt
# 解压文件
unxz file.txt.xz
7z
(Compress and decompress 7z files)
bash
# 压缩文件到 7z
7z a archive.7z file.txt
# 解压 7z 文件
7z x archive.7z
其他常用文件处理命令
file
(Determine file type)
bash
# 确定文件类型
file file.txt
basename
和 dirname
(Extract file name and directory name)
bash
# 提取文件名
basename /path/to/file.txt
# 提取目录名
dirname /path/to/file.txt
xargs
(Build and execute command lines from standard input)
bash
# 删除所有 .txt 文件
ls *.txt | xargs rm
# 查找所有 .log 文件并压缩
find . -name "*.log" | xargs tar -czvf logs.tar.gz
tee
(Read from standard input and write to standard output and files)
bash
# 将命令输出保存到文件同时显示在终端
echo "Hello, World!" | tee output.txt
tr
(Translate or delete characters)
bash
# 将文件内容中的小写字母转换为大写
cat file.txt | tr 'a-z' 'A-Z'
# 删除文件内容中的所有数字
cat file.txt | tr -d '0-9'
sort
(Sort lines of text files)
bash
# 对文件内容进行排序
sort file.txt
# 反向排序
sort -r file.txt
# 按数字排序
sort -n file.txt
uniq
(Report or omit repeated lines)
bash
# 删除重复行
uniq file.txt
# 仅显示重复行
uniq -d file.txt
# 显示重复行的出现次数
uniq -c file.txt
cut
(Remove sections from each line of files)
bash
# 提取每行的前 10 个字符
cut -c 1-10 file.txt
# 提取以逗号分隔的第二个字段
cut -d ',' -f 2 file.csv
paste
(Merge lines of files)
bash
# 将两个文件的内容按行合并
paste file1.txt file2.txt
# 将多个文件的内容按列合并
paste -d ',' file1.txt file2.txt
awk
(Pattern scanning and processing language)
bash
# 打印文件中的第二列
awk '{print $2}' file.txt
# 统计文件中的行数
awk 'END {print NR}' file.txt
# 计算文件中第三列的总和
awk '{sum += $3} END {print sum}' file.txt
sed
(Stream editor for filtering and transforming text)
bash
# 替换文件中的字符串
sed 's/old_string/new_string/' file.txt
# 删除文件中的空行
sed '/^$/d' file.txt
# 打印文件中的特定行(例如第 2 到第 4 行)
sed -n '2,4p' file.txt
echo
和 printf
(Display a line of text)
bash
# 打印字符串
echo "Hello, World!"
# 打印变量
name="Alice"
echo "Hello, $name!"
# 使用 printf 打印格式化字符串
printf "Name: %s\nAge: %d\n" "Alice" 25