Linux 常用命令

Ubuntu apt-get设置代理

echo `Acquire::http::proxy "socks5h://IP:1080/";
Acquire::https::proxy "socks5h://IP:1080/";` > /etc/apt.conf

硬盘

挂载移动硬盘

sudo mount  /dev/sda1 /mnt/hk -o iocharset=utf8

取消挂载移动硬盘

sudo umount /mnt/hk 

挂载 nfs (v3 协议)

mount -t nfs -o vers=3,nolock,proto=tcp,noresvport 10.0.2.222:/uc0qrfbp /mnt/nfs

开机自动挂载 nfs (v3 协议)

10.0.4.44:/z6dajk13 /mnt/nfs nfs vers=3,nolock,proto=tcp,hard,timeo=600,retrans=2,_netdev,noresvport 0 0

挂载 nfs (v4 协议)

mount -t nfs -o vers=4.0,noresvport 10.0.2.222:/ /mnt/nfs

开机自动挂载 nfs (v4 协议)

10.0.2.222:/ /mnt/nfs nfs vers=4,minorversion=0,hard,timeo=600,retrans=2,_netdev,noresvport 0 0

硬盘 / 云盘 测速

一、测试写速度:

time dd if=/dev/zero of=/tmp/test bs=8k count=1000000
测试结果:565 MB/s

二、测试读速度:

time dd if=/tmp/test of=/dev/null bs=8k
测试结果:4.7GB/s

三、测试读写速度:

time dd if=/tmp/test of=/var/test bs=64k
测试结果:387MB/s

四、参数说明

①、time 有计时作用,dd 用于复制,从 if 读出,写到 of;

②、if=/dev/zero 不产生 IO,因此可以用来测试纯写速度;

③、同理 of=/dev/null 不产生 IO,可以用来测试纯读速度;

④、将/tmp/test 拷贝到/var 则同时测试了读写速度;

⑤、bs 是每次读或写的大小,即一个块的大小,count 是读写块的数量。

————————————————
参考链接:https://blog.csdn.net/yue_2018/article/details/103347105

系统相关

批量杀进程

kill -9 `ps -ef |grep xxx|awk '{print $2}' `   

切换应用程序版本 update-alternatives

ubuntu@VM-0-2-ubuntu:/opt/application$ java -version
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)

ubuntu@VM-0-2-ubuntu:/opt/application$ update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

ubuntu@VM-0-2-ubuntu:/opt/application$ java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)

文件与文件夹

Mac 批量修改文件后缀 (包含子目录)

find . -type f -name "*.md" -exec sh -c 'mv "$0" "${0%.md}.mdx"' {} \;

批量删除指定后缀文件

# find . -name "*.mp3" | xargs rm -rf
find . -name "*.mp3" -type f -exec rm -rfv {} \;

批量清除日志文件

例如清除 docker 容器日志文件

truncate -s 0 /var/lib/docker/containers/*/*-json.log

统计当前目录下子目录大小

du -h --max-depth=1

root@l3:/var/lib/docker/containers# du -h --max-depth=1
24K     ./fa1cff828ae6d97be9187379b041911b92636a09c87ee6a585cf47494f6ad9a4
1.2G    ./f65d5367ea1b2462173fc6ef384f49616f7ddaf92551b4be258eb2bc3570af79
842M    ./aa777a7c8af5a138d3e83a8d1a5a2f60a3c0ed7358b87fa7c2f89510ac66b3fc
53G     ./a24d3bc36ecec01da0844171ea5c907e4b11dabc0ca4d7c65e00e650a69764c9
852M    ./a170d2622afeb99dc22c1f678a46b55e5efe426d556b1ad139766e8fedea88fc
647M    ./1e3f1bcfbf95080463192473cf2c8f7ee109e0c29cba53f6166b9656a2ee1c01
32K     ./80713744ebdccf1a020d93e71db30e5fc8f4d6d7284c7083ea05ee514933f9d4
942M    ./0c664ddf6d7b2b99b866619d62b29b1ac7097db3a56d8f2f42b0bca278199e07
28K     ./eda9463c78b72534c8c059c460fbd35ddf4ca39fd28d3ed4f49c9acab98d0ebf
32K     ./1ef91eb8bb5f3f9004b3d98c15587276813de4aac1d65be3b6f89863144a0d63
32K     ./800cb33c308bc4f769e7ea65e733f6a4ea352c5ad763400cd9c83b7cc9ea053c
704M    ./d6128aa5fe3088c1357ea97317126cbb62d34bdf54e1b412b81cea3afdd2df29
64K     ./8c6e7cc48cb00c576d3fb3b44dcc74ae6c986e01c490551c1951e59d454b8972
24K     ./4fbba86dc0f6c9e762a66a7184658ec6d5bd2330b08c8ed56716975c2863c393
40K     ./4316e9174c771b94bd4fb6f065f142639b311c2751dac1f411aca89bfa0d51c7
58G     .

批量命名

对指定目录下的文件批量命名

# 去除 -n 
find ./ -type f -not -iname '*.jpg' -not -iname '*.gif' -not -iname '*.jpeg' -not -iname '*.png' -exec rename -n 's/old/new/g' {} +

统计当前文件目录下的文件个数

  • 含目录
ls -l | grep -v total | wc -l 

zip 压缩文件

  • 压缩 abc 目录下文件至 abc.zip
zip -q -r abc.zip ./abc/ 

修改文件所有者

chown

jansora@jansora:~$ ls -l | grep translation
drwxr-xr-x  5 root    root    4096 79 09:32 translation
jansora@jansora:~$ sudo chown jansora translation
jansora@jansora:~$ ls -l | grep translation
drwxr-xr-x  5 jansora root    4096 79 09:32 translation

修改文件所属组

chgrp

jansora@jansora:~$ ls -l | grep translation
drwxr-xr-x  5 jansora root    4096 79 09:32 translation
jansora@jansora:~$ sudo chgrp jansora translation
jansora@jansora:~$ ls -l | grep translation
drwxr-xr-x  5 jansora jansora   4096 79 09:32 translation

内存相关

检查内存使用情况

free -h

              total        used        free      shared  buff/cache   available
Mem:           3.7G        548M        164M         20M        3.0G        2.9G
Swap:            0B          0B          0B

内存监控

/usr/bin/free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' |  awk '{print $3}' | awk '{ if($1 > 80) print $0;}'

9.66%

Docker

docker 批量删除无用镜像

docker image rm `docker images | grep none | awk '{print $3}' `

docker 批量删除无用容器

 docker container rm `docker container ls -a | grep bash | awk '{print $1}'`

评论栏