­

文件查找及上傳下載

2020.10.26星期一  正式班D15

9.6 文件查找命令find

9.6.1 基本命令

  • find [path] [-option] [expression]

9.6.2 基本用法

  • 基礎

    # 按文件名 -name
    [root@ccc ~]# find /etc -name 'ifcfg-ens33'
    /etc/sysconfig/network-scripts/ifcfg-ens33  # 查找ifcfg-ens33
    [root@ccc ~]# find /etc -name 'ifcfg-*'
    /etc/sysconfig/network-scripts/ifcfg-lo
    /etc/sysconfig/network-scripts/ifcfg-ens33
    /etc/sysconfig/network-scripts/ifcfg-ENS33  # 查找以ifcfg開頭的文件
    [root@ccc ~]# find /etc/ -iname 'ifcfg-ens33'
    /etc/sysconfig/network-scripts/ifcfg-ens33
    /etc/sysconfig/network-scripts/ifcfg-ENS33  # i不區分大小寫
    
    # 按文件大小 -size
    [root@ccc ~]# 
    [root@ccc ~]# find /etc -size +5M  # 大於5M的文件
    /etc/udev/hwdb.bin
    [root@ccc ~]# find /etc -size 3M  # 等於3M的文件
    [root@ccc ~]# find /etc -size -1k  # 小於1k的文件
    /etc/crypttab
    /etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
    /etc/environment
    /etc/exports
    /etc/motd
    /etc/sysconfig/network-scripts/ifcfg-ENS33
    [root@ccc ~]# find /etc -size +3M -ls  # -ls找到的處理動作
        70 8188 -r--r--r--   1 root     root      8384358 1016 12:32 /etc/udev/hwdb.bin
    8809555 3816 -rw-------   1 root     root      3905267 41  2020 /etc/selinux/targeted/active/policy.kern
    13347359 3816 -rw-r--r--   1 root     root      3905267 41  2020 /etc/selinux/targeted/policy/policy.31
    
    # 指定查找的目錄深度 -maxdepth levels
    [root@ccc ~]# find / -maxdepth 5 -a -name 'ifcfg-ens33'
    /etc/sysconfig/network-scripts/ifcfg-ens33
    /test/ifcfg-ens33  # -a並且,-o或者,不加-a默認就是-a
    
    # 按時間找 -atime -mtime -ctime
    [root@ccc ~]# find . -mtime +15  # 修改時間超過15天
    ./.bash_logout
    ./.bash_profile
    ./.bashrc
    ./.cshrc
    ./.tcshrc
    ./Python-2.7.18.tgz
    ./1.jpg
    [root@ccc ~]# find . -mtime 1  # 修改時間等於1天
    [root@ccc ~]# find . -mtime -3  # 修改時間3天以內
    .
    ./.bash_history
    ./t.txt
    ./f.txt
    ./1.py
    ./.viminfo
    ./a.txt
    
    # 按文件屬主、屬組找 -user -group
    [root@ccc ~]# find /home -user lili  # 屬主是lili的文件
    /home/lili
    /home/lili/.bash_logout
    [root@ccc ~]# find /home -group lili  # 數組是lili的文件
    /home/lili
    /home/lili/.bash_logout
    [root@ccc ~]# find /home -user lili -a -group lili  # 屬主lili且數組lili的文件
    /home/lili
    /home/lili/.bash_logout
    [root@ccc ~]# find /home -user lili -group lili  # 同上-a
    /home/lili
    /home/lili/.bash_logout
    [root@ccc ~]# find /home -user lili -o -group lili  # 屬主lili或數組lili的文件
    /home/lili
    /home/lili/.bash_logout
    
    [root@ccc ~]# su - lili  # 切換到lili用戶
    [lili@ccc ~]$ touch 111.txt  # 創建111.txt
    [lili@ccc ~]$ exit
    登出
    [root@ccc ~]# ll /home/lili/111.txt 
    -rw-rw-r-- 1 lili lili 0 1026 14:34 /home/lili/111.txt
    [root@ccc ~]# vim /etc/group
    [root@ccc ~]# find /home -nogroup -ls # 用戶還存在,在/etc/group刪除了記錄
    8494233    0 drwx------   2 lili     1000           98 1026 14:34 /home/lili
    8494238    4 -rw-r--r--   1 lili     1000           18 41  2020 /home/lili/.bash_logout
    8494239    4 -rw-r--r--   1 lili     1000          193 41  2020 /home/lili/.bash_profile
    8493577    4 -rw-r--r--   1 lili     1000          231 41  2020 /home/lili/.bashrc
    8493579    0 -rw-rw-r--   1 lili     1000            0 1026 14:34 /home/lili/111.txt
    8493580    4 -rw-------   1 lili     1000           19 1026 14:34 /home/lili/.bash_history
    
    [root@ccc ~]# find /home -nouser -ls
    8494233    0 drwx------   2 1000     1000           98 1026 14:34 /home/lili
    8494238    4 -rw-r--r--   1 1000     1000           18 41  2020 /home/lili/.bash_logout
    8494239    4 -rw-r--r--   1 1000     1000          193 41  2020 /home/lili/.bash_profile
    8493577    4 -rw-r--r--   1 1000     1000          231 41  2020 /home/lili/.bashrc
    8493579    0 -rw-rw-r--   1 1000     1000            0 1026 14:34 /home/lili/111.txt
    8493580    4 -rw-------   1 1000     1000           19 1026 14:34 /home/lili/.bash_history
    
    [root@ccc ~]# find /home -nouser -ls -o -nogroup -ls
    8494233    0 drwx------   2 1000     1000           98 1026 14:34 /home/lili
    8494238    4 -rw-r--r--   1 1000     1000           18 41  2020 /home/lili/.bash_logout
    8494239    4 -rw-r--r--   1 1000     1000          193 41  2020 /home/lili/.bash_profile
    8493577    4 -rw-r--r--   1 1000     1000          231 41  2020 /home/lili/.bashrc
    8493579    0 -rw-rw-r--   1 1000     1000            0 1026 14:34 /home/lili/111.txt
    8493580    4 -rw-------   1 1000     1000           19 1026 14:34 /home/lili/.bash_history
    
    # 按文件類型 -type
    [root@ccc ~]# find /dev -type f  # f普通
    [root@ccc ~]# find /dev -type d  # d目錄
    [root@ccc ~]# find /dev -type l  # l鏈接
    [root@ccc ~]# find /dev -type b  # b塊設備
    [root@ccc ~]# find /dev -type c  # c字元設備
    [root@ccc ~]# find /dev -type s  # s套接字
    [root@ccc ~]# find /dev -type p  # p管道文件
    
    # 根據inode號查找 -inum n
    [root@ccc ~]# find / -inum 1811
    
    # 按文件許可權 -perm
    [root@ccc ~]# find . -perm 644 -ls
    
    
  • 後續處理

    -print
    -ls
    -delete
    -exec
    -ok
    
    # -exec非交互
    [root@ccc ~]# find /etc/ -name "ifcfg-*" -exec cp -rvf {} /tmp \;
    "/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
    "/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
    "/etc/sysconfig/network-scripts/ifcfg-ENS33" -> "/tmp/ifcfg-ENS33"
    # -ok交互
    [root@ccc ~]# find /etc/ -name "ifcfg-*" -ok cp -rvf {} /tmp \;
    < cp ... /etc/sysconfig/network-scripts/ifcfg-lo > ? y
    "/etc/sysconfig/network-scripts/ifcfg-lo" -> "/tmp/ifcfg-lo"
    < cp ... /etc/sysconfig/network-scripts/ifcfg-ens33 > ? y
    "/etc/sysconfig/network-scripts/ifcfg-ens33" -> "/tmp/ifcfg-ens33"
    < cp ... /etc/sysconfig/network-scripts/ifcfg-ENS33 > ? y
    "/etc/sysconfig/network-scripts/ifcfg-ENS33" -> "/tmp/ifcfg-ENS33"
    
    # 刪除的一種用法
    [root@ccc ~]# find /etc/ -name "ifcfg-*" -exec rm -rf {} \;
    [root@ccc ~]# find /etc/ -name "ifcfg-*" -delete
    
  • 拓展:find用管道必須結合xargs

    [root@ccc ~]# find . -name 'cjx*.txt' |xargs rm -rf  # 刪除的另一種方式
    
    [root@ccc ~]# find /etc/ -name 'ifcfg-ens33' |xargs -I {} cp -rf {} /var/tmp/
    [root@ccc ~]# ll /var/tmp/
    總用量 1
    -rw-r--r-- 1 root root 262 1026 15:07 ifcfg-ens33
    
    [root@ccc ~]# find /test -name 'ifcfg-ens33' | xargs -I {} mv {} /ttt
    
    

9.7 文件的上傳與下載

9.7.1 下載

  • wget命令

    # 將遠程包下載到本地,-O指定下載到哪裡,也可省略-O 本地路徑
    wget -O 本地路徑 遠程包鏈接地址
    [root@ccc ~]# wget -O ./2.jpg //pic17.nipic.com/20111115/8754739_105205490111_2.jpg
    
    # 如果wget下載提示無法建立SSL連接,需加上選項--no-check-certificates
    [root@ccc ~]# wget --no-check-certificate -O ./2.jpg //pic17.nipic.com/20111115/8754739_105205490111_2.jpg
    
    
  • curl命令

    # curl命令是一個利用URL規則在命令行下工作的文件傳輸工具
    # 支援文件的上傳和下載,是綜合傳輸工具,按傳統稱curl為下載工具
    # wget會將下載的內容直接列印在螢幕上
    
    [root@ccc ~]# curl -O ./3 //tiyu.baidu.com/match/S10/tab/賽程
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: 
    
    # 如果遇到下載提示無法建立SSL連接,使用-k選項或者--insecure
    [root@ccc ~]# curl -k -O ./3 //tiyu.baidu.com/match/S10/tab/賽程
    
  • sz命令

    # 系統默認沒有sz命令,需下載yum install -y lrzsz
    # 將伺服器上選定的文件下載/發送到本機
    [root@ccc ~]# sz 1.jpg
    

9.7.2 上傳

  • rz

    # 系統默認沒有rz命令,需下載yum install lrzsz -y
    # 運行該命令會彈出一個文件選擇窗口,從本地選擇文件上傳到伺服器
    [root@ccc ~]# rz  # 如果文件已經存在,則上傳失敗,可以用-E選項解決
    [root@ccc ~]# rz -E  # -E如果目標文件名已存在,則重命名傳入文件。新文件名多一個點或一個數字(0..999)
    

9.8 輸出與重定向

9.8.1 基本概念

  • 幾種情況

    # 輸出即把相關對象通過輸入設備(顯示器等)顯示出來
    # 輸出分為正確輸出和錯誤輸出
    # 一般情況下輸出設備為顯示器
    # 標準輸入設備為鍵盤
    
    0  # 標準輸出
    1  # 標準正確輸出
    2  # 標準錯誤輸出
    

9.8.2 輸出重定向

  • > # 覆蓋

  • >> # 追加

    # 正常輸出是把內容輸出到顯示器上
    # 輸出重定向是把內容輸出到文件中
    
    # 標準輸出重定向(1可以省略)
    命令>文件  # 以覆蓋的形式把命令的正確輸出輸出到指定的文件或設備當中
    命令>>文件  # 以追加的方式把命令的正確輸出輸出到指定的文件或設備中
    
    # 標準錯誤輸出重定向
    錯誤命令 2>文件  # 以覆蓋的形式把命令的錯誤輸出輸出到指定的文件或設備中
    錯誤命令 2>文件  # 以追加的方式把命令的錯誤輸出輸出到指定的文件或設備中
    
    # 例子
    ifconfig>test.log  # 把ifconfig執行顯示的正確內容寫入test.log當前介面不再顯示執行結果
    # 錯誤輸出重定向>與>>後面不能加空格
    
    # 正確輸出與錯誤輸出同時保存
    命令>文件 2>&1  # 以覆蓋的方式把正確輸出和錯誤輸出都保存到同一個文件夾中
    命令>>文件 2 >&1  # 以追加的方式把正確輸出和錯誤輸出都保存到同一個文件中
    命令 &>文件  # 以覆蓋的方式把正確輸出和錯誤輸出打偶把存到同一個文件中
    命令 &>>文件  # 以追加的方式把正確輸出和錯誤輸出都保存到同一個文件夾中
    命令>>文件1 2>>文件2  # 把正確的輸出追加到文件1中,把錯誤的輸出追加到文件2中
    
    # 注意
    命令 >>file.log 2>&1  與  命令 &>>file.log  # 命令作用相同
    命令 >>file.log 2>>file2.log  # 正確日誌和錯誤日誌分開保存
    ls &>/dev/null  # 系統常見用法,正確輸出或錯誤輸出都不要(null理解為黑洞或垃圾站)
    
    

9.8.3 輸入重定向

  • cat >目標文件<內容來源(源文件地址):也可以寫成cat<內容來源>目標文件(將源文件內容寫至目標文件)
  • cat >目標文件<<EOF(鍵盤輸入,最後以EOF結尾)將鍵盤輸入內容寫入目標文件,也可以寫成cat<<EOF>目標文件
Tags: