Linux命令入門篇(一)

最近都在學習Linux命令,漸漸以爲命令行模式是那麼的簡潔明瞭,短小精悍,而且功能強大。本文用來記錄學習的過程,僅供學習分享使用,若有不足之處,還請指正。html

Linux的目錄結構

在Linux命令模式下,輸入ls / 或者 先切換到根目錄下,在使用ls命令。 便可查詢Linux操做系統的目錄結構,以下所示:linux

1 [root@bogon ~]# ls /
2 bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
3 boot  etc  lib   media  opt  root  sbin  sys  usr
4 [root@bogon ~]# cd /
5 [root@bogon /]# ls
6 bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
7 boot  etc  lib   media  opt  root  sbin  sys  usr

樹狀目錄結構以下所示:centos

關於各個目錄的說明,可參考菜鳥筆記bash

Linux帳號相關命令

經過useradd新增帳號 和經過passwd設置密碼,以下所示:網絡

1 [root@bogon ~]# useradd heabc
2 [root@bogon ~]# passwd heabc
3 Changing password for user heabc.
4 New password:
5 BAD PASSWORD: The password is shorter than 7 characters
6 Retype new password:
7 passwd: all authentication tokens updated successfully.

經過userdel刪除密碼,以下所示:app

1 [root@bogon ~]# userdel heabc
2 userdel: user heabc is currently used by process 2477

以上提示表示用戶被進程佔用,不能夠刪除,須要關閉進程,才能夠刪除成功。ide

經過su命令切換帳號,切換帳號時,若是不加短橫線,表示不切換環境變量;加短橫線,則切換變量。以下所示:oop

1 [root@bogon ~]# su - heabc
2 [heabc@bogon ~]$ ls
3 [heabc@bogon ~]$ who
4 root     pts/0        2020-11-29 22:17 (192.168.127.1)
5 [heabc@bogon ~]$ su - root
6 Password:
7 Last login: Sun Nov 29 22:54:26 CST 2020 on pts/0

經過who命令,查詢當前登陸的帳號,以下所示:學習

1 [root@bogon /]# who
2 root     pts/0        2020-11-30 22:42 (192.168.127.1)
3 admin    :0           2020-11-30 22:58 (:0)

經過uptime查詢系統運行的時間,以下所示:測試

1 [root@bogon /]# uptime
2  23:01:02 up 19 min,  2 users,  load average: 0.14, 0.24, 0.13

同過shutdown -h now 進行關機命令,經過shutdown -r now進行重啓命令操做,以下所示:

 

 

 Linux進程查詢命令

經過ps命令查詢系統後臺運行的進程。以下所示:

1 [root@bogon ~]# ps -aux
2 USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
3 root          1  0.0  0.7 128268  6972 ?        Ss   22:15   0:01 /usr/lib/syste
4 root          2  0.0  0.0      0     0 ?        S    22:15   0:00 [kthreadd]
5 root          4  0.0  0.0      0     0 ?        S<   22:15   0:00 [kworker/0:0H]
6 root          5  0.0  0.0      0     0 ?        S    22:15   0:00 [kworker/u256:
7 root          6  0.0  0.0      0     0 ?        S    22:15   0:00 [ksoftirqd/0]

Linux目錄相關命令

經過cd命令,切換目錄,能夠就相對路徑,也能夠就絕對路徑,以下所示:

1 [root@bogon admin]# cd ../hexx
2 [root@bogon hexx]# ls
3 dir1  dir2  dir2_2  dir3  file2.txt  file4  file5.txt  file6.txt

經過pwd命令,顯示當前所在的目錄,以下所示:

1 [root@bogon ~]# pwd
2 /root

經過ls命令顯示當前目錄的內容,以下所示:

1 [root@bogon ~]# cd /home
2 [root@bogon home]# ls
3 admin  hexx
4 [root@bogon home]# cd /root
5 [root@bogon ~]# ls
6 anaconda-ks.cfg  initial-setup-ks.cfg

經過ls -a 顯示目錄下全部的內容,包括隱藏內容,以下所示:

1 [root@bogon ~]# ls -a
2 .                .bash_history  .bashrc  .cshrc                .local
3 ..               .bash_logout   .cache   .dbus                 .tcshrc
4 anaconda-ks.cfg  .bash_profile  .config  initial-setup-ks.cfg

經過touch命令建立文件,默認在當前目錄下建立,以下所示:

1 [root@bogon hexx]# touch file1 file2 file3
2 [root@bogon hexx]# ls
3 file1  file2  file3

也能夠指定路徑下建立文件,以下所示:

1 [root@bogon home]# touch  hexx/file4
2 [root@bogon home]# cd hexx
3 [root@bogon hexx]# ls
4 file1  file2  file3  file4

經過cat命令,查看文件內容,以下所示:

1 [root@bogon etc]# cat host.conf
2 multi on
3 [root@bogon etc]# cat passwd
4 root:x:0:0:root:/root:/bin/bash
5 bin:x:1:1:bin:/bin:/sbin/nologin
6 daemon:x:2:2:daemon:/sbin:/sbin/nologin
7 adm:x:3:4:adm:/var/adm:/sbin/nologin
8 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9 ......

 經過head命令,查看文件內容前幾行,以下所示:

 1 [root@bogon etc]# head passwd
 2 root:x:0:0:root:/root:/bin/bash
 3 bin:x:1:1:bin:/bin:/sbin/nologin
 4 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 5 adm:x:3:4:adm:/var/adm:/sbin/nologin
 6 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 7 sync:x:5:0:sync:/sbin:/bin/sync
 8 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 9 halt:x:7:0:halt:/sbin:/sbin/halt
10 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 operator:x:11:0:operator:/root:/sbin/nologin

經過cp命令複製文件

複製文件,既能夠採用源文件名,也能夠從新命名,以下所示:

1 [root@bogon hexx]# cp file1 file6.txt
2 [root@bogon hexx]# ls
3 file1  file2  file3  file4  file5.txt  file6.txt

cp命令既能夠相同目錄內容複製,也能夠跨目錄複製,以下所示:

1 [root@bogon hexx]# cp file2 ../admin/file2.txt
2 [root@bogon hexx]# ls
3 file1  file2  file3  file4  file5.txt  file6.txt
4 [root@bogon hexx]# cd ../admin
5 [root@bogon admin]# ls
6 Desktop    Downloads  Music     Public     Videos
7 Documents  file2.txt  Pictures  Templates

 

經過cp -r 命令複製文件夾及其內容,以下所示:

 1 [root@bogon hexx]# cp -r  dir2 dir2_2
 2 [root@bogon hexx]# ls
 3 dir1  dir2  dir2_2  dir3  file2.txt  file4  file5.txt  file6.txt
 4 [root@bogon hexx]# cd dir2_2
 5 [root@bogon dir2_2]# ls
 6 [root@bogon dir2_2]# cd ../
 7 [root@bogon hexx]# ls
 8 dir1  dir2  dir2_2  dir3  file2.txt  file4  file5.txt  file6.txt
 9 [root@bogon hexx]# cp -r dir2 ../admin/dir2_3
10 [root@bogon hexx]# cd ../admin
11 [root@bogon admin]# ls
12 Desktop  dir3       Downloads  file2.txt  Pictures  Templates
13 dir2_3   Documents  file1      Music      Public    Videos

經過mv命令移動文件,以下所示:

1 [root@bogon hexx]# mv file1 ../admin/file1
2 [root@bogon hexx]# ls
3 file2  file3  file4  file5.txt  file6.txt
4 [root@bogon hexx]# cd ../admin
5 [root@bogon admin]# ls
6 Desktop    Downloads  file2.txt  Pictures  Templates
7 Documents  file1      Music      Public    Videos

若是mv移動的文件在同一目錄內容,則是重命名,以下所示:

1 [root@bogon hexx]# mv file2 file2.txt
2 [root@bogon hexx]# ls
3 file2.txt  file3  file4  file5.txt  file6.txt

 

若是mv在同一目錄操做,在當前文件目錄中已存在,則移動到目標目錄內;如不存在,則是重命名。以下所示:

 1 [root@bogon hexx]# ls
 2 dir1  dir2  dir2_2  dir3  file2.txt  file4  file5.txt  file6.txt
 3 [root@bogon hexx]# mv dir3 dir3_3
 4 [root@bogon hexx]# ls
 5 dir1  dir2  dir2_2  dir3_3  file2.txt  file4  file5.txt  file6.txt
 6 [root@bogon hexx]# mv dir2 dir2_2
 7 [root@bogon hexx]# ls
 8 dir1  dir2_2  dir3_3  file2.txt  file4  file5.txt  file6.txt
 9 [root@bogon hexx]# cd dir2_2
10 [root@bogon dir2_2]# ls
11 dir2

若是mv在不一樣目錄內操做,則至關於剪切功能,以下所示:

1 [root@bogon hexx]# ls
2 dir1  dir2_2  dir3_3  file2.txt  file4  file5.txt  file6.txt
3 [root@bogon hexx]# mv dir1 ../admin/dir1_1
4 [root@bogon hexx]# ls
5 dir2_2  dir3_3  file2.txt  file4  file5.txt  file6.txt
6 [root@bogon hexx]# cd ../admin
7 [root@bogon admin]# ls
8 Desktop  dir2_3  Documents  file1      Music     Public     Videos
9 dir1_1   dir3    Downloads  file2.txt  Pictures  Templates

從別的目錄移動到當前目錄,以下所示:

1 [root@bogon hexx]# mv ../admin/dir1_1 dir5
2 [root@bogon hexx]# ls
3 dir2_2  dir3_3  dir5  file2.txt  file4  file5.txt  file6.txt

經過rm命令刪除文件,以下所示:

1 [root@bogon hexx]# rm file3
2 rm: remove regular empty file ‘file3’? Y
3 [root@bogon hexx]# ls
4 file2.txt  file4  file5.txt  file6.txt

經過佔位符?刪除文件,以下所示:

1 [root@bogon hexx]# ls
2 dir  dir2_2  dir5  file2.txt  file4  file5.txt  file6.txt
3 [root@bogon hexx]# rm file?
4 rm: remove regular empty file ‘file4’? Y
5 [root@bogon hexx]# ls
6 dir  dir2_2  dir5  file2.txt  file5.txt  file6.txt

 

經過通配符*刪除文件及目錄,以下所示:

1 [root@bogon hexx]# rm -r dir*
2 rm: remove directory ‘dir’? Y
3 rm: descend into directory ‘dir2_2’? Y
4 rm: remove directory ‘dir2_2/dir2’? Y
5 rm: remove directory ‘dir2_2’? Y
6 rm: remove directory ‘dir5’? Y
7 rm: remove regular empty file ‘dir_file1’? Y
8 [root@bogon hexx]# ls
9 file2.txt  file5.txt  file6.txt

經過find命令查詢文件,

查詢指定文件名的文件,以下所示:

1 [root@bogon hexx]# find ./ -name file4
2 ./file4

經過?佔位符查詢,以下所示:

1 [root@bogon hexx]# find ./ -name file?
2 ./file4
3 [root@bogon hexx]# find ./ -name file\?
4 ./file4

經過*號模糊查詢,以下所示:

 1 [root@bogon hexx]# find ./ -name file\*
 2 ./file4
 3 ./file5.txt
 4 ./file6.txt
 5 ./file2.txt
 6 [root@bogon hexx]# find ./ -name \*le\*
 7 ./.bash_profile
 8 ./file4
 9 ./file5.txt
10 ./file6.txt
11 ./file2.txt

同file命令,查詢文件類型,以下所示:

1 [root@bogon admin]# file file2.txt
2 file2.txt: empty
3 [root@bogon admin]# file Music
4 Music: directory

經過ll命令,查詢文件的詳細內容,以下所示:

1 [root@bogon ~]# ll
2 total 8
3 -rw-------. 1 root root 1987 May 26  2020 anaconda-ks.cfg
4 -rw-r--r--. 1 root root 2018 May 26  2020 initial-setup-ks.cfg

其中,第一個,短橫線表示普通文件,D表示文件目錄。

 經過ls - l查詢當前目錄全部文件的詳細信息,以下所示:

1 [root@bogon hexx]# ls -l
2 total 0
3 drwxr-xr-x. 2 root root  6 Nov 26 22:47 dir1
4 drwxr-xr-x. 2 root root  6 Nov 26 22:47 dir2
5 drwxr-xr-x. 3 root root 19 Nov 26 22:51 dir3
6 -rw-r--r--. 1 root root  0 Nov 25 22:07 file2.txt
7 -rw-r--r--. 1 root root  0 Nov 25 22:09 file4
8 -rw-r--r--. 1 root root  0 Nov 25 22:13 file5.txt
9 -rw-r--r--. 1 root root  0 Nov 25 22:17 file6.txt

 

也能夠查詢,其餘目錄的文件信息,以下所示:

1 [root@bogon hexx]# ls -l ../admin
2 total 0
3 drwxr-xr-x. 2 admin admin 6 May 26  2020 Desktop
4 drwxr-xr-x. 2 root  root  6 Nov 26 22:48 dir3
5 drwxr-xr-x. 2 admin admin 6 May 26  2020 Documents
6 drwxr-xr-x. 2 admin admin 6 May 26  2020 Downloads
7 -rw-r--r--. 1 root  root  0 Nov 25 22:07 file1
8 -rw-r--r--. 1 root  root  0 Nov 25 22:36 file2.txt
9 ......

經過ls -al查詢當前目錄和其餘目錄下的所有文件的詳細信息,以下所示:

 1 [root@bogon hexx]# ls -al
 2 total 16
 3 drwx------. 8 1001 1001 228 Nov 26 22:51 .
 4 drwxr-xr-x. 4 root root  31 Nov 24 22:53 ..
 5 -rw-------. 1 1001 1001  23 Nov 24 22:59 .bash_history
 6 -rw-r--r--. 1 1001 1001  18 Apr  1  2020 .bash_logout
 7 ........
 8 [root@bogon hexx]# ls -al ../admin
 9 total 32
10 drwx------. 16 admin admin 4096 Nov 26 22:48 .
11 drwxr-xr-x.  4 root  root    31 Nov 24 22:53 ..
12 -rw-------.  1 admin admin  279 Nov 25 23:06 .bash_history
13 -rw-r--r--.  1 admin admin   18 Apr  1  2020 .bash_logout
14 -rw-r--r--.  1 admin admin  193 Apr  1  2020 .bash_profile
15 -rw-r--r--.  1 admin admin  231 Apr  1  2020 .bashrc
16 drwx------. 14 admin admin 4096 May 26  2020 .cache
17 drwxr-xr-x. 14 admin admin  261 May 26  2020 .config
18 ......

經過mkdir命令,建立文件目錄,以下所示:

1 [root@bogon hexx]# mkdir dir dir1
2 [root@bogon hexx]# ls
3 dir  dir1  dir2_2  dir3_3  dir5  file2.txt  file4  file5.txt  file6.txt

經過rmdir命令刪除目錄,以下所示:

1 [root@bogon hexx]# rmdir dir1
2 [root@bogon hexx]# ls
3 dir  dir2_2  dir3_3  dir5  file2.txt  file4  file5.txt  file6.txt

經過rmdir -r命令刪除目錄及子目錄,以下所示:

1 [root@bogon hexx]# rm -r  dir3_3
2 rm: descend into directory ‘dir3_3’? Y
3 rm: descend into directory ‘dir3_3/dir33’? Y
4 rm: remove directory ‘dir3_3/dir33/dir333’? Y
5 rm: remove directory ‘dir3_3/dir33’? Y
6 rm: remove directory ‘dir3_3’? Y
7 [root@bogon hexx]# ls
8 dir  dir2_2  dir5  file2.txt  file4  file5.txt  file6.txt

 

網絡相關命令

ifconfig命令,查看網絡配置,以下所示:

 1 [root@bogon ~]# ifconfig
 2 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
 3         inet 192.168.127.129  netmask 255.255.255.0  broadcast 192.168.127.255
 4         inet6 fe80::24e8:9baf:7982:428b  prefixlen 64  scopeid 0x20<link>
 5         ether 00:0c:29:6d:3e:16  txqueuelen 1000  (Ethernet)
 6         RX packets 2976  bytes 214136 (209.1 KiB)
 7         RX errors 0  dropped 0  overruns 0  frame 0
 8         TX packets 283  bytes 41368 (40.3 KiB)
 9         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
10 
11 lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
12         inet 127.0.0.1  netmask 255.0.0.0
13         inet6 ::1  prefixlen 128  scopeid 0x10<host>
14         loop  txqueuelen 1000  (Local Loopback)
15         RX packets 48  bytes 4080 (3.9 KiB)
16         RX errors 0  dropped 0  overruns 0  frame 0
17         TX packets 48  bytes 4080 (3.9 KiB)
18         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
19 
20 virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
21         inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
22         ether 52:54:00:56:b3:7f  txqueuelen 1000  (Ethernet)
23         RX packets 0  bytes 0 (0.0 B)
24         RX errors 0  dropped 0  overruns 0  frame 0
25         TX packets 0  bytes 0 (0.0 B)
26         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ping命令,測試網絡連通,經過Ctrl+C中止命令,以下所示:

 1 [root@bogon ~]# ping 192.168.1.102
 2 PING 192.168.1.102 (192.168.1.102) 56(84) bytes of data.
 3 64 bytes from 192.168.1.102: icmp_seq=1 ttl=128 time=48.6 ms
 4 64 bytes from 192.168.1.102: icmp_seq=2 ttl=128 time=60.9 ms
 5 64 bytes from 192.168.1.102: icmp_seq=3 ttl=128 time=83.2 ms
 6 64 bytes from 192.168.1.102: icmp_seq=4 ttl=128 time=105 ms
 7 64 bytes from 192.168.1.102: icmp_seq=5 ttl=128 time=35.8 ms
 8 64 bytes from 192.168.1.102: icmp_seq=6 ttl=128 time=46.6 ms
 9 64 bytes from 192.168.1.102: icmp_seq=7 ttl=128 time=67.5 ms
10 64 bytes from 192.168.1.102: icmp_seq=8 ttl=128 time=14.0 ms
11 64 bytes from 192.168.1.102: icmp_seq=9 ttl=128 time=110 ms
12 64 bytes from 192.168.1.102: icmp_seq=10 ttl=128 time=33.6 ms
13 64 bytes from 192.168.1.102: icmp_seq=11 ttl=128 time=52.2 ms
14 ^C
15 --- 192.168.1.102 ping statistics ---
16 11 packets transmitted, 11 received, 0% packet loss, time 10024ms
17 rtt min/avg/max/mdev = 14.050/59.924/110.534/28.619 ms

經過fdisk -l 查詢磁盤掛載狀況,以下所示:

 1 [root@bogon /]# fdisk -l
 2 
 3 Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
 4 Units = sectors of 1 * 512 = 512 bytes
 5 Sector size (logical/physical): 512 bytes / 512 bytes
 6 I/O size (minimum/optimal): 512 bytes / 512 bytes
 7 Disk label type: dos
 8 Disk identifier: 0x000bfc23
 9 
10    Device Boot      Start         End      Blocks   Id  System
11 /dev/sda1   *        2048      411647      204800   83  Linux
12 /dev/sda2          411648    25593855    12591104   8e  Linux LVM
13 
14 Disk /dev/mapper/centos-root: 10.7 GB, 10737418240 bytes, 20971520 sectors
15 Units = sectors of 1 * 512 = 512 bytes
16 Sector size (logical/physical): 512 bytes / 512 bytes
17 I/O size (minimum/optimal): 512 bytes / 512 bytes
18 
19 
20 Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
21 Units = sectors of 1 * 512 = 512 bytes
22 Sector size (logical/physical): 512 bytes / 512 bytes
23 I/O size (minimum/optimal): 512 bytes / 512 bytes

歸檔命令

歸檔命令tar,也能夠進行解壓縮,以下所示:

經過tar -czvf 進行壓縮成gz格式

1 [root@bogon hexx]# tar -czvf xxx.gz file2.txt file5.txt file6.txt
2 file2.txt
3 file5.txt
4 file6.txt
5 [root@bogon hexx]# ls
6 file2.txt  file5.txt  file6.txt  xxx.gz

解壓命令,經過tar -zxvf解壓gz格式的文件,以下所示:

 1 [root@bogon hexx]# rm -r file*
 2 rm: remove regular empty file ‘file2.txt’? Y
 3 rm: remove regular empty file ‘file5.txt’? Y
 4 rm: remove regular empty file ‘file6.txt’? Y
 5 [root@bogon hexx]# ls
 6 xxx.gz
 7 [root@bogon hexx]# tar -zxvf xxx.gz
 8 file2.txt
 9 file5.txt
10 file6.txt
11 [root@bogon hexx]# ls
12 file2.txt  file5.txt  file6.txt  xxx.gz

將文件解壓到指定目錄,以下所示:

 1 [root@bogon hexx]# mkdir xxxx
 2 [root@bogon hexx]# ls
 3 file2.txt  file5.txt  file6.txt  xxx.gz  xxxx
 4 [root@bogon hexx]# tar -zxvf xxx.gz -C xxxx
 5 file2.txt
 6 file5.txt
 7 file6.txt
 8 [root@bogon hexx]# ls
 9 file2.txt  file5.txt  file6.txt  xxx.gz  xxxx
10 [root@bogon hexx]# cd xxxx
11 [root@bogon xxxx]# ls
12 file2.txt  file5.txt  file6.txt

tar命令詳解,以下所示:

tar -c 歸檔,-x 解檔,-v顯示操做的全部文件,-f 歸檔普通文件的名稱 -z 調用gzip來進行解壓和壓縮,-j 調用bzip2的方式進行解壓和壓縮,-C 指定解壓到的目錄。

備註

從軍行

做者:楊炯

烽火照西京,心中自不平。 牙璋辭鳳闕,鐵騎繞龍城。 雪暗凋旗畫,風多雜鼓聲。 寧爲百夫長,勝做一書生。
相關文章
相關標籤/搜索