Linux - 深度學習 - 常用命令

Linux體系結構

  • 體系結構主要分爲用戶態(用戶上層活動)和內核態
  • 內核:本質是一段管理計算機硬件設備的程序
  • 系統調用:內核的訪問接口,是一種能再簡化的操作
  • 公用函數庫:系統調用的組合拳
  • Shell:命令解釋器,可編程

必知必會

命令 說明
find 文件查找
grep 查找文件中包含的字符
| 管道操作符
awk 對文件內容做統計
sed 字符串替換

常見Shell命令

查看命令目錄

which python

更多查找find命令

man find

輸出文件內容

cat 1.txt

文件查找

在指定目錄下查找文件 - 語法

find path [options] params

當前目錄查找文件 默認遞歸查找

 find -name "*.conf"

全局查找查找文件,從根目錄查找

 find / -name "*.conf"

當前用戶名下查找

find ~ -name "target*"

忽略大小寫查找

find ~ -iname "target*"

計算文件MD5

md5sum a.text

檢索文件內容

Grep全稱:Global Regular Expression Print

作用:查找文件裏符合文件的字符串

grep [options] pattern file

基本使用

顯示所有以 .txt 文件中包含 aa.com 的行

grep 'aa.com' *.text

顯示在 a.text b.text 中 包行 aa.com 的行

grep 'aa.com' a.text b.text

查找到文件裏,篩選出符合正則表達式的內容

grep '' a.txt | grep -o '[[a-z]*'

過濾掉不需要的

grep -v 'grep'

管道操作符 |

作用

  • 可將指令連接起來,前一個指令的輸出作爲後一個指令的輸入

注意事項

  • 只處理前一個命令正確輸出,不處理錯誤輸出
  • 右邊命令必須能夠接收標準輸入流,否則傳遞過程中數據會被拋棄
  • sed、awk、grep、cut、head、top、less、more、wc、join、sort、split等

普通查找

find ~ | grep "target"

當前批量查找文件內容 比如查找一堆nginx.conf文件中,包含 com 的域名

 find  -name "*.conf" | xargs grep 'com'

對文件內容做統計 - awk

語法:awk [options] 'cmd' file

  • 一次讀取一行文本,按輸入分割符進行切片,切成多個組成部分
  • 將切片直接保存在內建的變量中,$1,$2…($0表示行的全部)
  • 支持對單個切片的判斷,支持循環判斷,默認分割符爲空格

模擬文件 netstat.txt

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2480/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2675/master
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      2480/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2675/master

查看第一列 and 第四列的內容

awk '{print $1,$4}' netstat.txt
Proto Local
tcp 0.0.0.0:111
tcp 0.0.0.0:22
tcp 127.0.0.1:25
tcp6 :::111
tcp6 :::22
tcp6 ::1:25

查找出TCP6相關,並且第二行=1的

awk '$1=="tcp6" && $2==0{print $0}' netstat.txt
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      2480/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2675/master

顯示錶頭

awk '($1=="tcp6" && $2==0) || NR==1 {print $0}' netstat.txt
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
tcp6       0      0 :::22                   :::*                    LISTEN      2480/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2675/master

測試數據

vim test.txt
dafafa,132
w,12
ee,22

分隔符 -F 以什麼符號作爲分割要求

awk -F "," '{print $2}' test.txt
132
12
22

統計功能

awk '{tcpCount[$1]++}END{for(i in tcpCount) print i "\t" tcpCount[i]}' netstat.txt
tcp6	3
Proto	1
tcp	3

批量替換文本內容 - sed

語法: sed [option]  `sed command` filename
  • 全名stream editor,流編輯器
  • 適合用於對文本的行內容進行處理

測試文件 a.java

Str a = "hello xiaomin".
Str b = "hi xiaomin".

Integer bf = new integer(2).

替換文本後,輸出到終端 s表示字符串操作

sed 's/^Str/String/' a.java

替換文本後,輸出到文件

sed  -i 's/^Str/String/' a.java
$ cat a.java
String a = "hello xiaomin".
String b = "hi xiaomin".

Integer bf = new integer(2).

替換特殊字符,把.; 《其中$ 代表以xxx結尾》

sed  -i 's/\.$/\;/' a.java

進行全文替換 加入 /g 全文替換,如果不加只替換一次

sed -i 's/xiaomin/me/g' a.java
$ cat a.java
String a = "hello me";
String b = "hi me";

Integer bf = new integer(2);

刪除空行

sed -i '/^ *$/d' a.java

刪除關鍵字的內容

sed -i '/Integer/d' a.java
$ cat a.java
String a = "hello me";
String b = "hi me";

常用擴展

查看端口 net-tools

# 安轉依賴
yum -y install net-tools
# 查看端口
netstat -anp|grep 8080

查看端口 lsof

# 安轉依賴
yum -y install lsof
# 查看端口
lsof -i:8000

下載 wget

yum install wget
wget 下載文件的地址

查看網絡橋接 brctl

yum install -y bridge-utils
brctl show
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章