Linux常用命令
ClearSky Drizzle Lv4

linux常用命令总结

查看系统发行版本

1
2
grep -oP '(?<=^ID=).+' /etc/os-release
lsb_release -a #列出所有版本信息

打包

1
2
3
4
tar -cvf test.tar test/ #tar压缩包命令
unzip example.zip -d /home/user/Downloads #解压到指定目录
zip -r example.zip example #将文件夹压缩为zip文件
unzip -l example.zip #查看压缩包中文件

sed命令

语法

1
sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明

  • -e或–expression= 以选项中指定的script来处理输入的文本文件。
  • -f或–file= 以选项中指定的script文件来处理输入的文本文件。
  • -h或–help 显示帮助。
  • -n或–quiet或–silent 仅显示script处理后的结果。
  • -V或–version 显示版本信息。

动作说明

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  • d :删除,因为是删除啊,所以 d 后面通常不接任何东东;
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正则表达式!例如 1,20s/old/new/g 就是啦!

实例

我们先创建一个 testFile 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
$ cat testFile #查看testfile 中的内容  
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
Google
Taobao
Runoob
Tesetfile
Wiki

在第四行写入

1
sed -e 4a\newLine testFile 

删除1到3行

1
sed '1,3 d' testFile
1
2
3
4
5
6
7
root@ubuntu:/# sed '1,3 d' testFile 
Linux test
Google
Taobao
Runoob
Tesetfile
Wiki

数据的查找与替换

sed 的查找与替换的与 vi 命令类似,语法格式如下:

1
sed 's/要被取代的字串/新的字串/g'
1
2
3
4
5
6
7
8
9
10
root@ubuntu:/# sed 's/Runoob/test/g' testFile 
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
Google
Taobao
test
Tesetfile
Wiki

将 testFile 文件中每行第一次出现的 oo 用字符串 kk 替换,然后将该文件内容输出到标准输出:

1
2
3
4
5
6
7
8
9
10
root@ubuntu:/# sed -e 's/oo/kk/' testFile
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
Gkkgle
Taobao
Runkkb
Tesetfile
Wiki

g 标识符表示全局查找替换,使 sed 对文件中所有符合的字符串都被替换,修改后内容会到标准输出,不会修改原文件:

1
2
3
4
5
6
7
8
9
10
root@ubuntu:/# sed -e 's/oo/kk/g' testFile 
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
Gkkgle
Taobao
Runkkb
Tesetfile
Wiki

选项 i 使 sed 修改文件:

1
2
3
4
5
6
7
8
9
10
11
root@ubuntu:/# sed -i 's/oo/kk/g' testFile 
root@ubuntu:/# cat testFile
HELLO LINUX!
Linux is a free unix-type opterating system.
This is a linux testfile!
Linux test
Gkkgle
Taobao
Runkkb
Tesetfile
Wiki

替换原有的值

1
2
sed -i 's/password=.*/password=4444/g' ../testfile.1.ini	
sed -i "s%10.0.0.1:5010%${your_fetcher}%g" docs/nginx.conf

ifconfig 命令

启动网卡命令

1
ifconfig ens33 up

关闭网卡命令

1
ifconfig ens33 down

netstat 命令

获取服务端口占用情况

1
2
3
4
5
6
7
netstat -nlp|grep fisco-bcos
tcp 0 0 0.0.0.0:30501 0.0.0.0:* LISTEN 5639/fisco-bcos
tcp 0 0 0.0.0.0:30500 0.0.0.0:* LISTEN 5637/fisco-bcos
tcp 0 0 0.0.0.0:20501 0.0.0.0:* LISTEN 5639/fisco-bcos
tcp 0 0 0.0.0.0:20500 0.0.0.0:* LISTEN 5637/fisco-bcos
tcp 0 0 127.0.0.1:8946 0.0.0.0:* LISTEN 5639/fisco-bcos
tcp 0 0 127.0.0.1:8945 0.0.0.0:* LISTEN 5637/fisco-bcos

获取服务端口占用数量

1
netstat -nlp|grep fisco-bcos|wc -l

ps命令

检查服务进程

1
ps -ef | grep -v grep | grep fisco-bcos

检查服务进程数量

1
ps -ef | grep -v grep | grep fisco-bcos | wc -l

查看当前时间戳

1
date +%s
  • 时间戳转日期
    1
    date -d @1699319273

查看并杀死服务进程

  • 查看进程
    1
    netstat -anp|grep server_ctl
  • 杀死所有关于server_ctl的进程
    1
    pkill -f server_ctl

nohup后台运行服务保存日志

1
nohup ./my_service.sh > log.txt 2>&1 &

文件内容排序,去重统计数量

1
sort test.txt | uniq -c | sort -n
  • 输出
    1
    2
    3
    4
    [root@iZj6c6hk0w19ob8g31qessZ ~]# sort test.txt | uniq -c | sort -n
    4 ccc
    5 bbb
    6 aaa

查看文件内容是否为空,并截取第一个数据

1
wc -l test.txt | awk '{print $1}'
  • 输出
    1
    2
    3
    4
    [root@iZj6c6hk0w19ob8g31qessZ ~]# wc -l test.txt | awk '{print}'
    15 test.txt
    [root@iZj6c6hk0w19ob8g31qessZ ~]# wc -l test.txt | awk '{print $1}'
    15

mysql 命令行操作数据库

1
mysql -u root -p123456 -e "CREATE DATABASE webasestat;"

History 清除历史记录

1
2
rm -rf ~/.bash_history
history -c

修改用户密码

1
sudo passwdd <用户名>
 Comments
Comment plugin failed to load
Loading comment plugin
Powered by Hexo & Theme Keep
This site is deployed on
Unique Visitor Page View