1. 程式人生 > 實用技巧 >檔案屬性及find命令總結

檔案屬性及find命令總結

第1章 檔案屬性

1.1 檔案的屬性

  1.1.1 檢視檔案的詳細屬性

PS:ls檢視的檔案或目錄預設的是按照名字的第一個字母進行正序排序

ls 引數選項:

-t # 按照時間排序

-r # 逆序排序

按照時間進行逆序排序

 [root@oldboyedu-lnb ~]# ls -lrt /etc    

 [root@oldboyedu-lnb ~]# ls -l

 -rw-r--r--. 1 root root  138 Jul 30 09:34 1.sh

  1.1.2 如何識別linux中的檔案或目錄

a. 使用顏色分辨

b. 使用字尾名 windows

使用的方法 linux的字尾名是給我們自己看的 命令ll 檢視

c. 使用命令來辨別

1.2 檔案屬性每列含義

- rw-r--r-- . 1 root root 138 Jul 30 09:34 1.sh

--- ---------- -- - --- ---- ---------------- ----

1 2 3 4 5 6 7 8

  1.2.1 檔案的型別

什麼是檔案的型別:

普通檔案 -

目錄 d

可執行檔案 x

.mp4 .PDF

    1.2.1.1 windows系統檔案的型別:

特點:

以後綴名來區分的

修改後綴名 無法使用

.exe

.txt

.doc

.pdf

.avi .mp4

...........

    1.2.1.2 Linux系統檔案的型別:

不是以後綴名來區分 字尾名只是給我們自己看的

.txt 普通檔案

.sh shell指令碼

.rpm 安裝包

.jpg 圖片

.py python指令碼

    1.2.1.3 Linux檔案系統型別分類

- 普通檔案

有三種檔案的型別都是使用-來表示

第一種:

- 普通文字檔案 # 使用echo vim vi touch 等建立的檔案

第二種:

- 二進位制檔案 # 系統中的命令

第三種:

- 資料檔案 # .rpm包 壓縮包

d directory 目錄

drwxr-xr-x. 2 root root 6 Jul 30 08:22 3 # 檔案詳細資訊的最前面的第一列 d 代表目錄

l 小寫的L 軟連結檔案 softlink 類似於windows的快捷方式 符號連結

/etc/rc.local -> rc.d/rc.local # 訪問/etc/rc.local---> 真實訪問的是/etc/rc.d/rc.local

c 字元裝置檔案

[root@oldboyedu-lnb ~]# ll /dev/urandom               # 生成隨機數  一直不停的吐
crw-rw-rw- 1 root root 1, 9 May 22  2018 /dev/urandom

PS:tr

-c 取反

-d 刪除

[root@oldboyedu-lnb ~]# tr -cd "a-z0-9A-Z" < /dev/urandom|head -c10
 擴充套件: 使用random生成隨機數 獲取前8個字元
 [root@oldboyedu-lnb ~]# ll /dev/null    # 系統中的黑洞 把任意輸出內容都可以定向到null
 crw-rw-rw- 1 root root 1, 3 May 22  2018 /dev/null    

PS: null的意義 在執行命令的時候 我們不想輸出檢視命令的結果 因為結果太多太長 把結果定向到空 使用的特定的命令來判定上一條命令是否執行成功

[root@oldboyedu-lnb ~]# ping -c4 -W1 www.baidu.com > /dev/null
[root@oldboyedu-lnb ~]# echo $?
0
把ping的結果放入黑洞null 使用$?來判定是否ping的通  上一條命令是否執行成功 0為成功 非0失敗[root@oldboyedu-lnb ~]# ll /etc/hehehe.txt
ls: cannot access /etc/hehehe.txt: No such file or directory
[root@oldboyedu-lnb ~]# echo $?
 2
[root@oldboyedu-lnb ~]# ll /etc/hosts
-rw-r--r--. 1 root root 158 Jul 24 10:18 /etc/hosts
[root@oldboyedu-lnb ~]# echo $?
 0

33689416 - rw-r--r-- . 1 root root 5 Jul 30 11:10 1.txt

-------- ---------- -- -- --- ----- ---------------- ----

1 2 3 4 5 6 7 8 9

1 node index node 索引節點

檔案的許可權 詳細資訊 儲存在inode索引節點 存放著檔案具體的位置

2 檔案的型別

- 普通檔案

- 二進位制

- 資料檔案

d 目錄檔案

l 軟連結檔案

c 字元裝置檔案

b 塊裝置

----------

p 網路套接字

s 管道檔案

3 檔案的許可權 決定了當前的使用者對當前的檔案擁有什麼許可權

r # read 讀 cat file.txt less file.txt more head tail...

w # write 寫入 vi vim cat echo ...

x # execute 可執行 可以執行檔案中的命令 可執行顏色變綠

   如何執行檔案 ./1.txt 或者全路徑方式執行 /root/1.txt

  [root@oldboyedu-lnb ~]# ./1.txt
  -bash: ./1.txt: Permission denied
  [root@oldboyedu-lnb ~]# chmod +x 1.txt
  [root@oldboyedu-lnb ~]# ll 1.txt
  -rwxr-xr-x 1 root root 4 Aug  3 08:16 1.txt
  [root@oldboyedu-lnb ~]# ./1.txt
   /root
  [root@oldboyedu-lnb ~]# cat 1.txt
   pwd
  [root@oldboyedu-lnb ~]# /root/1.txt
  /root

Linux中檔案預設的許可權是 rw-r--r--

為什麼Linux中檔案預設的許可權沒有x許可權

普通文字存放什麼內容: 字串資訊 配置檔案

檔案中有可執行的命令: 不是普通文字 SHLL指令碼 需要有x 執行許可權

4 .開啟了selinux後建立檔案自帶的 關閉selinux點消失

5 數字1 代表了硬連結的個數

什麼是硬連結:

相當於檔案有多個入口 類似於超市有個入口 刪除一個入口 對檔案沒有影響

預設的目錄的硬連結個數為2

[root@oldboyedu-lnb test]# ll -ldi ../test
 17454490 drwxr-xr-x 2 root root 6 Aug  3 08:27 ../test
[root@oldboyedu-lnb test]# ll -lai
total 0
17454490 drwxr-xr-x  2 root root  6 Aug  3 08:27 .
17454451 drwxr-xr-x. 3 root root 44 Aug  3 08:27 ..
[root@oldboyedu-lnb test]# #cp /etc/hosts /root/3/test==== cp /etc/hosts .

6 檔案的屬主

當前的檔案屬於哪個使用者

7 檔案的屬組

當前的檔案屬於哪個組

Access 訪問時間

Modify 修改時間

Change 改變時間

8 檔名稱

第2章 正確與錯誤重定向

Linux給程式提供三種 I/O 裝置

標準輸入(STDIN) -檔案描述符為0 預設接受來自鍵盤的輸入

標準輸出(STDOUT)-檔案描述符為1 預設輸出到終端視窗

標準錯誤(STDERR)-檔案描述符為2 預設輸出到終端視窗

2.1 標準正確輸出重定向

所有命令執行成功後的結果稱為正確的把正確的結果定向到檔案中或者空稱為標準正確輸出重定向.

1> 正確重定向 PS1只接收命令返回的正確的結果 錯誤的結果扔到螢幕

1>> 正確追加重定向 PS: 加1和不加1 都是隻接收正確的結果

1> ======= >

1>> ====== >>

------------------------------- 只接收正確的結果

[root@oldboyedu-lnb ~]# ls 1.sh

1.sh

[root@oldboyedu-lnb ~]# ls 1.sh > ok.txt

[root@oldboyedu-lnb ~]# cat ok.txt

1.sh

[root@oldboyedu-lnb ~]# ls 1.sh >> ok.txt

[root@oldboyedu-lnb ~]# cat ok.txt

1.sh

1.sh

[root@oldboyedu-lnb ~]# ls 1.sh 1> ok.txt

[root@oldboyedu-lnb ~]# cat ok.txt

1.sh

[root@oldboyedu-lnb ~]# ls 1.sh 1>> ok.txt

[root@oldboyedu-lnb ~]# cat ok.txt

1.sh

1.sh    

2.2 標準錯誤輸出重定向

所有命令執行失敗後的結果 稱為錯誤的 把錯誤的結果定向到檔案或者空 稱為標準錯誤輸出重定向

2> 錯誤重定向 # 只接收錯誤的結果 不接收正確的結果

2>> 錯誤追加重定向 # 只追加接收錯誤的結果 不接收正確的結果

&>file 把標準輸出和標準錯誤都重定向到file

&>>file 把標準輸出和標準錯誤都追加重定向到file

1>&2 標準輸出值傳遞給2輸出通道 &2表示2輸出通道

2>&1 標準錯誤值傳遞給1輸出通道 &1表示1輸出通道.

   [root@oldboyedu-lnb ~]# ls 1.txt 2> error.txt
   1.txt
   [root@oldboyedu-lnb ~]# cat error.txt
   [root@oldboyedu-lnb ~]# ls 1.txtt 2> error.txt
   [root@oldboyedu-lnb ~]# cat error.txt
   ls: cannot access 1.txtt: No such file or directory
   [root@oldboyedu-lnb ~]# ls 1.txtt 2>> error.txt
   [root@oldboyedu-lnb ~]# cat error.txt
   ls: cannot access 1.txtt: No such file or directory
   ls: cannot access 1.txtt: No such file or directory

2.3 接收正確和錯誤的結果

方法1

        [root@oldboyedu-lnb ~]# cat error.txt

        [root@oldboyedu-lnb ~]# ls aa.txt >>ok.txt 2>>error.txt

        [root@oldboyedu-lnb ~]# cat error.txt

        ls: cannot access aa.txt: No such file or directory

        [root@oldboyedu-lnb ~]# ls 1.txt >>ok.txt 2>>error.txt

        [root@oldboyedu-lnb ~]# cat ok.txt

        ls: cannot access aa.txt: No such file or directory

       ls: cannot access a.txt: No such file or directory

        1.txt

       1.txt

        [root@oldboyedu-lnb ~]# > ok.txt

        [root@oldboyedu-lnb ~]# ls 1.txt >>ok.txt 2>>error.txt

        [root@oldboyedu-lnb ~]# cat ok.txt

       1.txt

        [root@oldboyedu-lnb ~]# cat error.txt

        ls: cannot access aa.txt: No such file or directory

方法2:

 [root@oldboyedu-lnb ~]# ls 1.txttt >>ok.txt 2>&1
 [root@oldboyedu-lnb ~]# cat ok.txt
 ls: cannot access 1.txttt: No such file or directory

方法3:

    [root@oldboyedu-lnb ~]# ls 1.txt &>>ok.txt

    [root@oldboyedu-lnb ~]# ls 1.txttt &>>ok.txt

    [root@oldboyedu-lnb ~]# cat ok.txt

    1.txt

    ls: cannot access 1.txttt: No such file or directory

2.4 擴充套件

[root@oldboyedu-lnb ~]# sh 1.sh www.baidu.com

網站通

[root@oldboyedu-lnb ~]# sh 1.sh www.baidu.commmm

網站不通

[root@oldboyedu-lnb ~]# cat 1.sh

ping -c1 -W1 $1 &>/dev/null

[ $? -eq 0 ] && echo "網站通" || echo "網站不通"

---------------------------------------

[root@oldboyedu-lnb ~]# ll /dev/zero # 生成大檔案

crw-rw-rw- 1 root root 1, 5 May 22  2018 /dev/zero

擴充套件: 生成1G的檔案

dd if=/dev/zero of=/root/1G bs=10M count=100

b 塊裝置 硬體裝置 磁碟 光碟機 U盤

brw-rw---- 1 root disk 8, 0 May 22 2018 /dev/sda

brw-rw---- 1 root cdrom 11, 0 May 22 2018 /dev/sr0

s 網路套接字

p 管道檔案

第3章 檔案相關的命令

3.1 which # 檢視命令的全路徑

      [root@oldboyedu-lnb ~]# which mkdir

      /usr/bin/mkdir

      [root@oldboyedu-lnb ~]# ll /usr/bin/mkdir

      -rwxr-xr-x. 1 root root 79760 Apr 11  2018 /usr/bin/mkdir

3.2 whereis # 檢視命令的全路徑和命令的幫助檔案位置 瞭解

3.3 file # 檢視檔案的型別

[root@oldboyedu-lnb ~]# file 1.txt
1.txt: ASCII text
[root@oldboyedu-lnb ~]# file 3
 3: directory
[root@oldboyedu-lnb ~]# file all.tar.gz
 all.tar.gz: gzip compressed data, from Unix, last modified: Fri Jul 31 10:42:15 2020
[root@oldboyedu-lnb ~]# file /etc/rc.local
 /etc/rc.local: symbolic link to `rc.d/rc.local' 3) find按照檔案的型別進行查詢檔案  用來查詢檔案 不支援查詢檔案中的內容

3.4 find

find 語法

find 在哪裡找 型別 特點

find 北京 男的 高富帥

find ./ -type f

-type的型別

f 普通檔案

d 目錄

l 軟連結

b 塊裝置

c 字元裝置

`b'    for 512-byte blocks (this is the default if no suffix is used)

`c'    for bytes   (推薦)

`w'    for two-byte words

`k'    for Kilobytes (units of 1024 bytes) (推薦)

`M'   for Megabytes (units of 1048576 bytes) (推薦)

`G'   for Gigabytes (units of 1073741824 bytes)

  3.4.1 精確查詢:

   find 路徑資訊 -type 檔案型別 -name "檔名"

  3.4.2 模糊查詢:

   find 路徑資訊 -type 檔案型別 -name "檔名*"

  find 路徑資訊 -type 檔案型別 -name "*檔名"

   find 路徑資訊 -type 檔案型別 -name "文*件名"

  3.4.3 忽略大小寫查詢:

   find 路徑資訊 -type 檔案型別 -iname "檔名"

    3.4.3.1 查詢當前目錄的所有普通檔案 預設會顯示隱藏的檔案

   [root@oldboyedu-lnb 3]# find ./ -type f

    ./1.txt

    ./2.txt

    ./3.txt

    ./4.txt

    ./5.txt

    ./6.txt

    ./7.txt

    ./8.txt

    ./9.txt

    ./10.txt

       

    3.4.3.2 查詢當前目錄下所有的目錄檔案

  [root@oldboyedu-lnb 3]# find /root/3/ -type d
  /root/3/
  /root/3/stu01
  /root/3/stu02
  /root/3/stu03
  /root/3/stu04
  /root/3/stu05
  /root/3/stu06
   /root/3/stu07
   /root/3/stu08
   /root/3/stu09
   /root/3/stu10

    3.4.3.3 查詢當前目錄下的oldboy.txt

   -name #按照名稱精確查詢

 [root@oldboyedu-lnb 3]# find ./ -type f -name "oldboy.txt"
  ./oldboy.txt

   -name #按照名稱模糊查詢

 [root@oldboyedu-lnb 3]# find ./ -type f -name "*.txt"
 ./1.txt
 ./2.txt
 ./3.txt
 ./4.txt
 ./5.txt
 ./6.txt
 ./7.txt
 ./8.txt
 ./9.txt
 ./10.txt
./oldboy.txt
[root@oldboyedu-lnb 3]# find ./ -type d -name "stu*" ./stu01 ./stu02 ./stu03 ./stu04
./stu05 ./stu06 ./stu07 ./stu08 ./stu09
./stu10

    3.4.3.4 -iname 不區分大小來查詢檔案

 [root@oldboyedu-lnb 3]# find ./ -type f -iname "*.txt"
   ./1.txt
   ./2.txt
   ./3.txt
   ./4.txt
   ./5.txt
   ./6.txt
   ./7.txt
   ./8.txt
   ./9.txt
   ./10.txt
   ./oldboy.txt
    ./1.TXT
    ./2.TXT
    ./3.TXT

find 預設的是查詢所有的目錄及目錄下面的所有檔案

-maxdepth 深度等級

[root@oldboyedu-lnb 3]# touch stu{1..3}/{bbs,blog,www}.conf

[root@oldboyedu-lnb 3]# find ./ -maxdepth 1 -type f
./1.txt

./2.txt

 ./3.txt

[root@oldboyedu-lnb 3]# find ./ -maxdepth 2 -type f
./1.txt
./2.txt
 ./3.txt
./stu1/bbs.conf
./stu1/blog.conf
./stu1/www.conf
./stu2/bbs.conf
./stu2/blog.conf
./stu2/www.conf
./stu3/bbs.conf
./stu3/blog.conf
./stu3/www.conf
[root@oldboyedu-lnb 3]# find stu{1..3}/ -maxdepth 1 -type f
stu1/bbs.conf
stu1/blog.conf
stu1/www.conf
stu2/bbs.conf
stu2/blog.conf
stu2/www.conf
stu3/bbs.conf
stu3/blog.conf
stu3/www.conf

    3.4.3.5 按照檔案的大小查詢

-size -k -M -G

a.在當前目錄查詢大於1M的檔案

 [root@oldboyedu-lnb 3]# find ./ -type f -size +1M

 ./all.txt

 [root@oldboyedu-lnb 3]# find ./ -type f -size +500k

 ./services

 ./all.txt

  [root@oldboyedu-lnb ~]# find ./ -type f -size +1G

  ./1G

b.查詢/root目錄下 大於1M並且小於2G的檔案

[root@oldboyedu-lnb ~]# find /root/ -type f -size +1M -and  -size -3G
/root/3/all.txt
/root/1G

-and 並且 並集關係 預設就是並且

find執行的結果呼叫

第4章 檔案的型別

4.1 使用find按照型別查詢檔案

-type f d l #

find按名字查詢

-name file.txt

模糊查詢

-name "*.txt"

-name "stu.*"

-name "*.*"

不區分大小

-iname "*.txt"

按照深度等級查詢

--maxdepth 1

find ./ -maxdepth 1 -type f

按照檔案的大小查詢

-size + 大於多少 - 小於多少

find ./ -type f -size +1G

-and 並且 兩端同時成立 -o or 或者

  [root@oldboyedu-lnb ~]# find ./ -type f -name "*.bak" -size +1G
   ./1.bak
   -o 或者
  [root@oldboyedu-lnb ~]# find ./ -type f -name "*.txt" -o -size +1G
  ./3/1.txt
  ./3/2.txt
  ./2.txt
  ./all.txt
  ./error.txt
  ./1.txt
  ./1.bak
  ./ok.txt

4.2 find命令補充

PS:注意中文語法的問題

find可以查詢檔案目錄

對查詢到的檔案或者目錄做cp ls mv rm操作

只要是輸出到螢幕上的內容稱為普通字串 只有用命令呼叫普通字串的時候 才稱為檔案或目錄名稱

4.3 xargs

作用:可以把其他命令輸出的字串資訊 傳遞到其他命令的最後面 來當做檔案或目錄名稱.

語法格式:

命令 [引數選項] file|xargs 命令 最後面這個位置

示例:

[root@oldboyedu-lnb 3]# echo 1.txt 2.txt|xargs ls -l
-rw-r--r-- 1 root root 0 Aug  3 08:20 1.txt
 -rw-r--r-- 1 root root 0 Aug  3 08:20 2.txt
示例:
[root@oldboyedu-lnb 3]# echo 111 >1.txt
[root@oldboyedu-lnb 3]# echo 222 >2.txt
[root@oldboyedu-lnb 3]# echo 1.txt 2.txt|xargs cat
111
222

4.4 find結合xargs find不支援別名

  4.4.1 把find查詢到的檔案 交給ls命令

[root@oldboyedu-lnb 3]# find ./ -type f|xargs ls -l
 -rw-r--r-- 1 root root 4 Aug  3 08:57 ./1.txt
 -rw-r--r-- 1 root root 4 Aug  3 08:57 ./2.txt
 -rw-r--r-- 1 root root 0 Aug  3 08:41 ./hehe.

  4.4.2 把find查詢的檔案交給 rm 命令 find不支援別名 所以 rm慎用

[root@oldboyedu-lnb 3]# find ./ -type f -name "1.txt"|xargs rm

  4.4.3 把find查詢到的檔案 交給 mv命令

格式:

[root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs mv 1.bak 1.txt  # 錯誤的使用方式
[root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs –i(使用大括號要用這個) mv {} /tmp/1.bak    # 正確的格式
[root@oldboyedu-lnb 3]# find ./ -type f -name 1.txt|xargs -i mv {} /tmp/1.bak
[root@oldboyedu-lnb 3]# ll 1.txt /tmp/1.bak
 ls: cannot access 1.txt: No such file or directory
 -rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/1.bak

  4.4.4 把find查詢到的檔案 交給 cp命令

   [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt|xargs cp -t /tmp/

    [root@oldboyedu-lnb 3]# ll 2.txt /tmp/2.txt

    -rw-r--r--  1 root root 0 Aug  3 09:21 2.txt

    -rw-r--r--. 1 root root 0 Aug  3 09:28 /tmp/2.txt

    [root@oldboyedu-lnb 3]# find ./ -type f -name 3.txt|xargs -i cp {} /tmp

    [root@oldboyedu-lnb 3]# ll 3.txt /tmp/3.txt

    -rw-r--r--  1 root root 0 Aug  3 09:21 3.txt

    -rw-r--r--. 1 root root 0 Aug  3 09:29 /tmp/3.txt 

4.4.5 結合grep過濾查詢到檔案的字串

  [root@oldboyedu-lnb 3]# find ./ -type f

    ./hehe.

    ./2.txt

    ./3.txt

    ./4.txt

    ./5.txt

    ./6.txt

    ./7.txt

    ./8.txt

    ./9.txt

    ./10.txt

    [root@oldboyedu-lnb 3]# find ./ -type f |xargs grep oldboy

    ./10.txt:oldboy

PS: grep 預設只過濾當前目錄 遞迴過濾檔案內容使用-R

 [root@oldboyedu-lnb 3]# grep -R oldboy ./*
  ./test/10.txt:oldboy 

---------------- 批量查詢檔案 批量改名

find ./ -type f |xargs -i cp {} {}.bak

4.5 find查詢到的內容交給其他命令

命令格式:

find ./ -type -name 1.txt -exec 命令 {} \;

  4.5.1 find的結果交給ls 檢視

[root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec ls -l {} \;
-rw-r--r-- 1 root root 0 Aug  3 09:21 ./2.txt

  4.5.2 find的結果交給rm 使用

 [root@oldboyedu-lnb 3]# find ./ -type f -name 2.txt -exec rm {} \;
 [root@oldboyedu-lnb 3]# ll 2.txt
 ls: cannot access 2.txt: No such file or directory

  4.5.3 find結果交給mv 使用

 [root@oldboyedu-lnb 3]# find ./ -type f -name 3.txt -exec mv {} /tmp/3.bak \;
 [root@oldboyedu-lnb 3]# ll 3.txt /tmp/3.bak
 ls: cannot access 3.txt: No such file or directory
 -rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/3.bak

  4.5.4 把find的結果交給cp使用

[root@oldboyedu-lnb 3]# find ./ -type f -name 4.txt -exec cp {} /tmp/ \;
[root@oldboyedu-lnb 3]# ll 4.txt /tmp/4.txt
-rw-r--r--  1 root root 0 Aug  3 09:21 4.txt
-rw-r--r--. 1 root root 0 Aug  3 09:42 /tmp/4.txt

  4.6 find查詢的內容交給其他的命令

語法格式:

命令 `find ./ -type f -name`` # 反引號中執行後的結果 留在原地 供其他命令使用

  4.6.1 find的結果交給ls檢視命令列支援別名

[root@oldboyedu-lnb 3]# ll `find ./ -type f`
-rw-r--r-- 1 root root 0 Aug  3 09:21 ./4.txt

  4.6.2 把find 結果交給rm

[root@oldboyedu-lnb 3]# rm -f `find ./ -type f -name 4.txt`
[root@oldboyedu-lnb 3]# ll 4.txt
ls: cannot access 4.txt: No such file or directory

  4.6.3 把find的結果交給mv

[root@oldboyedu-lnb 3]# mv `find ./ -type f -name 5.txt` /tmp/5.bak
[root@oldboyedu-lnb 3]# ll 5.txt /tmp/5.bak
ls: cannot access 5.txt: No such file or directory
-rw-r--r-- 1 root root 0 Aug  3 09:21 /tmp/5.bak

  4.6.4 把find的結果交給cp

 [root@oldboyedu-lnb 3]# \cp -r `find ./ -type d` /tmp/test

PS: ;分號在命令列中有特殊含義 執行多條命令使用 不管前面的命令是否執行成功都會繼續執行用來分隔命令

 [root@oldboyedu-lnb 3]# ls -l;mkdir oldboy;touch oldboy/test.txt
  total 0
  -rw-r--r-- 1 root root  0 Aug  3 09:21 6.txt
  -rw-r--r-- 1 root root  0 Aug  3 09:21 7.txt
  -rw-r--r-- 1 root root  0 Aug  3 09:21 8.txt
  -rw-r--r-- 1 root root  0 Aug  3 09:21 9.txt
  -rw-r--r-- 1 root root  0 Aug  3 08:41 hehe.
  drwxr-xr-x 2 root root 20 Aug  3 09:32 test
  [root@oldboyedu-lnb 3]# ll oldboy/
  total 0
  -rw-r--r-- 1 root root 0 Aug  3 10:10 test.txt

-----命令執行失敗 繼續執行後面的命令

[root@oldboyedu-lnb 3]# lll -l;mkdir alex;touch lidao/hehe.txt
-bash: lll: command not found
 touch: cannot touch ‘lidao/hehe.txt’: No such file or directory

  4.6.5 &&命令 # 前面的命令必須執行成功才執行後面的命令

示例1:

 [root@oldboyedu-lnb 3]# mkdir test && mkdir alex
 [root@oldboyedu-lnb 3]# ll
  total 0
  drwxr-xr-x 2 root root 6 Aug  3 10:13 alex
  drwxr-xr-x 2 root root 6 Aug  3 10:13 test

示例2:

 [root@oldboyedu-lnb 3]# mkdir oldboy
 [root@oldboyedu-lnb 3]# ll
  total 0
  drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
  [root@oldboyedu-lnb 3]# mkdir oldboy && mkdir ale
  mkdir: cannot create directory ‘oldboy’: File exists
  [root@oldboyedu-lnb 3]# ll
  total 0
  drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
  示例3:
  [root@oldboyedu-lnb 3]# ls -ld oldboy && echo hehe
  drwxr-xr-x 2 root root 6 Aug  3 10:14 oldboy
  hehe

  4.6.6 ||命令 # 前面的命令必須執行失敗 才執行後面的命令

 [root@oldboyedu-lnb 3]# ls test.txt || echo hehe
 ls: cannot access test.txt: No such file or directory
 hehe
 [root@oldboyedu-lnb 3]# ls test.txt || echo hehe && ls hehe.txt || echo ok
 ls: cannot access test.txt: No such file or directory
 hehe
 ls: cannot access hehe.txt: No such file or directory
 ok

示例:

[root@oldboyedu-lnb 3]# cd alex|| mkdir alex
 -bash: cd: alex: No such file or directory
[root@oldboyedu-lnb 3]# ll
total 0
drwxr-xr-x 2 root root 6 Aug  3 10:18 alex

第5章 檔案壓縮

5.1 windows的壓縮方式 .rar .rar5 .zip # 提前安裝rar壓縮軟體

5.2 Linux的壓縮方式:

命令格式:

命令 [引數選項] 壓縮包的名稱.tar.gz 檔案/目錄

tar -zcvf test.tar.gz /etc/hosts

命令 引數選項 筐子 水果/香蕉/荔枝

tar -zcvf test.tar.gz /etc/hosts 1.txt /tmp/test.txt

引數選項:

z # 使用gzip方式壓縮

c # create 建立壓縮包

v # verbose 顯示壓縮的過程

f # 指定檔案

x # 解壓

t # 檢視壓縮包的檔名稱

C # 指定解壓到哪裡

-P # 不提示從成員中刪除/ PS: 進入到相對路徑打包 不提示

--exclude # 排除檔案 --exclude=file

--exclude-from # 排除檔案中的檔名 --exclude-from=file

示例1: 把/etc/hosts檔案打包成test.tar.gz

[root@oldboyedu-lnb ~]# tar zcvf hosts.tar.gz /etc/hosts
tar: Removing leading `/' from member names
/etc/hosts

示例2: 不顯示壓縮過程

[root@oldboyedu-lnb ~]# touch 1.txt 2.txt
[root@oldboyedu-lnb ~]# tar zcf test.tar.gz {1..2}.txt
-rw-r--r-- 1 root root 116 Aug  3 10:51 test.tar.gz

示例3: 同時壓縮打包壓縮多個檔案

 [root@oldboyedu-lnb ~]# tar zcvf all.tar.gz *.txt /etc/hosts /etc/passwd
 1.txt
 2.txt
 tar: Remov  ing leading `/' from member names
 /etc/hosts
 /etc/passwd

不顯示刪除/

[root@oldboyedu-lnb ~]# tar zcvfP all.tar.gz *.txt /etc/hosts /etc/passwd
1.txt
2.txt
/etc/hosts
/etc/passwd

示例4: 指定壓縮包的位置

[root@oldboyedu-lnb ~]# tar zcvf /opt/all.tar.gz 1.txt 2.txt
1.txt
2.txt
[root@oldboyedu-lnb ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 116 Aug  3 10:57 all.tar.gz

示例5:如何解壓 xf

[root@oldboyedu-lnb ~]# tar xf hosts.tar.gz 

示例6: 解壓到固定的目錄

[root@oldboyedu-lnb ~]# tar xf all.tar.gz -C /opt/
[root@oldboyedu-lnb ~]# ll /opt/
total 4
-rw-r--r-- 1 root root   0 Aug  3 10:51 1.txt
-rw-r--r-- 1 root root   0 Aug  3 10:51 2.txt
-rw-r--r-- 1 root root 116 Aug  3 10:57 all.tar.gz
 drwxr-xr-x 2 root root  33 Aug  3 10:59 etc

示例7: 檢視壓縮包裡面的檔案或目錄名稱 tf

[root@oldboyedu-lnb ~]# tar tf hosts.tar.gz
etc/hosts
[root@oldboyedu-lnb ~]# tar tf test.tar.gz
1.txt
2.txt
[root@oldboyedu-lnb ~]# echo hehe > 1.txt
[root@oldboyedu-lnb ~]# tar tf test.tar.gz |xargs cat
hehe
[root@oldboyedu-lnb ~]# cat 1.txt 2.txt

示例8: 排除壓縮某個檔案 exclude 排除

打包當前目錄所有的檔案

[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./*
 ./1.txt
 ./2.txt
 ./etc/
 ./etc/hosts

排除當前目錄的1.txt

 [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude=1.txt
 ./2.txt
 ./etc/
 ./etc/hosts
[root@oldboyedu-lnb ~]# tar tf exclude.tar.gz
 ./2.txt
 ./etc/
 ./etc/hosts    

排除當前目錄的1.txt和2.txt

 [root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude=1.txt --exclude=2.txt
 ./etc/
 ./etc/hosts
 [root@oldboyedu-lnb ~]# ll
 total 4
  -rw-r--r-- 1 root root  5 Aug  3 11:02 1.txt
  -rw-r--r-- 1 root root  0 Aug  3 10:51 2.txt
  -rw-r--r-- 1 root root  0 Aug  3 11:13 alex.txt
  drwxr-xr-x 2 root root 19 Aug  3 10:56 etc
  -rw-r--r-- 1 root root  0 Aug  3 11:13 oldboy.txt
[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude={1,alex}.txt
  ./2.txt
  ./etc/
  ./etc/hosts
  ./oldboy.txt

排除多個檔案: 可以把多個檔名稱 寫入到文字中 排除文字中的檔案

第一步: 排除的檔名寫入exclude.txt

[root@oldboyedu-lnb ~]# echo -e "1.txt\nalex.txt\noldboy.txt" > exclude.txt
[root@oldboyedu-lnb ~]# cat exclude.txt
1.txt
alex.txt
oldboy.txt

第二步: 打包排除 --exclude-from=file.txt

[root@oldboyedu-lnb ~]# tar zcvf exclude.tar.gz ./* --exclude-from=exclude.txt
./2.txt
./all.tar.gz
 ./etc/
 ./etc/hosts
 ./exclude.txt      

PS: xargs -n 輸出的內容顯示n

[root@oldboyedu-lnb ~]# echo {1..10}|xargs -n3
 1 2 3
 4 5 6
 7 8 9
 10
[root@oldboyedu-lnb ~]# echo {1..10}|xargs -n4
1 2 3 4
 5 6 7 8
9 10

第6章 檔案許可權

33575026 -rw-r--r-- 1 root root 5 Aug 3 11:02 1.txt

rw-r--r-- # 9位許可權位 三位一組

wx # 三位最大的許可權

6.1 前三位 # 屬主的許可權位 檔案的主人 檔案的建立者對檔案的許可權

rw-

r 可讀

w 可寫

- 沒有許可權

最大許可權 rwx

類似於 筆記本(檔案) 屬於 張三的

當前的1.txt檔案是屬於root使用者的

6.2 中三位 # 屬組的許可權位 在組內的成員對我的檔案擁有什麼許可權

r--

r 可讀

- 沒有許可權

- 沒有許可權

類似於 筆記本屬於張三 也屬於某個組 張三的家庭

6.3 後三位 # 其他使用者的許可權位 對於張三來說 陌生人擁有什麼許可權

不是屬主 也不在組內的人 對檔案的許可權

r-- 可讀

- 沒有許可權

- 沒有許可權

類似於 張三不認識的人 也不在組內的成員 對筆記本的許可權

PS:組的許可權和其他使用者的許可權預設相同

rwx 相對應的數字來表示許可權

r read----> 4

w write---> 2

x write---> 1

rw-r--r-- # 使用數字來表示許可權

每個許可權位相加 rw-

把所有位置的數字組合在一起

r(4)w(2)-(0)===6 # 屬主 前三位相加

r(4)-(0)-(0)===4 # 屬組 中三位相加

r(4)-(0)-(0)===4 # 其他 後三位相加

許可權組合在一起 644 # Linux檔案預設的許可權

目錄的許可權: 預設的許可權755

rwxr-xr-x

rwx=7

r-x=5

r-x=5

6.4 檔案的屬主

rw-r--r-- 1 root root 5 Aug 3 11:02 1.txt

----- ------

1 2

1.檔案的屬主 檔案的主人 檔案的建立者

6.5 檔案的屬組

2.檔案的屬組 使用者組 對檔案的許可權

6.6 練習題:

  6.6.1 在oldboy目錄下建立lidao1..lidao5 5個目錄

[root@oldboyedu-lnb~]# mkdir  /oldboy/lidao{1..5}

drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao1

drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao2

drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao3

drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao4

drwxr-xr-x  2 root root    6 Aug  3 15:52 lidao5

 6.6.2 在oldboy目錄下建立alex1.txt aelx2.txt 然後在5個目錄下建立2個檔案 www.conf bbs.conf

[root@oldboyedu-lnb~]#  touch  /oldboy/alex{1..2}.txt
-rw-r--r--  1 root root    0 Aug  3 15:57 alex1.txt
-rw-r--r--  1 root root    0 Aug  3 15:57 alex2.txt
[root@oldboyedu-lnb~]# touch /oldboy/lidao{1..5}/{www,bbs}.conf
[root@oldboyedu-lnb~]# tree /oldboy/lidao{1..5}

/oldboy/lidao1

├── bbs.conf

└── www.conf

/oldboy/lidao2

├── bbs.conf

└── www.conf

/oldboy/lidao3

├── bbs.conf

└── www.conf

/oldboy/lidao4

├── bbs.conf

└── www.conf

/oldboy/lidao5

├── bbs.conf

└── www.conf

 6.6.3 使用find的三種方法查詢oldboy的一級目錄下所有的檔案 複製到/tmp下

① find  /oldboy/ –maxdepth 1 -type f |xargs –i cp {} /tmp

② find  /oldboy/ –maxdepth 1 –type f –exec cp {} /tmp/ \;

③cp  `find  /oldboy/ –maxdepth 1 –type f`  /tmp

[root@oldboyedu-lnb~]# tree -L 1 /oldboy

/oldboy

├── alex1.txt

├── alex2.txt

├── lidao1

├── lidao2

├── lidao3

├── lidao4

└── lidao5

  6.6.4 使用find的三種方式把oldboy下所有的檔案mv 到 /opt目錄下

①  find  /oldboy/* –type f |xargs –i mv  {} /opt

②  find /oldboy/* –type f –exec mv  /opt/ \;

③  mv `find /oldboy/* -type f`  /opt/

[root@oldboyedu-lnbopt]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 15:57 alex1.txt

-rw-r--r-- 1 root root 0 Aug  3 15:57 alex2.txt

-rw-r--r-- 1 root root 0 Aug  3 17:09 bbs.conf

-rw-r--r-- 1 root root 0 Aug  3 17:09 www.conf

  6.6.5 使用find的三種方式把 oldboy目錄下所有目錄刪除 把 /opt所有檔案刪除

①   find  oldboy/ -type d |xargs –i  rm –rf          find  /opt/ –type f | xargs rm -f       
②   find  /oldboy/ -type d –exec rm -r {} \+;(不報錯) find  /opt/ –type f –exec rm –f {} \;
③  rm -rf `find /oldboy/ -type d`                      rm –rf `find /opt/ –type f`

6.7 find命令如何取反

非
! EXPR
-not EXPR
find命令如何排除某個檔案
-prune
find  -path “./sk”-prune -o  -name “*.txt” –print