引言
Linux命令是服务器运维的基础。本文介绍最常用的Linux命令。
一、文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| ls ls -l ls -a ls -lh
cd /path/to/dir cd ~ cd -
pwd
mkdir dir mkdir -p parent/child
rm file rm -r dir rm -rf dir
cp source dest cp -r dir1 dir2
mv source dest
cat file.txt head -n 10 file.txt tail -n 10 file.txt tail -f log.txt less file.txt more file.txt
|
二、文本处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| grep "pattern" file.txt grep -r "pattern" dir/ grep -i "pattern" file.txt grep -n "pattern" file.txt
sed 's/old/new/' file.txt sed 's/old/new/g' file.txt
awk '{print $1}' file.txt awk -F':' '{print $1}' /etc/passwd
sort file.txt sort -r file.txt sort -n file.txt
uniq file.txt sort file.txt | uniq
wc file.txt wc -l file.txt wc -w file.txt
|
三、进程管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| ps aux ps aux | grep java
top htop
pgrep java pidof java
kill PID kill -9 PID killall java
nohup command & command &
jobs fg %1 bg %1
|
四、系统信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| uname -a hostname whoami uptime
cat /proc/cpuinfo lscpu
free -h cat /proc/meminfo
df -h du -sh dir/ du -h --max-depth=1
ifconfig ip addr netstat -tunlp ss -tunlp
|
五、网络工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| ping www.google.com ping -c 4 www.google.com
curl http://example.com curl -X POST -d "data" http://api.com
wget http://example.com/file.zip wget -c url
scp file.txt user@host:/path/ scp user@host:/path/file.txt ./
ssh user@host ssh -p 2222 user@host
telnet host port
|
六、压缩解压
1 2 3 4 5 6 7 8 9 10 11 12 13
| tar -cvf archive.tar files/ tar -xvf archive.tar tar -czvf archive.tar.gz files/ tar -xzvf archive.tar.gz
zip archive.zip files unzip archive.zip
gzip file.txt gunzip file.txt.gz
|
七、权限管理
1 2 3 4 5 6 7 8 9 10 11 12 13
| chmod 755 file.sh chmod +x file.sh chmod u+w,g-x,o-r file.txt
chown user:group file.txt chown -R user:group dir/
ls -l file.txt
|
八、实用技巧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ps aux | grep java | awk '{print $2}'
command > output.txt command >> output.txt command 2> error.txt
find /path -name "*.log" find /path -type f -mtime -7
alias ll='ls -lh' alias gs='git status'
history !100 !!
|
总结
Linux命令核心:
- 文件操作:ls、cd、cp、mv
- 文本处理:grep、sed、awk
- 进程管理:ps、top、kill
- 网络工具:curl、wget、ssh
参考资料