Linux入門基礎——常用命令(三)

1.文件權限管理命令

管理員root和文件所有者可以更改文件權限

 1.1 文件權限管理命令chmod

命令名稱:chmod
命令英文原意:change the permissions mode of a file
命令所在路徑:/bin/chmod
執行權限:所有用戶
語法:chmod [{ugoa}{±=}{rwx}] [文件或目錄]
   chmod [mode=421 ] [文件或目錄]
   -R 遞歸修改 改變目錄權限的同時,會把目錄下所有的文件改成這個權限
功能描述:改變文件或目錄權限

權限的數字表示:
r——4
w——2
x——1
例如:
rwx rw- r–
7  6  4
rwx r-x r-x
7  5  5


u表示文件所有者,g表示文件所屬組, o表示文件的其他人, a表示這三者都是。

代表字符 權限 對文件的含義 對目錄的含義
r 讀權限 可以查看文件內容 可以列出目錄中的內容
w 寫權限 可以修改文件內容 可以在目錄中創建、刪除文件
x 執行權限 可以執行文件 可以進入目錄

權限管理命令:chmod
範例:

[cfp@bogon for]$ chmod g+w testfile
賦予文件testfile所屬組寫權限
[cfp@bogon for]$ chmod -R 777 testdir
修改目錄testfile及其目錄下文件爲所有用戶具 有全部權限

實例1:

[cfp@bogon for]$ ls -l a.sh
-rw-rw-r--. 1 cfp cfp 0 Mar 11 16:51 a.sh
[cfp@bogon for]$ chmod u+x a.sh
[cfp@bogon for]$ ls -l a.sh
-rwxrw-r--. 1 cfp cfp 0 Mar 11 16:51 a.sh
[cfp@bogon for]$ chmod g+w,o-r a.sh
[cfp@bogon for]$ ls -l a.sh
-rwxrw----. 1 cfp cfp 0 Mar 11 16:51 a.sh
[cfp@bogon for]$ chmod o=rwx a.sh
[cfp@bogon for]$ ls -l a.sh
-rwxrw-rwx. 1 cfp cfp 0 Mar 11 16:51 a.sh

實例2:

[cfp@bogon for]$ ls -l b.sh
-rw-rw-r--. 1 cfp cfp 0 Mar 11 16:51 b.sh
[cfp@bogon for]$ chmod 755 b.sh
[cfp@bogon for]$ ls -l b.sh
-rwxr-xr-x. 1 cfp cfp 0 Mar 11 16:51 b.sh
[cfp@bogon for]$ chmod 777 b.sh
[cfp@bogon for]$ ls -l b.sh
-rwxrwxrwx. 1 cfp cfp 0 Mar 11 16:51 b.sh

 1.2 其他文件權限管理命令

命令名稱:chown
命令英文原意:change file ownership
命令所在路徑:/bin/chown
執行權限:所有用戶
語法: chown [用戶] [文件或目錄]
功能描述:改變文件或目錄的所有者
範例:[root@bogon cfp]# chown Liangjie d.txt
    #改變文件d.txt的所有者爲Liangjie

只有管理員root可以改變文件所有者

[root@bogon ~]# useradd Liangjie
[root@bogon ~]# cd /home/cfp
[root@bogon cfp]# ls -l d.txt
-rw-rw-r--. 1 cfp cfp 0 Mar  7 09:16 d.txt
[root@bogon cfp]# chown Liangjie d.txt
[root@bogon cfp]# ls -l  d.txt
-rw-rw-r--. 1 Liangjie cfp 0 Mar  7 09:16 d.txt

命令名稱:chgrp
命令英文原意:change file group ownership
命令所在路徑:/bin/chown
執行權限:所有用戶
語法: chgrp [用戶] [文件或目錄]
功能描述:改變文件或目錄的所屬組
範例:[root@bogon cfp]# chgrp lamp d.txt
    #改變文件longze的所屬組爲Liangjie

[root@bogon cfp]# groupadd lamp    #增加一個所屬組lamp
[root@bogon cfp]# chgrp lamp d.txt
[root@bogon cfp]# ls -l  d.txt
-rw-rw-r--. 1 Liangjie lamp 0 Mar  7 09:16 d.txt

命令名稱:umask
命令英文原意:the user file-creation mask
命令所在路徑:Shell內置命令
執行權限:所有用戶
語法: umask [-S]
    -S 以rwx形式顯示新建文件缺省權限(文件缺省權限指創建文件或目錄時的默認權限)
功能描述:顯示、設置文件的缺省權限
範例:[root@bogon cfp]# umask -S

[root@bogon cfp]# umask -S
u=rwx,g=rx,o=rx
[root@bogon cfp]# mkdir liuyi
[root@bogon cfp]# ls -ld liuyi
drwxr-xr-x. 2 root root 4096 Mar 13 14:52 liuyi
[root@bogon cfp]# touch liu
#因爲liu是一個文件,Linux中默認所有新創建的文件可執行權限都會被刪除。
[root@bogon cfp]# ls -l liu
-rw-r--r--. 1 root root 0 Mar 13 14:48 liu

2.文件搜索命令

 2.1 文件搜索命令find

命令名稱:find
命令所在路徑:/bin/find
執行權限:所有用戶
語法: find [搜索範圍] [匹配條件]
功能描述:文件搜索

$ find /etc -name init
在目錄/etc 中查找文件init
-iname 不區分大小寫
-name 嚴格區分大小寫

[cfp@bogon ~]$ find /home/cfp/text -name a.out
/home/cfp/text/socket/a.out
/home/cfp/text/a.out

 #通配符,只要文件名裏面有'out',就會被顯示出來
[cfp@bogon ~]$ find /home/cfp/text -name *out*   
/home/cfp/text/socket/a.out
/home/cfp/text/Cshell.out
/home/cfp/text/a.out
/home/cfp/text/b.out

$ find / -size +204800
在根目錄下查找大於100MB的文件。Linux中一個數據塊是512字節=0.5k

$ find /home -user Liangjie
在根目錄下查找所有者爲Liangjie的文件
$ find /home -group Liangjie
在根目錄下查找所屬組爲Liangjie的文件

$ find /etc -cmin -5
在/etc下查找5分鐘內被修改過屬性的文件和目錄
-amin 訪問時間 access
-cmin 文件屬性 change
-mmin 文件內容 modify

$ find /etc -size +163840 -a -size 204800
在/etc下查找大於80MB小於100MB的文件
-a 兩個條件同時滿足
-o 兩個條件滿足一個即可

$ find /etc -name inittab -exec ls -l {} ;
在/etc下查找inittab文件並顯示其詳細信息
-exec 無確認
-ok 對搜索到的結果,一個一個進行確認,再執行

[root@bogon cfp]# find /etc -name inittab
/etc/inittab
[root@bogon cfp]# find /etc -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 490 Oct 19 10:16 /etc/inittab

$ find /etc -inum 351351
查找文件id號爲351351的文件

[root@bogon text]# ls -i
958149 a.out  958198 Cshell.c    957139 s
958178 b.out  958187 Cshell.out  958081 socket
[root@bogon text]# find /home/cfp/text -inum 957139 -exec rm {} \;
[root@bogon text]# ls
a.out  b.out  Cshell.c  Cshell.out  socket

$ find /etc -type TYPE 查找文件類型爲TYPE的文件
f爲文件,d爲目錄,l爲軟鏈接文件

[cfp@bogon text]$ ls -l
total 76
-rwxrwxr-x. 1 cfp cfp 22808 Apr  4 16:42 a.out
-rwxrwxr-x. 1 cfp cfp 22968 Apr  4 18:11 b.out
-rw-rw-r--. 1 cfp cfp  2846 Apr  4 18:28 Cshell.c
-rwxrwxr-x. 1 cfp cfp 22976 Apr  4 18:28 Cshell.out
[cfp@bogon text]$ find /home/cfp/text -type f
/home/cfp/text/.serverblock.c.swp
/home/cfp/text/Cshell.out
/home/cfp/text/a.out
/home/cfp/text/b.out
/home/cfp/text/Cshell.c

 2.2 其他文件搜索命令

命令名稱:locate
命令所在路徑:/usr/bin/locate
執行權限:所有用戶
語法:locate 文件名
功能描述:在文件資料庫中查找文件(速度塊),Linux定期會維護一個文件資料庫。但是/tmp目錄下無法搜索到
範例:$ locate -i inittab
   # -i 不區分大小寫
   $ updatedb
   #更新文件資料庫

[cfp@bogon text]$ locate inittab
/etc/inittab
/usr/share/augeas/lenses/dist/inittab.aug
/usr/share/man/zh_CN/man5/inittab.5.gz
/usr/share/vim/vim82/syntax/inittab.vim
#創建一個Liangjie文件,使用locate無法查到它的位置。updatedb之後,就可查看到了
[cfp@bogon text]$ touch Liangjie
[cfp@bogon text]$ locate -i liangjie
[root@bogon text]# updatedb
[root@bogon text]# locate Liangjie
/home/cfp/text/Liangjie

命令名稱:which
命令所在路徑:/usr/bin/which
執行權限:所有用戶
語法:which 命令
功能描述:搜索命令所在絕對路徑及別命信息
範例:$ which ls

[cfp@bogon ~]$ which ls
alias ls='ls --color=auto'
	/usr/bin/ls

命令名稱:whereis
命令所在路徑:/usr/bin/whereis
執行權限:所有用戶
語法:whereis [命令名稱]
功能描述:搜索命令所在絕對路徑及幫助文檔路徑
範例:$ whereis ls

命令名稱:grep
命令所在路徑:/bin/grep
執行權限:所有用戶
語法:grep -iv [指定字串] [文件]
功能描述:在文件中搜尋字串匹配的行並輸出
-a 將binary文件以text文件的方式查找數據,不要忽略二進制數據
-i 不區分大小寫
-v 排除指定字符串
-c 計算查找到字符串的次數
範例:$ grep mysql /root/install.log
   在/root/install.log文件中查找含 mysql 的行
   $ grep -v ^# /etc/inittab
   顯示把以#開頭的行去掉的內容

3.幫助命令

 3.1 Linux 內部命令、外部命令

  Linux命令有內部命令(內建命令)和外部命令之分,內部命令和外部命令功能基本相同,但也有些細微差別。

  內部命令實際上是shell程序的一部分,其中包含的是一些比較簡單的linux系統命令,這些命令由shell程序識別並在shell程序內部完成運行,通常在linux系統加載運行時shell就被加載並駐留在系統內存中。內部命令是寫在bashy源碼裏面的,其執行速度比外部命令快,因爲解析內部命令shell不需要創建子進程。比如:exit,history,cd,echo等。

  外部命令是linux系統中的實用程序部分,因爲實用程序的功能通常都比較強大,所以其包含的程序量也會很大,在系統加載時並不隨系統一起被加載到內存中,而是在需要時纔將其調用內存。通常外部命令的實體並不包含在shell中,但是其命令執行過程是由shell程序控制的。shell程序管理外部命令執行的路徑查找、加載存放,並控制命令的執行。外部命令是在bash之外額外安裝的,通常放在/bin,/usr/bin,/sbin,/usr/sbin…等等。可通過“echo $PATH”命令查看外部命令的存儲路徑,比如:ls、vi等。

PS:可以用type 命令查看命令是否是內置命令:

[root@localhost account]# type cd
cd is a shell builtin

 3.2 man、help

命令名稱:man
命令所在路徑:/usr/bin/man
執行權限:所有用戶
語法:man [命令或配置文件]
功能描述:獲得幫助信息
範例:$ man ls 查看ls命令的幫助信息
   #可以使用[ /+要搜索的關鍵詞]來標記搜索,瀏覽方式類似於命令 less
   $ man services
   #查看配置文件services的幫助信息

PS小貼士:使用 “ 命令 --help ”,會只出現關於這條命令的參數信息

[cfp@bogon ~]$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/touch>
or available locally via: info '(coreutils) touch invocation'

命令名稱:help
命令所在路徑:Shell 內置命令
執行權限:所有用戶
語法:help 命令
功能描述:獲得Shell內置命令的幫助信息
範例:$ help if
   #查看 if 的使用,包括while、case等

[cfp@bogon ~]$ help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
    Execute commands based on conditional.
    
    The `if COMMANDS' list is executed.  If its exit status is zero, then the
    `then COMMANDS' list is executed.  Otherwise, each `elif COMMANDS' list is
    executed in turn, and if its exit status is zero, the corresponding
    `then COMMANDS' list is executed and the if command completes.  Otherwise,
    the `else COMMANDS' list is executed, if present.  The exit status of the
    entire construct is the exit status of the last command executed, or zero
    if no condition tested true.
    
    Exit Status:
    Returns the status of the last command executed.


4.用戶管理命令

命令名稱:useradd
命令所在路徑:/usr/sbin/useradd
執行權限:root
語法:useradd 用戶名
功能描述:添加新用戶
範例:$ useradd Liangjie

每個人可以更改自己的密碼,root可以更改任何人的密碼

命令名稱:passwd
命令所在路徑:/usr/bin/passwd
執行權限:所有用戶
語法:passwd 用戶名
功能描述:設置用戶密碼
範例:$ passwd Liangjie

命令名稱:who
命令所在路徑:/usr/bin/who
執行權限:所有用戶
語法:who
功能描述:查看登錄用戶信息
範例:$ who

[root@bogon cfp]# who
liangjie pts/1        2020-04-05 18:08 (192.168.182.1)
參數信息:
登錄用戶名   登錄終端:tty表示本地終端、pts遠程終端   登陸時間

命令名稱:w
命令所在路徑:/usr/bin/w
執行權限:所有用戶
語法:w
功能描述:查看登錄用戶詳細信息
範例:$ w

[cfp@bogon ~]$ sudo su
[sudo] password for cfp: 
[root@bogon cfp]# useradd liangjie
[root@bogon cfp]# systemctl start sshd.service  #開啓遠程連接服務
[root@bogon cfp]# who
cfp      tty2         2020-04-05 17:01 (tty2)
liangjie pts/1        2020-04-05 18:08 (192.168.182.1)
[root@bogon cfp]# w
 18:11:12 up  1:11,  2 users,  load average: 0.05, 0.05, 0.01
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
cfp      tty2      17:01    1:11m  0.28s  0.20s /usr/libexec/gnome-session-binary
liangjie pts/1     18:08    2:45   0.15s  0.15s -bash

5. 總結

  文章總結了常見的文件權限管理命令、文件搜索命令、幫助命令、用戶管理命令。寫的都是一些常見的,用的比較多的命令,包括參數什麼的…命令不在多,而在於要多練,熟能生巧這句話是很有道理的。與其說買一本幾百頁的Linux書裝模做樣的看,不如實實在在自己動手練習幾個命令,就我這個Linux初學者而言,我覺得一點一點的敲命令這個辦法,讓很多Linux常見命令很容易記住的。

    在這裏插入圖片描述

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