01.linux基本指令
文章目錄
- 01.linux基本指令
-
- 一、目錄相關指令
- 二、檔案管理
- 三、解壓和壓縮檔案相關指令
- 四、檢視文本
- 五、系統管理指令
- 六、其他指令
一、目錄相關指令
1、ls //列出目錄内容
-l//長格式
-f//普通檔案
-d//目錄檔案
-b//塊狀裝置檔案
-c//字元裝置檔案
-l//符号連結檔案
-p//指令管道
-s//套接字檔案
-h//以人類更友善的方式顯示大小
-t//按時間順序排列
-r//逆序顯示
2、cd //切換目錄
3、pwd//檢視目前所在目錄路徑
4、mkdir//建立目錄
-p建立目錄時候目錄不存在則自動遞歸建立
-v顯示建立過程
5、rmdir//删除目錄,隻能删除空目錄
6、tree//檢視目錄樹
-d//隻顯示目錄
-l//指定顯示層級數目
-p//隻顯示由指定比對到的路徑
7、history//檢視曆史指令
8、grep //檢視過濾出關鍵字
[[email protected] ~]# ls -l
總用量 4
-rw-------. 1 root root 1231 9月 29 18:25 anaconda-ks.cfg
drwxr-xr-x. 2 root root 84 11月 2 08:18 test
[[email protected] ~]# ls -f
. .. .bash_logout .bash_profile .bashrc .cshrc .tcshrc anaconda-ks.cfg .bash_history .pki .ssh test .viminfo
[[email protected] ~]# ls -d
.
[[email protected] ~]# ls -b
anaconda-ks.cfg test
[[email protected] ~]# ls -c
test anaconda-ks.cfg
[[email protected] ~]# ls -l
總用量 4
-rw-------. 1 root root 1231 9月 29 18:25 anaconda-ks.cfg
drwxr-xr-x. 2 root root 84 11月 2 08:18 test
[[email protected] ~]# ls -p
anaconda-ks.cfg test/
[[email protected] ~]# ls -s
總用量 4
4 anaconda-ks.cfg 0 test
[[email protected] ~]# ls -h
anaconda-ks.cfg test
[[email protected] ~]# ls -t
test anaconda-ks.cfg
[[email protected] ~]# ls -r
test anaconda-ks.cfg
[[email protected] ~]# tree -d
.
└── test
1 directory
[[email protected] ~]# tree -l
.
├── anaconda-ks.cfg
└── test
├── a.txt
├── b.txt
├── c.txt
├── d.txt
├── e.txt
└── f.txt
1 directory, 7 files
[[email protected] ~]# tree -p
├── [-rw-------] anaconda-ks.cfg
└── [drwxr-xr-x] test
├── [-rw-r--r--] a.txt
├── [-rw-r--r--] b.txt
├── [-rw-r--r--] c.txt
├── [-rw-r--r--] d.txt
├── [-rw-r--r--] e.txt
└── [-rw-r--r--] f.txt
二、檔案管理
1、touch//建立一個空檔案
2、stat//顯示檔案系統狀态
3、rm//删除檔案,指令會提示是否需要删除
-r//遞歸删除,删除目錄用此選項
-f//強制删除不詢問
4、cp//複制檔案到一個檔案,多個檔案到一個目錄
*-a//歸檔複制常用于複制
-r//遞歸拷貝,拷貝目錄必須使用此選項
-p//拷貝時保留原權限
5、mv//移動檔案
6、install//複制檔案且設定屬性
-m MODE
-o OWNER
-g GROUP
[[email protected] ~]# rm -r a.txt
rm:是否删除普通空檔案 "a.txt"?y
[[email protected] ~]# rm -f b.txt
[[email protected] ~]# cp -a a.txt b.txt //檔案複制
[[email protected] ~]# ls
anaconda-ks.cfg a.txt b.txt test
[[email protected] ~]# ls
anaconda-ks.cfg a.txt c test
[[email protected] ~]# cp -r test c //目錄複制
[[email protected] ~]# ls c
b.txt test
[[email protected] ~]# ll
總用量 4
-rw-------. 1 root root 1231 9月 29 18:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 11月 2 09:45 a.txt
drwxr-xr-x. 3 root root 31 11月 2 09:48 c
drwxr-xr-x. 2 root root 84 11月 2 08:18 test
[[email protected] ~]# cp -p a.txt b.txt //拷貝保留權限
[[email protected] ~]# ll
總用量 4
-rw-------. 1 root root 1231 9月 29 18:25 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 11月 2 09:45 a.txt
-rw-r--r--. 1 root root 0 11月 2 09:45 b.txt //a和b的權限未變
drwxr-xr-x. 3 root root 31 11月 2 09:48 c
drwxr-xr-x. 2 root root 84 11月 2 08:18 test
三、解壓和壓縮檔案相關指令
第一種gzip解壓及壓縮指令
gzip -d//表示删除源檔案進行解壓
gzip -k//表示保留原檔案
zcat 檔案名//檢視壓縮檔案内容
第二種bzip壓縮及解壓指令
舉例說明bzip2後面xz和zip同理可解壓以及檢視
bzip2 -d 解壓以後删除原檔案
bzip2 -k f壓縮成bz2壓縮保留源檔案或者bunzip2解壓
bzcat a.txt.bz2檢視壓縮檔案内容
[[email protected] ~]# ls
anaconda-ks.cfg a.txt test
[[email protected] ~]# bzip2 a.txt
[[email protected] ~]# ls
anaconda-ks.cfg a.txt.bz2 test
[[email protected] ~]# bzip2 -d a.txt.bz2 //不保留原壓縮檔案解壓
[[email protected] ~]# ls
anaconda-ks.cfg a.txt test
[[email protected] ~]# bzip2 -k a.txt //保留源檔案壓縮
[[email protected] ~]# ls
anaconda-ks.cfg a.txt a.txt.bz2 test
[[email protected] ~]# bzcat a.txt.bz2 //不解壓檢視檔案内容
hello baby
第三種xz壓縮
xz f //壓縮
unxz f.xz//解壓
xzcat f.xz//檢視壓縮檔案内容
第四種zip壓縮及解壓指令
zip a.zip 檔案夾 //把一整個檔案夾裡的資料壓縮命名問a.zip
zip a.zip 檔案一 檔案二 檔案三 //把後面連續多個檔案壓縮檔案a.zip
unzip a.zip //解壓a.zip檔案
第五種tar解壓壓縮可以把前面三種壓縮指令進行綜合運用
關于tar的幾種應用
-cf a.tar d e f //把幾個檔案進行歸檔壓縮
-zcf //歸檔調用gzip
-zxf //調用gzip解壓并展開歸檔
-jcf //歸檔并調用bzip2壓縮
-jxf //調用bzip2解壓并展開歸檔
-Jcf //歸檔并調用xz壓縮
-Jxf //調用xz解壓并展開歸檔
-tf //檢視壓縮包有幾個檔案
四、檢視文本
1、cat //檢視檔案内容或者多個檔案内容重定向單個檔案内
2、tac //可以倒順序檢視
3、more//檢視檔案内容從前往後,檔案内容顯示完以後自動退出
4、less//檢視檔案内容從後往前。
5、head//檢視檔案内容前幾行内容預設前十行,也可以-n自己設定前面哪幾行檢視
6、tail//檢視檔案内容後幾行内容預設後十行,也可以-n自己設定後面哪幾行檢視,還有加-f cup 可以實時檢視檔案更新内容,在檢視日志中經常用到,
[[email protected] ~]# cat abc > b.txt
cat: abc: 沒有那個檔案或目錄
[[email protected] ~]# echo "hello" >b.txt
[[email protected] ~]# ls
anaconda-ks.cfg a.txt a.txt.bz2 b.txt test
[[email protected] ~]# cat a.txt b.txt >ab \\多個檔案内容定向ab檔案内
[[email protected] ~]# cat ab \\内容檢視驗證
hello baby
hello
[[email protected] ~]# cat -n ab \\加-n可檢視顯示幾行
1 hello baby
2 hello
[[email protected] ~]# tac ab \\倒序檢視檔案内容
hello
hello baby
[[email protected] ~]# cat > qy << EOF
> A
> B
> C
> D
> hello
> EOF
\1、請注意建立檔案并輸入多行内容内容,EOF可替換為其他單詞,但是結尾必須是替換的單詞結尾
\2、如果想給檔案輸入的單詞為變量,隻想顯示原内容,那就給結尾單詞強引用單引号引起來
\3、還可以利用\轉譯符号保持原來的變量,剩下的變量可以正常輸入
[[email protected] ~]# a=2
[[email protected] ~]# b=3
[[email protected] ~]# cat > qy <<qinyong
> $a
> $b
> qinyong
[[email protected] ~]# cat qy
2
3
例一和例二如下:
[[email protected] ~]# cat > qy <<'qinyong' \\加入強引用引号
>$a
>$b
qinyong
[[email protected] ~]# cat qy
$a
$b
例三如下:
[[email protected] ~]# cat > qy << qinyong
> \$a \利用轉譯符号保持原樣
> \$b
> $c
> qinyong
[[email protected] ~]# cat qy \檢視隻改變了變量C
$a
$b
5
[[email protected] ~]# cat qy
A
B
C
D
hello
[[email protected] ~]# head hehe
1
2
3
4
5
6
7
8
9
10
[[email protected] ~]# tail hehe
41
42
43
44
45
46
47
48
49
50
[[email protected] ~]# head -5 hehe
1
2
3
4
5
[[email protected] ~]# tail -5f cup \\檢視後面五行實時變化的内容
46
47
48
49
50
[[email protected] ~]# head -18 hehe|tail -1 \\檢視第18行head和tail利用管道符結合應用
18
五、系統管理指令
1、df磁盤使用情況,-h人類友好方式顯示
2、sed的替換屬于重點
sed -i ‘s/被替換/替換項/g’修改替換檔案内容,其中g表示全局,改為數字表示每行第幾個被修改
還有sed -i ‘nd’ 檔案名:表示删除第n行,sed -i ‘na 内容’ 檔案名:表示添加在第n行.
sed -n ‘數字p’ 檔案名:表示取第幾行,sed -n ‘/關鍵詞/p’:表示取那幾組全部相關句子
sed -i ‘nc 内容’ 檔案名:表示把n行全部改為修改内容
3、sort //預設升序排列例如:cat 檔案名|sort -n,還有-u表示去除重複
cat 檔案名|sort -r :按照逆序排列包括字母
4、uniq//去除重複,可接管道符,-c表示每個詞語重複次數,-d表示重複行,-u表示沒有重複行
5、wc//-c表示顯示位元組數,-d//表示重複行數,-w表示顯示單詞數
6、du//du 目錄:表示目錄下所有檔案大小,du 目錄/ 表示:目錄下每個檔案占多少大小*
7、df :顯示磁盤使用情況
8、hostname:修改主機名稱,如果是hostnamectl:永久修改主機名稱
[[email protected] ~]# df -h \\檢視磁盤使用情況
檔案系統 容量 已用 可用 已用% 挂載點
/dev/mapper/centos-root 17G 1.5G 16G 9% /
devtmpfs 887M 0 887M 0% /dev
tmpfs 899M 0 899M 0% /dev/shm
tmpfs 899M 9.5M 890M 2% /run
tmpfs 899M 0 899M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 180M 0 180M 0% /run/user/0
[[email protected] ~]# df -h|awk '{print $3}' \\利用awk取出第三列資料
已用
1.5G
0
0
9.6M
0
142M
0
[[email protected] ~]# cat a.txt
1hello
2boy
3red
4baby
5face
6book
7number
8say
9bye
10work
[[email protected] ~]# sed -i 's/7number/11111/g' a.txt \\修改替換檔案内容
[[email protected] ~]# cat a.txt
1hello
2boy
3red
4baby
5face
6book
11111 \\替換成功
8say
9bye
10work
[[email protected] ~]# sed -n '5p' a.txt
5face
[[email protected] ~]# sed -n '/book/p' a.txt
6book
[[email protected] ~]# cat a.txt
1hello
2boy
3red
4baby
5face
6book
11111
8say
9bye
10work
[[email protected] ~]# sed -i '2d' a.txt \\删除第二行
[[email protected] ~]# cat a.txt
1hello
3red
4baby
5face
6book
11111
8say
9bye
10work
[[email protected] ~]# sed -i '2a 2tianjia' a.txt \\在第二行後面添加,在第三行顯示内容
[[email protected] ~]# cat a.txt
1hello
3red
2tianjia
4baby
5face
6book
11111
8say
9bye
10work
[[email protected] ~]# du test \\目錄全部大小
0 test
[[email protected] ~]# du test/* \\目錄下每個檔案全部大小
0 test/a.txt
0 test/b.txt
0 test/c.txt
0 test/d.txt
0 test/e.txt
0 test/f.txt
0 test/g.txt
[[email protected] ~]# du -s test \\目錄下的大小也可以du -sh test檢視
0 test
六、其他指令
1、time 指令表示執行時間
2、clear 表示清屏
3、whoami表示目前誰登陸系統,w也可以顯示目前線上使用者
4、date表示現在什麼時間,-s可以修改時間例如:date -s ‘2019-10-10 00:00:00’
cal表示顯示月曆時間。例如看2019年12個月月曆輸入:cal -y 2019
5、which後面加上指令:顯示指定指令絕對路徑
6、ntpdate後面加上伺服器位址:表示同步時間服務
7、bc表示保留小數點例如:echo ‘scale=2;5/2’|bc -l,其中scale=表示保留幾位小數
8、ldd表示檢視指令依賴庫檔案例如檢視ls的就輸入ldd /usr/bin/ls就會顯示依賴庫有哪些