这里主要是linux常用命令的参考
命令行中常用的代码片可以参考
shell脚本中的常用命令片段可以参考
常用命令合集
菜鸟教程
进程
ps
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
|
ps aux
ps -auxwww
ps -elf
ps -elf | grep bash
ps pid
ps -ef|grep bash |grep -v "grep"|wc -l
|
参考
top
用于动态查看进程信息
watch
用于实时监测一个命令的运行结果
参考
kill
1 2 3 4 5 6 7 8 9 10 11 12 13
|
kill -n pid
|
文件查看
more
less
1 2 3 4 5 6
| G: 移动到最后一行 g: 移动到开头一行 /字符串: 查找字符串并高亮 ESC+u: 取消高亮 q: 退出less
|
cat
1 2 3 4 5 6 7 8 9 10
| cat 1.jpg 2.jpg > 3.jpg
cat > 1.txt <<EOF asdfasdf asdfasd asdf EOF
|
tail
1 2 3 4 5 6
| tail -10 a.log
tail -f a.log
tail -10f a.log
|
字符串操作
jq
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
echo '{ "foo": { "bar": { "baz": 123 } } }' | jq '.'
echo '{ "foo": { "bar": { "baz": 123 } } }' | jq '.foo.bar.baz'
echo '{ "foo": [{ "bar": { "baz": 123 } }, { "bar2": { "baa": 123 } }] }' | jq '.["foo"][0]'
url="test" echo '{ "foo": [{ "bar": { "baz": 123 } }, { "bar2": { "baa": 123 } }] }' | jq '.["foo"][2]'='{ "title": "hello", "url": "'$url'" }'
echo '{ "foo": [{ "bar": { "baz": 123 } }, { "bar2": { "baa": 123 } }] }' | jq 'del(.["foo"][0])'
jq -c -r '.' input.json > output.json
|
参考
jq手册
awk
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 38
|
netstat -antup | grep 7770 | awk '{ print $NF NR}' | awk '{ print $1}'
result=($(ls -l | grep "easul" | awk '{print $9}' | awk -F['@'] '{print $1}')) echo ${result[0]}
echo "[[176120,180000,1681796460510]]" | awk -F "," '{print $1}'
awk -F, 'NR>1{ gsub(/^"|"$/, "", $1); gsub(/^"|"$/, "", $3); imageId = $1; imageUrl = $2; videoId = $3; print imageId, imageUrl, videoId; }' "./test.log" | while read imageId, imageUrl, videoId do echo "$imageId, $imageUrl, $videoId" done
|
sed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
sed -r 's:.*tag/(.*?)" data-view-component="true".*:\1:g'
sed -i '/ <id>maven-default-http-blocker<\/id>/i\ <id>alimaven</id> \ <mirrorOf>central</mirrorOf> \ <name>aliyun maven</name> \ <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> \ </mirror> \ <mirror> \ <id>nexus-aliyun</id> \ <mirrorOf>*</mirrorOf> \ <name>Nexus aliyun</name> \ <url>http://maven.aliyun.com/nexus/content/groups/public</url> \ </mirror> \ <mirror>' ./maven/conf/settings.xml
|
cut
1 2 3 4 5 6 7 8
|
cut -c1-3 test.txt
ls -l | cut -d " " -f2
|
tr
1 2 3 4 5 6 7 8 9 10 11 12 13
|
tr -d '\n'
tr ' ' %
echo "thissss is a text linnnnnnne." | tr -s ' sn'
|
wc
1 2 3 4 5
|
ls -l | grep "^-" | wc -l
|
grep
1 2 3 4 5 6 7 8 9 10 11
|
echo "asdf"| grep 'a' -m 1
echo "google " | grep -q "google" ; echo $?
|
printf
用于格式化字符串
1 2 3 4 5 6
| i=1 printf "%05d" $i
printf "$test [hello]${test2}${test3}"
|
文件写入
tee
1 2 3 4 5 6 7 8
|
sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://xxx.xxx.xxx.xxx"] } EOF
|
echo
1 2 3 4 5 6 7 8
| echo "test test test" > test.txt
echo "test test test" >> test.txt
|
echo与printf的区别
- 前者自带回车,后者原样输出
nohup
用于生成日志命令
1 2 3 4 5
| nohup ./bin/code-server > ./logs/`date +%Y-%m-%d`.log 2>&1 &
echo "nohup ./bin/code-server > ./logs/\$(date +%Y-%m-%d).log 2>&1 &" > runcmd echo "nohup ./bin/code-server > ./logs/\`date +%Y-%m-%d\`.log 2>&1 &" > runcmd
|
文件与目录
dirname
pwd
file
用于查看文件类型
which
用于找某个可执行文件的路径,只在环境变量中找
whereis
用于查找二进制文件、源代码文件和man手册页面的路径,它会在预定义的一些目录中查找命令
find
1 2 3
|
find . -regex ".*\(\.txt\|\.pdf\)$"
|
文件容量
du
参考
df
查看所有磁盘的使用情况
参考
解压
zip
1 2
| unzip -d ./mydir/ myfile.zip
|
tar
1 2 3 4 5 6
| tar -zxvf file.tar.gz
tar -zxvf file.tar.gz -C ./test
tar -zcvf file.tar.tz ./fileFolder/
|
xz
1 2 3
| xz -d node.tar.xz tar -xvf node.tar -C ..
|
编码
base64
1 2 3 4 5 6 7
|
base64 -w 1.jpg
echo $base64Str | base64 -i -d
|
系统
crontab
1 2 3 4 5 6
| crontab -l
crontab -u easul ./deepin/crontab/crontab
crontab -e
|
systemctl
1 2 3 4 5 6 7 8 9 10 11 12
| systemctl start xxx.service
systemctl restart xxx.service
systemctl stop xxx.service
systemctl enable xxx.service
systemctl disable xxx.service
systemctl daemon-reload
|
如果需要创建自启动的服务,可以参考这篇文章的创建xx.service。
date
1 2
| date -d @1633017937 "+%Y-%m-%d %H:%M:%S"
|
网络
wget
1 2 3 4 5 6
|
wget -O file.tar.gz https://test.com/test-v1.1.tar.gz wget -P ./test -O ./test/test.tar.gz file.tar.gz https://test.com/test-v1.1.tar.gz
|
curl
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
|
wd="hello" curl -X GET -H "Host: www.baidu.com" --data-raw "wd=$wd" --compressed --url "https://www.baidu.com/s" curl -X GET -H "Host: www.baidu.com" -d "wd=$wd" --compressed --url "https://www.baidu.com/s"
curl -X GET -H "Host: www.baidu.com" -F "wd=$wd" --compressed --url "https://www.baidu.com/s"
filePath="/home/xxx/1.png" curl -X POST -H "Host: www.baidu.com" -F "file=@$filePath" --compressed --url "https://www.baidu.com/s" curl -X POST -H "Host: www.baidu.com" -d "file=@$filePath" --compressed --url "https://www.baidu.com/s" curl -X POST -H "Host: www.baidu.com" --data-urlencode 'picture='"$(base64 $filePath)"'' --compressed --url "https://www.baidu.com/s"
echo "file=$(base64 $filePath)" | curl -X POST -H "Host: www.baidu.com" -d @- --compressed --url "https://www.baidu.com/s"
curl -X POST -H "Host: www.baidu.com" -d "asdf=@file.jpg" --compressed --url "https://www.baidu.com/s"
curl -X POST -H "Host: www.baidu.com" -d "asdf=@file.txt" --compressed --url "https://www.baidu.com/s"
|
nc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
nc -nvv 192.168.0.1 80
nc -lp 8888 > recieveFile.tar.gz nc -zv 192.168.5.4 8888 < sendFile.tar.gz
ssh root@192.168.2.1 -o ProxyCommand="nc -X 5 -x 127.0.0.1:1080 %h %p"
|
命令
xargs
1 2 3 4 5 6 7 8 9
|
cut -c 9-15|xargs kill -9
ls *.jpg | xargs -n1 -I{} cp {} /data/images
|
alias
为命令起别名
1 2 3
| echo " alias mvn='/opt/software/coding/apache-maven-3.8.4/bin/mvn' " >> ~/.bashrc
|
eval
将参数看做shell命令执行,并返回结果
let
用于计算
小工具
xclip
1 2 3 4
| xclip -o -sel clip -t "image/png" > test.png
echo "asdf" | xclip -sel clip
|
shuf
seq
打印一个数字序列
sort
1 2 3
|
shuf -i 0-59 -n 10 | sort -n
|
screen
用于后台运行命令行
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
| screen -S mysession
screen -ls
screen -r mysession
exit
screen -dr mysession
|
tmux
更强大的后台运行命令行
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 38 39
| tmux new -s mysession
tmux ls
tmux attach -t mysession
exit
tmux attach -d -t mysession
|
我觉得底下颜色条的颜色不太好看,可以通过如下方式来改
1 2 3 4 5 6 7 8
|
cat >> ~/.tmux.conf <<EOF set -g status-bg "#8CA747" set -g status-fg black EOF
tmux source-file ~/.tmux.conf
|