学 Linux,最重要的不是“背命令”,而是理解命令 + 多敲几遍 + 能解决问题
这篇文章是我在学习 Linux 过程中整理的高频常用命令笔记,非常适合:
- 🧑💻 Linux 初学者
- 📦 运维 / 后端 / 嵌入式 / 打包(RPM / LFS)学习者
- 🧠 想系统复习 Linux 基础命令的人
📂 一、目录与文件操作(最最基础)
1️⃣ ls —— 查看目录内容
ls
ls -l # 详细信息
ls -a # 显示隐藏文件
ls -h # 人性化显示大小
ls -hl
📌
-a -l -h是 使用频率最高的组合
2️⃣ cd / pwd —— 切换 & 查看当前目录
cd /etc
cd ~ # 回到当前用户家目录
cd # 等价于 cd ~
pwd # 查看当前路径
3️⃣ 创建 & 查看文件
touch test.txt
查看文件内容:
cat test.txt # 一次性输出
more test.txt # 支持分页
# 空格翻页,q 退出
📦 二、文件操作(复制 / 移动 / 删除)
4️⃣ cp / mv / rm
📌 复制
cp test.txt test2.txt
cp -r dir1 dir2 # 复制文件夹(必须加 -r)
📌 移动 / 重命名
mv old.txt new.txt
mv file /tmp/
📌 删除(⚠️ 谨慎)
rm test.txt
rm -f test.txt # 强制删除
rm -rf dir/ # 强制删除文件夹
rm *.log # 通配符
🔍 三、查找与定位
5️⃣ which / find
which ls
which cd
📌
cd是 shell 内建命令,所以which cd通常找不到
find / -name "test*"
find / -size -10k
🧹 四、文本过滤 & 统计(非常重要)
6️⃣ grep —— 文本过滤
grep "heima" test.txt
grep -n "code" test.txt # 显示行号
7️⃣ wc —— 统计
wc -l test.txt # 行数
wc -w test.txt # 单词数
wc -c test.txt # 字节数
wc -m test.txt # 字符数
🔗 五、管道符 |(Linux 灵魂)
cat itheima.txt | grep itheima
👉 把左边的输出,当成右边的输入
常见组合:
ps -ef | grep mysql
netstat -anp | grep 3306
🖨 六、输出 & 重定向
8️⃣ echo / 重定向符
echo "hello linux"
📌 重定向
> 覆盖
>> 追加
echo "hello" > test.txt
ls >> test.txt
📌 反引号(命令替换)
echo `date`
9️⃣ tail —— 查看日志神器
tail test.txt
tail -20 test.txt
tail -f test.txt # 实时跟踪(日志必备)
✍️ 七、vim / vi 编辑器(绕不过去)
常用模式
- 命令模式
- 输入模式
- 底线命令模式
高频操作
i / a / o 进入输入模式
dd 删除一行
yy 复制一行
p 粘贴
u 撤销
Ctrl + r 反向撤销
底线命令
:wq 保存退出
:q 退出
:q! 强制退出
:set nu
:set paste
搜索
/关键词
n 向下
N 向上
⌨️ 八、快捷键(效率飞升)
Ctrl + C 强制停止
Ctrl + D 退出登录
Ctrl + L 清屏
Ctrl + A 行首
Ctrl + E 行尾
Ctrl + R 搜索历史命令
📜 九、历史命令
history
!ls # 执行最近的 ls
📦 十、软件管理
10️⃣ yum(CentOS)
yum install
yum remove
yum search
yum -y install wget
Ubuntu 使用
apt
⚙️ 十一、systemctl 服务管理
systemctl start 服务名
systemctl stop 服务名
systemctl status 服务名
systemctl enable 服务名
systemctl disable 服务名
常见服务:
sshdfirewalldNetworkManager
🔗 十二、软链接(快捷方式)
ln -s 源文件 目标链接
🕒 十三、时间与时区
date
date +%Y-%m-%d
date -d "+1 day" +%Y-%m-%d
修改时区
rm -f /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
🌐 十四、网络相关
ifconfig
hostname
hostnamectl set-hostname liuzixi
ping -c 4 baidu.com
下载工具
wget url
wget -b url
curl -O url
curl cip.cc
🚪 十五、端口 & 进程
端口查看
nmap 127.0.0.1
netstat -anp | grep 22
进程管理
ps -ef
ps -ef | grep mysql
kill pid
kill -9 pid
📊 十六、系统监控
top
df -h
iostat -x 1 5
sar -n DEV 1 5
🌱 十七、环境变量
env
echo $PATH
export MYNAME=liuzixi
永久生效
- 当前用户:
~/.bashrc - 所有用户:
/etc/profile
source ~/.bashrc
📤 十八、上传下载(终端)
rz # 上传
sz # 下载
📦 十九、压缩 & 解压
tar
tar -zcvf test.tar.gz a.txt b.txt
tar -zxvf test.tar.gz -C /tmp
zip
zip -r test.zip a.txt dir/
unzip test.zip -d /tmp
🐬 二十、MySQL 5.7(CentOS)
yum -y install mysql-community-server
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
🎯 总结
Linux 命令不需要一次全记住
用得多的,自然就熟了
📌 建议学习方式:
- 多敲
- 多组合(
| grep) - 多看日志(
tail -f) - 多踩坑(真的)