系统

Linux

文件、进程、权限、网络与远程运维常用命令。 — 共 16 条

共 16 条代码片段

A. 文件与目录

浏览与切换目录

ls -la
ls -lh
cd /var/www
cd -
pwd

创建 / 复制 / 移动 / 删除

mkdir -p logs/app
cp -r src/ backup/
mv a.txt b.txt
rm -rf tmp/
rm -i important.txt

查找文件

find . -name "*.log"
find /var/www -type f -mtime -7
find . -type d -name "node_modules"
find . -size +100M

文本搜索

grep -rn "TODO" .
grep -i "error" app.log
grep -E "error|warn" app.log
tail -f app.log | grep --line-buffered error

压缩与解压

tar -czvf archive.tar.gz dir/
tar -xzvf archive.tar.gz
tar -tzvf archive.tar.gz
zip -r app.zip dist/
unzip app.zip

B. 进程与端口

查看进程

ps aux | grep nginx
top
htop
kill -9 <pid>
pkill -f "node src/index.js"

查看端口占用

lsof -i :3000
ss -lntp | grep 3000
netstat -lntp | grep 3000
kill -9 $(lsof -t -i:3000)

后台任务

nohup node app.js > app.log 2>&1 &
jobs
fg %1
Ctrl + Z   # 挂起
bg %1

C. 系统与权限

权限与归属

chmod +x deploy.sh
chmod 755 script.sh
chmod -R 644 public/
chown -R www-data:www-data /var/www/app

磁盘与内存

df -h
du -sh *
du -h --max-depth=1
free -h
uptime

systemd 服务

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl enable nginx
journalctl -u nginx -f

用户与 sudo

whoami
id
sudo -i
sudo -u www-data bash
passwd

D. 网络与远程

curl 请求

curl https://example.com
curl -I https://example.com
curl -X POST -H "Content-Type: application/json" \
  -d '{"name":"vic"}' https://api.example.com/user

SSH / SCP

ssh user@host
ssh -p 22 user@host
scp ./file.txt user@host:/tmp/
scp -r ./dist user@host:/var/www/app/
rsync -avz ./dist/ user@host:/var/www/app/

网络诊断

ping -c 4 example.com
dig example.com
nslookup example.com
ip a
traceroute example.com

下载文件

wget https://example.com/file.tar.gz
wget -O app.tar.gz https://example.com/file.tar.gz
curl -L -o app.tar.gz https://example.com/file.tar.gz