Linux 常用命令
2026/1/16大约 2 分钟
Linux 常用命令
文件操作
# 列出文件
ls
ls -la # 详细信息
ls -lh # 人类可读大小
# 切换目录
cd /path/to/dir
cd ~ # 家目录
cd - # 上一个目录
# 查看当前目录
pwd
# 创建目录
mkdir dir
mkdir -p a/b/c # 递归创建
# 创建文件
touch file
echo "content" > file
# 复制
cp file1 file2
cp -r dir1 dir2 # 递归复制
# 移动/重命名
mv file1 file2
mv file dir/
# 删除
rm file
rm -r dir # 递归删除
rm -rf dir # 强制删除
# 查看文件
cat file
head -n 10 file # 前10行
tail -n 10 file # 后10行
tail -f file # 实时查看
less file # 分页查看
# 查找文件
find /path -name "*.txt"
find /path -type f -size +100M # 大于100M的文件
find /path -mtime -7 # 7天内修改的文件
# 文件权限
chmod 755 file
chmod +x file # 添加执行权限
chown user:group file # 修改所有者文本处理
# grep 搜索
grep "pattern" file
grep -r "pattern" dir # 递归搜索
grep -i "pattern" file # 忽略大小写
grep -n "pattern" file # 显示行号
grep -v "pattern" file # 反向匹配
# sed 替换
sed 's/old/new/' file # 替换第一个
sed 's/old/new/g' file # 替换所有
sed -i 's/old/new/g' file # 原地修改
# awk 处理
awk '{print $1}' file # 打印第一列
awk -F: '{print $1}' /etc/passwd # 指定分隔符
awk '$3 > 100' file # 条件过滤
# 排序去重
sort file
sort -n file # 数字排序
sort -r file # 逆序
uniq file # 去重(需先排序)
sort file | uniq -c # 统计次数
# 统计
wc -l file # 行数
wc -w file # 单词数
wc -c file # 字节数系统管理
# 系统信息
uname -a # 系统信息
hostname # 主机名
uptime # 运行时间
date # 当前时间
# 磁盘
df -h # 磁盘使用
du -sh dir # 目录大小
du -sh * | sort -rh # 按大小排序
# 内存
free -h
# CPU
top
htop # 更友好的 top
# 进程
ps aux
ps -ef | grep process
pgrep process # 查找进程ID
kill PID
kill -9 PID # 强制杀死
killall process # 按名称杀死
# 网络
ifconfig
ip addr
netstat -tlnp # 监听端口
ss -tlnp # 更快的 netstat
curl url
wget url
ping host
telnet host port用户管理
# 用户
useradd username
userdel username
passwd username # 修改密码
su - username # 切换用户
sudo command # 以 root 执行
# 用户组
groupadd groupname
usermod -aG group user # 添加用户到组
# 查看
whoami
id username压缩解压
# tar
tar -cvf archive.tar files # 打包
tar -xvf archive.tar # 解包
tar -czvf archive.tar.gz files # 打包压缩
tar -xzvf archive.tar.gz # 解压
# zip
zip -r archive.zip dir
unzip archive.zip
# gzip
gzip file
gunzip file.gz管道与重定向
# 管道
command1 | command2
cat file | grep "pattern" | wc -l
# 重定向
command > file # 覆盖
command >> file # 追加
command 2> error.log # 错误输出
command > file 2>&1 # 合并输出
command < file # 输入重定向
# 后台运行
command &
nohup command & # 不挂断运行
nohup command > output.log 2>&1 &定时任务
# 编辑 crontab
crontab -e
# 格式:分 时 日 月 周 命令
# 每天凌晨执行
0 0 * * * /path/to/script.sh
# 每5分钟执行
*/5 * * * * /path/to/script.sh
# 每周一9点执行
0 9 * * 1 /path/to/script.sh
# 查看任务
crontab -l