1. 程式人生 > 實用技巧 >linux檔案和目錄(二)

linux檔案和目錄(二)

rm命令

rm 刪除檔案,刪除時會提示是否刪除。預設會附帶選項-i。使用/usr/bin/rm就不會有提示。
rm -f 強制刪除沒有任何提示,包括錯誤資訊也不提示。
rm -r 刪除目錄
rm -v 刪除時可以看到刪除的過程

語法
rm -rfv [目錄]

環境變數PATH

如果命令在$PATH定義的路徑下,可以在任何路徑執行。
臨時在PATH下新增路徑:PATH= PATH下新增路徑:PATH=PATH下新增路徑:PATH=PATH:目錄
永久在$PATH下新增路徑:vi /etc/profile
或
echo "PATH=$PATH:/usr/local/mysql/bin/" >> /etc/profile
source /etc/profile

cp命令(拷貝)

cp 複製原始檔至目標檔案,預設附帶-i選項,提示是否覆蓋目標檔案。
cp -r 複製源目錄至目標目錄
約定:凡是目錄尾部都加上/
當目標目錄存在,源目錄會複製到目標目錄下做為他的子目錄

[root@centos ~]# cp /etc/passwd /root/1.txt 
[root@centos ~]# cp /etc/passwd /root/1.txt  若1.txt有你內容會提示你是否覆蓋
cp: overwrite ‘/root/1.txt’? y

mv命令

mv 把原始檔移動至目標位置(預設附加-i選擇)
如果目標是一個目錄,將直接移動至這個目錄下
如果目標是一個檔案且不存在,將被視為移動並改名。
如果目標是一個檔案且存在,將提示覆蓋檔案(直接使用/usr/bin/rm 命令將不提示覆蓋)。
如果源是一個目錄,目標目錄存在將會把源目標移動至目標目錄之下。
如果源是一個目錄,目標目錄不存在被視為移動並改名。

文件檢視cat more less head tail

cat命令

cat 檔案 檢視檔案內容
tac 檔案 倒序檢視檔案內容
cat -A 顯示所有字元,包括換行,結束。等隱藏符號。
cat -n 顯示行號

more命令

more 檔案 分屏顯示檔案內容。
顯示時按“空格” 向下翻頁,看完全部自動退出
顯示時按“ctrl+b"向向上翻頁

less命令

less 命令 分屏顯示檔案內容。看完後不自動退出。按q鍵退出。
顯示時按上下箭頭一行一行的檢視。
和more的翻頁方式相同。
向下查詢功能“/”: /查詢內容 #會將查詢內容高亮。
查詢下一個相同內容 按字母“n”鍵。查詢上一個相同內容shift+n
向上查詢功能“?”: ?查詢內容 #會將查詢內容高亮。
查詢上一個相同內容 按字母“n”鍵。查詢下一個相同內容shift+n
shift+g 定位至檔案未尾
按一下g 定位至檔案開頭

head&tail

head 顯示檔案開頭10行內容
head -n 顯示檔案開頭n行內容
tail 顯示檔案倒數10行內容
tail -n 顯示檔案未尾n行內容
tail -f 動態顯示檔案未尾10行內容

chomd命令

用ls -l +檔案命令列出每個位置是什麼

[root@linux1 ~]# touch 1.txt 建立一個檔案
[root@linux1 ~]# ls -l 1.txt
總用量 20
-rw-r--r--. 1 root root     0 8月  29 11:26 1.txt
  • 第一位開頭是檔案型別,後面九位都是許可權位
  • 許可權位後面有一個(.)受至於selinux,關閉selinux在建立檔案目錄就沒有(.)
  • 在九位許可權位。前三位便是所有者許可權拿上面1.txt檔案(rw-)中間三位是所屬組許可權(r–)最後三位是其他使用者許可權(r–)

解釋rwx

r 可讀 用數字表示4

w可寫 用數字表示2

x 可執行 數字表示1

  • 許可權可以用數字表示,比如777 所有者許可權(rwx)所屬組(rwx)其他使用者(rwx)都是可以讀寫執行
  • 怎麼得到777就是把三個字母用數字表示來相加

更改許可權命令chmod

  • 語法chmod 777 +檔案或者目錄名字
[root@linux1 ~]# chmod 777 1.txt 
[root@linux1 ~]# ls -l 1.txt 
-rwxrwxrwx. 1 root root 0 8月  29 11:26 1.txt

chmod -R 操作只對目錄或檔案生效。需要目錄以及子檔案全部生效就需要加上-R

[root@linux1 ~]# mkdir 123
[root@linux1 ~]# touch 123/1.txt
[root@linux1 ~]# touch 123/2.txt
[root@linux1 ~]# ls -l
總用量 20
drwxr-xr-x. 2 root root    32 8月  29 11:44 123
[root@linux1 ~]# ls -l 123/
總用量 0
-rw-r--r--. 1 root root 0 8月  29 11:43 1.txt
-rw-r--r--. 1 root root 0 8月  29 11:44 2.txt

[root@linux1 ~]# chmod -R 777 123/
[root@linux1 ~]# ls -l
總用量 20
drwxrwxrwx. 2 root root    32 8月  29 11:44 123
[root@linux1 ~]# ls -l 123/
總用量 0
-rwxrwxrwx. 1 root root 0 8月  29 11:43 1.txt
-rwxrwxrwx. 1 root root 0 8月  29 11:44 2.txt

chown命令更改所有者和所屬組

  • 用法chown 所有者:所屬組 檔案或者目錄
  • 修改1.txt檔案的所有者 也支援同時更改所有者和所屬組
[root@linux1 ~]# useradd centos 建立一個使用者
[root@linux1 ~]# ls -l 1.txt 
-rwxrwxrwx. 1 root root 0 8月  29 11:26 1.txt 所有者和所屬組都屬於root

[root@linux1 ~]# chown centos:root 1.txt  更改所有者
[root@linux1 ~]# ls -l 1.txt 
-rwxrwxrwx. 1 centos root 0 8月  29 11:26 1.txt 更改成功

[root@linux1 ~]# chown centos:centos 1.txt 更改所屬組
[root@linux1 ~]# ls -l 1.txt 
-rwxrwxrwx. 1 centos centos 0 8月  29 11:26 1.txt
  • chonw -R同時可以更改目錄與子目錄或檔案的所有者所屬組
[root@linux1 ~]# ls -l
總用量 20
drwxrwxrwx. 2 root   root      32 8月  29 11:44 123

[root@linux1 ~]# ls -l 123/
總用量 0
-rwxrwxrwx. 1 root root 0 8月  29 11:43 1.txt
-rwxrwxrwx. 1 root root 0 8月  29 11:44 2.txt
更改所有者和所屬組
[root@linux1 ~]# ls -l
總用量 20
drwxrwxrwx. 2 centos centos    32 8月  29 11:44 123

[root@linux1 ~]# ls -l 123/
總用量 0
-rwxrwxrwx. 1 root root 0 8月  29 11:43 1.txt
-rwxrwxrwx. 1 root root 0 8月  29 11:44 2.txt

umask命令

  • linux建立目錄和檔案的預設許可權值是怎麼來的?執行下umask
[root@linux1 ~]# umask
0022
  • 目錄最高許可權是777
  • 檔案最高許可權是666
  • 預設值是怎麼由來
當前用umask檢視的出值為0022
目錄(最高許可權為777) 777-022=755 所以建立目錄預設值為755
檔案(最高許可權為666) 666-022=644 所以建立檔案預設值為644
  • 當然也可以更改umask值
[root@linux1 ~]# umask 0002
[root@linux1 ~]# umask
0002
這時候我們建立的目錄預設值為775,檔案為664
  • 這個規律邏輯不一定是準確的
  • 假如把umask設定0003,按之前檔案演算法666-003=663,實際測試下
[root@linux1 ~]# umask 0003
[root@linux1 ~]# touch 3.txt
[root@linux1 ~]# ls -l 3.txt 
-rw-rw-r--. 1 root root 0 8月  29 12:07 3.txt
  • 之前演算法是有漏洞
  • 正確演算法
檔案666(rw-rw-rw-)-003(-------wx)=rw-rw-r-也就是664
目錄777(rwxrwxrwx)-003(rwxrwxr--)=rwxrwxr--也就是774