檔案目錄管理指令
-----------------------------------------------------------------------------------------
touch指令用于建立空白檔案或設定檔案的時間,文法格式為:“touch [參數] 檔案名稱
[root@localhost mnt]# ls
[root@localhost mnt]# touch bing
bing
# vim linux 使用vim編輯一個不存在的檔案,修改儲存後,該會自動建立
[root@localhost mnt]# vim linux
bing linux
--------------------------------------------------------------------------------------------------------------
mkdir指令用于建立空白的目錄,英文全稱為:“make directory”,文法格式為:“mkdir [參數] 目錄名稱”
[root@localhost mnt]# mkdir linux-b
bing linux linux-b
mkdir -p /mnt/linux-c/bing/geely/7dct 建立遞增層次的檔案
[root@localhost mnt]# mkdir -p /mnt/linux-c/bing/geely/7dct
bing linux linux-b linux-c
[root@localhost mnt]# cd /mnt/linux-c/bing/geely/7dct
[root@localhost 7dct]# pwd
/mnt/linux-c/bing/geely/7dct
--------------------------------------------------------------------------------
cp指令用于複制檔案或目錄,英文全稱為:“copy”,文法格式為:“cp [參數] 源檔案名稱 目标檔案名稱”
文法: cp 源… 目的
複制目錄時,多個-r 選項:
# cp -r newdir/ /tmp/ 目标目錄存在,則将源目錄放在該目錄下
# cp -r newdir /tmp/olddir 目标目錄不存在,類似複制過去後重命名
# cp -r /run/media/admin/RHEL-7.0Server.x86_64/. /mnt/redhat7/ 複制/run/media/admin/RHEL-7.0Server.x86_64/下所有檔案到/mnt/redhat7/下
[root@localhost mnt]# cp bing bing1
bing bing1 linux linux-b linux-c
[root@localhost mnt]# cp -r linux-b /mnt/linux-c
[root@localhost mnt]# cd linux-c
[root@localhost linux-c]# ls
bing linux-b
mv指令用于剪切或重命名檔案,英文全稱為:“move”,文法格式為:“mv [參數] 源檔案名稱 目标檔案名稱”
[root@localhost mnt]# mv bing1 bing2
bing bing2 linux linux-b linux-c
[root@localhost mnt]# mv bing2 /mnt/linux-b
[root@localhost mnt]# cd linux-b
[root@localhost linux-b]# ls
bing2
rm指令用于删除檔案或目錄,英文全稱為:“remove”,文法格式為:“rm [參數] 檔案名稱”
rm -f 強制删除檔案
rm -rf 強制删除目錄檔案
[root@localhost mnt]# rm linux
rm: remove regular file 'linux'? y
bing linux-b linux-c
[root@localhost mnt]# rm -f bing
linux-b linux-c
[root@localhost mnt]# rm -rf linux-b
linux-c
dd指令用于按照指定大小的資料塊個數來複制檔案或轉換檔案
if input file 輸入檔案
of output file 輸出檔案
bs 設定每個“塊”的大小
count 設定要複制“塊”的個數
文法格式為:“dd if=參數值 of=參數值 count=參數值 bs=參數值”
file指令用于檢視檔案的類型,文法格式為:“file 檔案名稱”
[root@localhost mnt]# file linux-c/
linux-c/: directory
(考試指令tar)
tar指令用于對檔案進行打包壓縮或解壓,文法格式為:“tar 參數 檔案名稱
[root@localhost mnt]# tar czvf etc.tar.gz /etc
etc.tar.gz linux-c
z ---tar.gz
j ---tar.bz2
tar czvf 壓縮包.tar.gz
tar xjvf 壓縮包.tar.bz2
參數 作用
-c 建立壓縮檔案
-x 解開壓縮檔案
-t 檢視壓縮包内有哪些檔案
-z 用Gzip壓縮或解壓
-j 用bzip2壓縮或解壓
-v 顯示壓縮或解壓的過程
-f 目标檔案名
-p 保留原始的權限與屬性
-P 使用絕對路徑來壓縮
-C 指定解壓到的目錄
grep指令用于在文本中執行關鍵詞搜尋,并顯示比對的結果,格式為“grep [選項] [檔案]”
[root@localhost mnt]# grep /sbin/nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
........
[root@localhost mnt]# grep -c /sbin/nologin /etc/passwd
48
-b 将可執行檔案(binary)當作文本檔案(text)來搜尋
-c 僅顯示找到的行數
-i 忽略大小寫
-n 顯示行号
-v 反向選擇——僅列出沒有“關鍵詞”的行。
find指令用于按照指定條件來查找檔案,格式為“find [查找路徑] 尋找條件 操作”
[root@localhost mnt]# find /etc -name "host*" -print
/etc/opa/hosts
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/avahi/hosts
/etc/hostname
---------------------------------------------------
重定向符(<、<
輸入重定向(檔案到指令)
指令 < 檔案 将檔案作為指令的标準輸入
指令 << 分界符 從标準輸入中讀入,直到遇見分界符才停止
-------------------------------
輸出重定向(指令到檔案)
符号 作用
指令 > 檔案 将标準輸出重定向到一個檔案中(清空寫入 )
指令 2> 檔案 将錯誤輸出重定向到一個檔案中(追加寫入)
指令 >> 檔案 将标準輸出重定向到一個檔案中(清空寫入 )
指令 2>> 檔案 将錯誤輸出重定向到一個檔案中(追加寫入)
指令 &> 檔案 将标準輸出與錯誤輸出共同寫入到檔案中(清空寫入 )
指令 &>> 檔案 将标準輸出與錯誤輸出共同寫入到檔案中(追加寫入 )
[root@localhost mnt]# ls -l > test
[root@localhost mnt]# cat test
total 6548
-rw-r--r--. 1 root root 6701472 Apr 10 12:49 etc.tar.gz
drwxr-xr-x. 4 root root 33 Apr 10 12:31 linux-c
-rw-r--r--. 1 root root 0 Apr 10 13:12 test
[root@localhost mnt]# ls -l bing > test
ls: cannot access 'bing': No such file or directory
[root@localhost mnt]# ls -l bing 2> test
[root@localhost mnt]# ls -l 2> test
-rw-r--r--. 1 root root 0 Apr 10 13:14 test
[root@localhost mnt]#
[root@localhost mnt]# ls -l &>> test
[root@localhost mnt]# ls -l geely &>> test
ls: cannot access 'geely': No such file or directory
total 6552
-rw-r--r--. 1 root root 53 Apr 10 13:18 test
------------------------------------------------------------
管道符
grep /sbin/nologin /etc/passwd | wc -l
grep搜尋指令的輸出值傳遞給wc統計指令,即把原本要輸出到螢幕的使用者資訊清單再交給wc指令作進一步的加工
[root@localhost mnt]# grep /sbin/nologin /etc/passwd | wc -l
把管道符和passwd指令的--stdin參數相結合,可以用一條指令來完成密碼重置操作
echo "root" | passwd --stdin root
[root@localhost mnt]# echo "root" | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
----------------------------------------------------------------
指令行的通配符
[root@localhost ~]# ls -l /dev/sda
brw-rw----. 1 root disk 8, 0 Apr 10 12:24 /dev/sda
[root@localhost ~]# ls -l /dev/sda1
brw-rw----. 1 root disk 8, 1 Apr 10 12:24 /dev/sda1
[root@localhost ~]# ls -l /dev/sda2
brw-rw----. 1 root disk 8, 2 Apr 10 12:24 /dev/sda2
[root@localhost ~]# ls -l /dev/sda3
ls: cannot access '/dev/sda3': No such file or directory
[root@localhost ~]#
[root@localhost ~]# ls -l /dev/sd*
[root@linuxprobe ~]# ls -l /dev/sda?
brw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1
brw-rw----. 1 root disk 8, 2 May 4 15:55 /dev/sda2
[root@localhost ~]# ls -l /dev/sd?
[root@localhost ~]# ls -l /dev/sd??
通配符 含義
* 任意字元
? 單個任意字元
[a-z] 單個小寫字母
[A-Z] 單個大寫字母
[a-Z] 單個字母
[0-9] 單個數字
[:alpha:] 任意字母
[:upper:] 任意大寫字母
[:lower:] 任意小寫字母
[:digit:] 所有數字
[:alnum:] 任意字母加數字
[:punct:] 标點符号
----------------------------------------------------------------------
轉義字元
單個轉義--反斜杠(\):使反斜杠後面的一個變量變為單純的字元。
全部轉義--單引号(''):轉義其中所有的變量為單純的字元串。
有空格加--雙引号(""):保留其中的變量屬性,不進行轉義處理。
執行指令--反引号(``):把其中的指令執行後傳回結果。
[root@localhost ~]# PRICE=5
[root@localhost ~]# echo "Price is $PRICE"
Price is 5
[root@localhost ~]# echo "Price is \$$PRICE"
Price is $5
[root@localhost ~]# echo `uname -a`
Linux localhost.localdomain 4.18.0-80.el8.x86_64 #1 SMP Wed Mar 13 12:02:46 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux