2021-7-12 DAY2
DAY2 2021.7.12
------------恢復內容開始------------
DAY2 2021.7.12
1.重啟網路設定
systemctl stop NetworkManager #關閉無線網路服務
systemctl restart network #重啟有線網服務
2.yum 是給linux安裝系統軟體的工具
例:yum install tree -y # -y預設yes同意安裝
3.檢視linux命令的幫助資訊
1.用man手冊,linux提供的幫助文件 man ls man cp man mkdir
[root@centos ~]# man ls man cp man mkdir --Man-- 下一頁: man(1) [ 檢視 (return) | 跳過 (Ctrl-D) | 退出 (Ctrl-C) ] 1 --Man-- 下一頁: cp(1) [ 檢視 (return) | 跳過 (Ctrl-D) | 退出 (Ctrl-C) ]
2.命令加上 --help引數,檢視簡短幫助資訊 mkdir --help rm --help
3.線上的搜尋一些命令查詢網站 http://linux.51yip.com/
4.echo命令
輸出字串 和print一樣
echo 需要輸出的字串
5.定義變數值Linux在終端中的變數賦值是臨時生效
[root@centos ~]# wq="1"
[root@centos ~]# echo $wq
1
6.vim編輯器是Linux的基本編輯器
安裝vim: yum install vim -y
vim開啟一個不存在的檔案,預設會建立此檔案
#用vim寫一個python指令碼, #vim的使用流程 第一步:vim first.py ,此時會進入命令模式,按下字母 i,進入編輯模式 第二步:想要退出編輯模式,按下鍵盤的esc,回到命令模式 第三部:此時輸入 shfit+冒號,輸入一個英文的冒號,進入底線命令模式 第四步:輸入 :wq! ,write寫入內容,quit退出vim ! 強制性的操作 :wq! 強制儲存寫入退出vim :q! 強制不儲存內容,直接退出
7.cat命令
安裝python:https://www.cnblogs.com/lemon-feng/p/11208435.html
cat 檔名
[root@centos Python-3.7.1]# python first.py
wqqqwqqwwq
檢視檔案內容
#讀取內容,且顯示行號
cat -n 檔名
[root@centos Python-3.7.1]# cat -n first.py
1 print("wqqqwqqwwq")
2 print("wqqqwqqwwq")
3
8.重定向符號
“>”:覆蓋原內容
“>>”追加內容
[root@centos Python-3.7.1]# cat -n first.py 1 print("wqqqwqqwwq") 2 print("wqqqwqqwwq") 3 [root@centos Python-3.7.1]# echo "123" > first.py #一個">"覆蓋檔案內容 [root@centos Python-3.7.1]# cat -n first.py 1 123 [root@centos Python-3.7.1]# echo "123" >> first.py #兩個">"在原始檔內容中追加內容 [root@centos Python-3.7.1]# cat -n first.py 1 123 2 123
9.cp命令
cp copy縮寫 複製檔案
[root@centos Python-3.7.1]# cp first.py newfirst.py
[root@centos Python-3.7.1]# ls
aclocal.m4 Grammar Misc python
build Include Modules Python
CODE_OF_CONDUCT.rst install-sh newfirst.py python-config
config.guess Lib Objects python-config.py
config.log libpython3.7m.a Parser python-gdb.py
config.status LICENSE PC README.rst
config.sub m4 PCbuild setup.py
configure Mac Programs Tools
configure.ac Makefile pybuilddir.txt
Doc Makefile.pre pyconfig.h
first.py Makefile.pre.in pyconfig.h.in
cp -r wq new_wq??
10.mv命令
1.重新命名
mv 舊檔名 新檔名
anaconda-ks.cfg qw wq wq.txt
[root@centos ~]# mv wq.txt qw.py
[root@centos ~]# ls
anaconda-ks.cfg qw qw.py wq
2.移動位置
mv 你要移動的檔案或是資料夾 移動之後的目錄名(如果資料夾存在,則移動,不存在是改名)
案例
mv test.txt b #移動 test.txt檔案 到 b資料夾下(b資料夾得存在)
[root@centos ~]# ls
anaconda-ks.cfg qw qw.py wq
[root@centos ~]# mv qw.py qw
[root@centos ~]# ls
anaconda-ks.cfg qw wq
[root@centos ~]# cd qw
[root@centos qw]# ls
qw.py
11.linux單引號和雙引號的區別
[root@centos qw]# echo '$name'
$name
[root@centos qw]# echo "$name"
12.alias別名命令
alias start="python3 /home/mysite/manager.py runserver 0.0.0.0:8000"
13.find命令
語法
find 你要從哪找 -type 你要的檔案型別是什麼 -size 你要的檔案內容多大 -name 你要的內容名字是什麼
-type f 是找普通文字檔案
-type d 是找 資料夾 型別
-name 是指定檔案的名字內容
#在系統上 全域性搜尋,所有的.txt檔案
find / -name "*.txt"
#指定在etc目錄下,進行區域性搜尋,一個網絡卡配置檔案,網絡卡名字是以ifcfg開頭的 ,文字型別檔案
find /etc -type f -name "ifcfg*"
1.準備好測試的資料,在/tmp目錄下
mkdir /tmp/python{1..5} #在/tmp目錄下 建立 出 python1 ptyhon2 ... python5
touch /tmp/python_{a..d} #在/tmp目錄下創建出 python_a ptyhon_b .. python_d 幾個檔案
2.查看準備好的測試檔案
[root@s25linux tmp]# ls
python1 python3 python5 python_b python_d
python2 python4 python_a python_c
3.在/tmp目錄下聯絡find命令
4.找出/tmp目錄下所有的pyton相關內容
[root@s25linux tmp]# find . -name "python*"
./python1
./python2
./python3
./python4
./python5
./python_a
./python_b
./python_c
./python_d
5.找出/tmp 下所有的python相關的檔案
[root@s25linux tmp]# find . -type f -name "python*"
./python_a
./python_b
./python_c
./python_d
6.反之找出所有的資料夾
find . -type d -name "python*"
7.全域性搜尋,超過10M大小的 txt文字
[root@s25linux tmp]# find / -size +10M -name "*.txt"
/tmp/python2.txt
/tmp/python3.txt
14.檢視檔案,資料夾大小
ls -lh # -h引數,是顯示檔案單位,以kb mb gb大小為單位 -l是列表形式,列出資料夾中詳細資訊
[root@centos qw]# ls -lh
總用量 4.0K
-rw-r--r--. 1 root root 12 7月 12 20:36 1 2 3
-rw-r--r--. 1 root root 0 7月 12 20:37 qw.py
[root@centos qw]# ls -l
總用量 4
-rw-r--r--. 1 root root 12 7月 12 20:36 1 2 3
-rw-r--r--. 1 root root 0 7月 12 20:37 qw.py
15.正則grep(過濾)
語法: grep “需要過濾的字串” 檔名
[root@centos qw]# grep "一" wq.txt #查詢wq.txt檔案中包括“一”的行
跟著我左右右手一個慢動作
一個大西瓜,送給你,也不送給他
[root@centos qw]# grep -n "一" wq.txt #查詢wq.txt檔案中包括“一”的行號
1:跟著我左右右手一個慢動作
7:一個大西瓜,送給你,也不送給他
[root@centos qw]# grep -i -n "一" wq.txt
1:跟著我左右右手一個慢動作
7:一個大西瓜,送給你,也不送給他
[root@centos qw]# grep -i -n "A" wq.txt #查詢wq.txt檔案中忽略大小寫的包括“A”的行 和行號
9:ALLOW_HOSTS=[]
過濾空白行
[root@centos qw]# grep -i -n "^$" wq.txt # 找出檔案中所有的空白行號 "^$" 以空開頭,以空結尾,因此是空白行
2:
3:
5:
6:
8:
10:
[root@centos qw]# grep -i -n -v "^$" wq.txt # -v:過濾出結果相反的結果 找出檔案中所有的非空白行號 "^$" 以空開頭,以空結尾,因此是空白行
1:跟著我左右右手一個慢動作
4:#左右右手慢動作重播
7:一個大西瓜,送給你,也不送給他
9:ALLOW_HOSTS=[]
11:1 2 3
過濾註釋行
[root@centos qw]# grep -i -n -v "^#" wq.txt
1:跟著我左右右手一個慢動作
2:
3:
5:
6:
7:一個大西瓜,送給你,也不送給他
8:
9:ALLOW_HOSTS=[]
10:
11:1 2 3
cat+grep
[root@centos qw]# cat wq.txt |grep -ivn "#" #檢視並過濾檔案中不包括“#”的內容
1:跟著我左右右手一個慢動作
2:
3:
5:
6:
7:一個大西瓜,送給你,也不送給他
8:
9:ALLOW_HOSTS=[]
10:
11:1 2 3
[root@centos qw]# cat wq.txt |grep -in "#" #檢視並過濾檔案中包括“#”的內容
4:#左右右手慢動作重播
16.head tail命令
語法:head wq.txt
[root@centos qw]# head -2 wq.txt # 檢視檔案的前2行 第二行是空行
跟著我左右右手一個慢動作
語法:tail wq.txt
[root@centos qw]# tail -3 wq.txt # 檢視檔案的後3行
ALLOW_HOSTS=[]
1 2 3
如何檢視檔案的,中間3行-7行的內容
linux 如何顯示一個檔案的某幾行(中間幾行)
【一】從第3000行開始,顯示1000行。即顯示3000~3999行
cat filename | tail -n +3000 | head -n 1000
【二】顯示1000行到3000行
cat filename| head -n 3000 | tail -n +1000
*注意兩種方法的順序
分解:
tail -n 1000:顯示最後1000行
tail -n +1000:從1000行開始顯示,顯示1000行以後的
head -n 1000:顯示前面1000行
https://blog.csdn.net/iware99/article/details/89294972
17.scp命令
將檔案通過網路安全從機器1傳輸到另外一臺機器2
語法:scp 檔名 要傳送到的機器2使用者名稱@IP地址:機器2的資料夾
[root@s25linux tmp]# scp /tmp/好嗨哦.txt [email protected]:/tmp/
scp 需要下載的檔案的機器2使用者名稱@IP地址 :機器2存放的檔案地址 傳送到機器1的資料夾
scp [email protected]:/tmp/小樣別偷看.txt /opt/
傳送資料夾及多個檔案
如果傳送的是整個資料夾,就得加上 -r 遞迴的拷貝引數
[root@s25linux tmp]# scp -r ./lol [email protected]:/tmp/
#用萬用字元傳送多個檔案
[root@s25linux tmp]# scp -r ./* [email protected]:/tmp/134bak/
18.lrzsz工具
用於windows(基於xshell工具)和linux之間互相傳遞檔案
1.安裝此工具
yum install lrzsz -y
2.安裝好lrzsz之後,就存在了2個命令 一個是 rz 一個是sz
rz #直接輸入rz命令,能夠蹦出一個彈窗,接收windows的資料
sz 檔案 #傳送linux的一個檔案,發給 windows某個位置,也是出現一個彈窗
19.du命令(disk usage)
語法:du 引數 檔案或目錄
[root@centos qw]# du -h
8.0K .
20.VIM命令
在命令模式下,常用的指令
$ 快速移動到行尾
0 快速移動到游標的行首
x 刪除游標所在的字元
g 移動到檔案的第一行
G 移動到檔案的最後一行
/string 你要從檔案開頭尋找的內容,例如 /to 找出檔案中所有的to字元,按下n鍵,跳轉到下一個匹配的字元
?string 從檔案的第行,向上搜尋字串資訊
% 找到括號的另一半
yy 複製游標當前行
3yy 複製游標後3行
p 列印yy所複製的內容
dd 刪除游標所在行
4dd 刪除游標向下的4行內容
dG 刪除游標當前行,到行尾的所有內容
u 就是撤銷上一次的動作
如何快速的複製,列印生成多行內容
例如 按下 9999yy 就是 複製 9999行,然後按下p列印,就能夠快速的複製N多行了...
底線命令模式下
:wq!
:q! 不儲存退出
:數字 快速的定位到某一行
:set nu 顯示vim的行號
21.top命令
第一行 (uptime)
系統時間 主機執行時間 使用者連線數(who) 系統1,5,15分鐘的平均負載
第二行:程序資訊
程序總數 正在執行的程序數 睡眠的程序數 停止的程序數 殭屍程序數
第三行:cpu資訊
1.5 us:使用者空間所佔CPU百分比
0.9 sy:核心空間佔用CPU百分比
0.0 ni:使用者程序空間內改變過優先順序的程序佔用CPU百分比
97.5 id:空閒CPU百分比
0.2 wa:等待輸入輸出的CPU時間百分比
0.0 hi:硬體CPU中斷佔用百分比
0.0 si:軟中斷佔用百分比
0.0 st:虛擬機器佔用百分比
第四行:記憶體資訊(與第五行的資訊類似與free命令)
total:實體記憶體總量
used:已使用的記憶體總量
free:空閒的記憶體總量(free+used=total)
buffers:用作核心快取的記憶體量
第五行:swap資訊
total:交換分割槽總量
used:已使用的交換分割槽總量
free:空閒交換區總量
cached Mem:緩衝的交換區總量,記憶體中的內容被換出到交換區,然後又被換入到記憶體,但是使用過的交換區沒有被覆蓋,交換區的這些內容已存在於記憶體中的交換區的大小,相應的記憶體再次被換出時可不必再對交換區寫入。
22.ps命令
用於檢視linux程序資訊的命令
語法就是
ps -ef # -ef,是一個組合引數,-e -f 的縮寫,預設顯示linux所有的程序資訊,以及pid,時間,程序名等資訊
#過濾系統有關vim的程序
[root@centos qw]# ps -ef|grep "vim"
root 3126 1162 0 17:04 pts/0 00:00:00 grep --color=auto vim
23.kill 命令
kill 程序的id號
如果遇見卡死的程序,殺不掉,就傳送 -9 強制的訊號
kill -9 pid
24.netstat命令
檢視linux的網路埠情況
過濾連線
[root@centos qw]# netstat -tnulp|grep 22 #過濾22埠的連線
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1399/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1020/sshd
tcp6 0 0 :::22 :::* LISTEN 1020/sshd
udp 0 0 192.168.122.1:53 0.0.0.0:* 1399/dnsmasq