1. 程式人生 > >linux 基礎命令(一)

linux 基礎命令(一)

linux 命令

今天和大家共同學習下linux系統裏面的基礎命令行的命令。

1.命令的組成基本分為 命令字+選項+參數
2.命令字:常見的基本命令
3.選項:對命令字進行詳細的區分
(1)長選項:格式為 (--加一個或多個單詞)
(2)短選項:格式為(-加上一個字母,這裏多個字母可以在一起寫)
4.參數:將命令作用於誰
5.註意一點:大多數的選項和參數可以翻轉順序,有些參數格式是定死的只能是:命令字+選項+參數

來些例子看看
1.cd + 路徑(結尾一定是個目錄,否則會報錯)
(1)cd 在我看來cd像是一個導航,只要你能告訴我你要到那裏去,cd可以幫你完成。
(2)cd + 回車(這個是直接切換到當前用戶的家目錄一般用~表示,root用戶的是/root,其他的用戶放在/home/用戶名)

(3)cd + -(返回上一次的目錄)
(4)cd + ..(返回上一級的目錄)
(5)這裏補充下 . 的作用(. 表示當前目錄,也可以使用. +可執行文件)

 [root@localhost ~]# ./1.sh 
1

2.路徑
(1)相對路徑:只作用於當前目錄(頭可以不加/)
(2)絕對路徑:作用於所有目錄(頭要加/)


3.pwd + 回車
(1)pwd(顯示當前你在哪個路徑下面,是以絕對路徑顯示的)


4.ls
(*補充)ls -R (查看多層目錄) ls -b(查看特殊字符:比如空格)

(1)ls + 回車(默認顯示當前目錄下的文件)

[root@localhost ~]# ls
1    2.c              ha                  log                                  scriptfifo  tining.LOG                 

zabbix-3.0.4
1.c  anaconda-ks.cfg  install.log         mariadb-10.0.20-linux-x86_64.tar.gz  sun.txt     zabbix-3.0.0alpha2         

zabbix-3.0.4.tar.gz
2    file_sum.md5     install.log.syslog  output.session      

-----

(2)ls + a(顯示當前目錄下的所有文件包括隱藏的文件,隱藏文件一般是以  .  開頭的文件)

[root@localhost ~]# ls -a
. 1.sh .bash_history .cshrc install.log.syslog output.session .ssh

tining.LOG zabbix-3.0.0alpha2.tar.gz
.. 2


(3)ls -l (列方式顯示詳細信息,由於經常使用此命令所以給ls -l設置了別名,使用 ll 一樣的效果)

[root@localhost local]# ls -l
total 48
drwxr-xr-x.  2 root root  4096 Sep 23  2011 bin
drwxr-xr-x.  2 root root  4096 Sep 23  2011 etc
以空白作為分隔對應的意思分別是 (權限,硬鏈接數,擁有者,所屬組,文件大小-字節,日期,文件/目錄名)

(4)ls -t(以時間的方式顯示文件)

[root@localhost local]# ls -lat
total 56
drwxr-xr-x. 14 root root  4096 Apr 16 15:06 .
lrwxrwxrwx.  1 root root    29 Apr 16 15:06 mysql -> mariadb-10.0.20-linux-x86_64/
drwxr-xr-x. 12 root mysql 4096 Apr 16 15:06 mariadb-10.0.20-linux-x86_64
drwxr-xr-x.  6 root root  4096 Apr 16 14:01 zabbix-server
drwxr-xr-x. 13 root root  4096 Apr 11 22:41 ..
drwxr-xr-x.  5 root root  4096 Apr 11 22:41 share
drwxr-xr-x.  2 root root  4096 Sep 23  2011 bin

(5)ls -lh(人性化顯示文件大小)

[root@localhost local]# ls -lh
total 48K
drwxr-xr-x.  2 root root  4.0K Sep 23  2011 bin
drwxr-xr-x.  2 root root  4.0K Sep 23  2011 etc

(6)ls -ld + 目錄名(顯示目錄本身的信息)

[root@localhost local]# ls -ldh src/
drwxr-xr-x. 2 root root 4.0K Sep 23  2011 src/

(7)補充一下(如果命令項不知道怎麽用可以 命令項 + --help,出來的幫助信息是英文的,也可以自己設置為中文字體)

(8)touch + 文件名(創建新文件,也可以修改文件的時間戳)(*補充):(touch "可包含特殊字符"創建出來是一個文件)

[root@localhost local]# touch dajiahao
[root@localhost local]# ls
bin  dajiahao

(9)stat + 文件/目錄名(類似於windows的右鍵屬性)

[root@localhost local]# stat dajiahao 
  File: `dajiahao‘
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 915067      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-04-18 12:12:51.850001341 +0800
Modify: 2018-04-18 12:12:51.850001341 +0800
Change: 2018-04-18 12:12:51.850001341 +0800

(10)touch -a 文件名(修改文件時間戳)

[root@localhost local]# touch -a dajiahao 
[root@localhost local]# stat dajiahao 
  File: `dajiahao‘
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 915067      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-04-18 12:16:29.883002668 +0800
Modify: 2018-04-18 12:12:51.850001341 +0800
Change: 2018-04-18 12:16:29.883002668 +0800

Access:訪問時間,Modify:內容修改時間,Change:文件修改時間
(11)touch -c 文件(不創建該文件,大多用於腳本裏面)
(12)touch 文件 文件 文件 .....(創建多個文件)

[root@localhost boke]# touch a b c
[root@localhost boke]# ls
a  b  c

(13)touch {1..5}(創建1-5,5個文件。{}兩邊可以添加字母,文字,數字)

[root@localhost boke]# touch {1..5}
[root@localhost boke]# ls
1  2  3  4  5  a  b  c

(14)rm + 文件(刪除文件,原始命令應該是帶提示的刪除文件rm -i,這裏做了alias別名所以直接用帶提示的rm刪除)

[root@localhost boke]# ls
1  2  3  4  5  a  b  c
[root@localhost boke]# rm a
rm: remove regular empty file `a‘? y
[root@localhost boke]# ls
1  2  3  4  5  b  c

[root@localhost boke]# alias

alias cp=‘cp -i‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘

(15)rm -f + 文件(不帶提示的刪除文件)

[root@localhost boke]# ls
1 2 3 4 5 b c
[root@localhost boke]# rm -f b
[root@localhost boke]# ls
1 2 3 4 5 c

(16)rm -r(遞歸刪除或者刪除目錄都可以)

[root@localhost boke]# ls
1 2 3 4 5 c mulu
[root@localhost boke]# tree mulu/
mulu/
├── 1
└── 2

1 directory, 1 file
[root@localhost boke]# rm -rf mulu/
[root@localhost boke]# ls
1 2 3 4 5 c

(17)rm + 通配符(?代表一個字符??代表兩個字符--沒有包含關系,*代表全部。)

[root@localhost boke]# touch 123 1234
[root@localhost boke]# ls
1 123 1234 2 3 4 5 c
[root@localhost boke]# rm -f ???
[root@localhost boke]# ls
1 1234 2 3 4 5 c
[root@localhost boke]# rm -f ????
[root@localhost boke]# ls
1 2 3 4 5 c
[root@localhost boke]# rm -f *
[root@localhost boke]# ls
[root@localhost boke]#

(18)mkdir + 目錄名(創建目錄)

[root@localhost boke]# mkdir a b
[root@localhost boke]# ls
a a1b b c2d

(19)mkdir -p 目錄名/目錄名...(遞歸創建目錄)

[root@localhost boke]# mkdir -p a/c/d
[root@localhost boke]# tree a
a
└── c
└── d

2 directories, 0 files

(20)rmdir 空目錄(只能刪除空目錄)

[root@localhost boke]# rmdir a
rmdir: failed to remove `a‘: Directory not empty
[root@localhost boke]# rmdir a/c/d/
[root@localhost boke]# tree a
a
└── c

1 directory, 0 files

(21)cp + 文件名+目錄/文件(如果最後的cp目標為目錄,那麽文件會存放到該目錄下,如果是文件,裏面的內容會被覆蓋:這個會有提

醒)

[root@localhost boke]# cp a1b /opt/
[root@localhost boke]# cd /opt/
[root@localhost opt]# ls
a1b rh

(22)cp -pr(-r 遞歸復制或目錄復制)(-p  保留原屬性)

[root@localhost boke]# cp -pr a /opt/
[root@localhost boke]# cd -
/opt
[root@localhost opt]# ls
a a1b rh
[root@localhost opt]#

(23)mv 文件/目錄(移動或者修改名稱)

[root@localhost opt]# mv a c
[root@localhost opt]# ls
a1b c rh
[root@localhost opt]# mv a1b -
[root@localhost opt]# cd -
/usr/local/boke
[root@localhost boke]# ls
a a1b b c2d
[root@localhost boke]# mv c2d /opt/
[root@localhost boke]# cd -
/opt
[root@localhost opt]# ls

  • c c2d rh
    (24)mv 1目錄/1文件  2目錄/2文件  (移動並改名,同理cp亦可以做到)

    [root@localhost boke]# mv /opt/- c
    [root@localhost boke]# ls
    a a1b b c
    [root@localhost boke]# cd -
    /opt
    [root@localhost opt]# ls
    c c2d rh
    [root@localhost opt]#

    
    (25)scp + 用戶@ip/域名:文件/目錄 + 路徑(遠程復制,可跨系統相互傳輸,亦可以作為中間人scp+機器1 機器2,可以做免密,亦可

以手打兩個機器密碼,如果傳輸的事目錄scp後面要加-r的,這個scp基於ssh協議工作)
(26)cat/tac +文件(正序/倒敘查看小文件)

[root@localhost boke]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@localhost boke]# tac /etc/hosts

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

(27)more + 大文件(查看大文件,空格為顯示下一頁,回車顯示下一行,b顯示上一頁,q或者ctr+c退出)
(28)less + 大文件(操作方法與more相通,比more多了一項查找的功能,按下 / 在後面跟上你要查找的內容,會高亮顯示)
(29)head -n行數 + 大文件(默認輸出文件前十行,可自定義)
(30)tail -n行數+大文件(默認輸出文件尾十行,可自定義,tail -f 為實時查看)
(31)echo + 內容 > 文件(echo 具有創建文件的功能,>這個是覆蓋文件內容,>>這是追加文件內容)

[root@localhost boke]# echo 1 > file
[root@localhost boke]# cat file 
1
[root@localhost boke]# echo 2 > file 
[root@localhost boke]# cat file 
2
[root@localhost boke]# echo 3 >> file 
[root@localhost boke]# cat file 
2
3

(32)grep +關鍵字+文件(篩選文件內容包含關鍵字的全部行)

[root@localhost boke]# grep root /etc/passwd --color (這裏我是6.5系統所以,加了一個--color長選項,7系統不用)
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

(33)grep + ^關鍵字$ + 文件(以關鍵詞開頭或結尾的行過濾出來)

[root@localhost boke]# grep ^root /etc/passwd --color
root:x:0:0:root:/root:/bin/bash

(34)cut -d: -f1,6 + 文件(cut是用來截取列的-d後面如果跟的是非合法字符必須加" ",-f後面跟上列數,多行用,分開)

[root@localhost boke]# cut -d: -f1,6 /etc/passwd
root:/root
bin:/bin
daemon:/sbin
adm:/var/adm
lp:/var/spool/lpd

(35) | (|管道,前一個命令的輸出是後一個命令的輸入,管道可以連用,但必須遵守秩序)

[root@localhost boke]# cut -d: -f1,6 /etc/passwd | grep ^root 
root:/root

(36)sort(默認以26個英文字母最為排序方式,就是ACII碼)
-r 降序排序
-n 數字大小排序(小-大,1-10)
-u 剔除重復的行
-k 指定某一列
-t 分隔符
(37)uniq +文件(剔除重復的行,但是我這裏試了不行,大家試過之後也可以評論補充下)
(38)diff +文件+文件(這是對於兩個內容類似的文件對比,diff會將有區別的地方顯示出來)

    [root@localhost boke]# uniq file 
2
o3
2
o3
[root@localhost boke]# vim file1
[root@localhost boke]# cat file
2
o3
2
o3
[root@localhost boke]# cat file1 
2
[root@localhost boke]# diff file file1
2,4d1
< o3
< 2
< o3
(39)wc +目錄/文件(目錄/文件統計)
          -l  只顯示行
                    -w 只顯示字節
                    -c 只顯示大小
[root@localhost boke]# ll |wc -l
8
[root@localhost boke]# ll |wc 
      8      65     328
[root@localhost boke]# ll |wc -w
65
[root@localhost boke]# ll |wc -c
328

好了,今天先跟大家分享這些吧,明天我會繼續的。

linux 基礎命令(一)