Linux 命令大全

摘要

Linux命令大全 Linux 1. 目录与文件操作 pwd # 查看当前所在路径 ls # 列出目录内容 ls -l # 详细列表 ls -al # 包含隐藏文件的详细列表 cd / # 切换到根目录 cd ~ # 切换到家目录 cd ....

Linux命令大全

Linux

1. 目录与文件操作

pwd                                 # 查看当前所在路径
ls                                  # 列出目录内容
ls -l                               # 详细列表
ls -al                              # 包含隐藏文件的详细列表
cd /                                # 切换到根目录
cd ~                                # 切换到家目录
cd ..                               # 返回上一级
mkdir test                          # 创建文件夹
mkdir -p a/b/c                      # 递归创建多级目录
rm file.txt                         # 删除文件
rm -rf folder                       # 强制删除文件夹(慎用!)
cp a.txt b.txt                      # 复制文件
cp -r folder1 folder2               # 复制文件夹
mv old.txt new.txt                  # 重命名 / 移动文件
cat file.txt                        # 查看文件全部内容
less file.txt                       # 分页查看(q退出)
head -n 10 file.txt                 # 查看前10行
tail -n 20 file.txt                 # 查看最后20行
tail -f file.txt                    # 实时跟踪文件(日志神器)
find ./ -name "*.log"               # 在当前目录查找文件
grep "keyword" file.txt             # 文件内搜索关键词
grep -r "keyword" ./                # 递归目录搜索

2. 权限 & 用户

chmod 755 run.sh                    # 修改文件权限
chown root:root app.jar             # 修改文件所属用户组
whoami                              # 查看当前用户
sudo command                        # 以管理员执行命令
su root                             # 切换root用户
useradd testuser                    # 添加用户
passwd testuser                     # 设置用户密码

3. 进程、端口、系统状态

ps -ef | grep java                  # 查看进程
top                                 # 实时查看CPU、内存
htop                                # 增强版top(需要安装)
ss -tulpn                           # 查看监听端口(推荐)
lsof -i :8080                       # 查看8080端口占用
kill 1234                           # 正常终止进程
kill -9 1234                        # 强制杀死进程
uptime                              # 查看开机时间、负载
free -h                             # 查看内存
df -h                               # 磁盘分区使用率
du -sh *                            # 查看目录大小

4. 网络相关

ip a                                # 查看网卡IP(替代ifconfig)
ping 8.8.8.8                        # 测试连通性
curl [http://127.0.0.1:8080](http://127.0.0.1:8080)          # 请求接口
curl -I http://xxx                  # 只获取响应头
wget https://xxx/file.zip           # 下载文件
scp local.txt root@ip:/opt/         # 本地传到服务器
scp root@ip:/opt/file ./            # 服务器下载到本地
ssh root@192.168.1.100 -p 22        # SSH远程连接

5. 压缩解压

tar -zcvf test.tar.gz ./test        # 打包压缩 tar.gz
tar -zxvf test.tar.gz               # 解压 tar.gz
tar -jcvf test.tar.bz2 ./test
tar -jxvf test.tar.bz2
unzip test.zip                      # 解压zip
zip -r test.zip ./test              # 压缩为zip

6. 包管理(Ubuntu / CentOS)

# Ubuntu/Debian
apt update                          # 更新源
apt install nginx                   # 安装软件
apt remove nginx                    # 卸载
apt list --installed               # 查看已安装包

# CentOS/RHEL
yum install nginx
yum remove nginx
yum list installed

7. 系统服务 systemctl

systemctl start xxx
systemctl stop xxx
systemctl restart xxx
systemctl status xxx
systemctl enable xxx                # 开机自启
systemctl disable xxx               # 取消开机自启
systemctl list-units --type=service # 列出所有服务

8. 定时任务

crontab -e                          # 编辑定时任务
crontab -l                          # 查看定时任务
crontab -r                          # 删除定时任务

9. 文本编辑 vi/vim

vim file.txt                        # 打开文件
# i 进入编辑模式;esc退出编辑;:w 保存;:q退出;:wq保存退出;:q!强制退出不保存

10. 其他高频小命令

date                                # 查看时间
hostname                            # 主机名
history                             # 查看历史命令
clear                               # 清屏