1. 程式人生 > 實用技巧 >LINUX核心命令實戰總結二——檔案和目錄操作命令一

LINUX核心命令實戰總結二——檔案和目錄操作命令一

1、檔案和目錄操作命令

1.1pwd:顯示當前所在位置

【功能說明】

Pwd 命令是“print working directory

【語法格式】

pwd [OPTION]...

pwd [選項]...

【引數選項】

Pwd 命令引數選項及說明

引數選項

解釋說明

-L

Logical首字母縮寫,表示顯示邏輯路徑,取pwd系統環境變數的值,極少用

-P

Physical首字母縮寫,表示顯示物理路徑如果當前目錄路徑是軟連線檔案,則會顯示軟連線檔案對應的原始檔,極少用

命令pwd -L echo $PWD 二者的功能是等價

[root@web01 ~]# echo $PWD 
/root [root@web01 ~]# pwd -L /root [root@web01 ~]#

【使用範例】

不帶任何選項執行pwd 命令

[root@web01 ~]# pwd #不帶任何選項執行pwd 命令 
/root [root@web01 ~]# cd /etc/init.d [root@web01 init.d]# pwd /etc/init.d [root@web01 init.d]#

對比使用-L-P引數

[root@web01 init.d]# ls -l /etc/init.d  
lrwxrwxrwx. 1 root root 11
7月 5 2016 /etc/init.d -> rc.d/init.d [root@web01 init.d]# pwd -L /etc/init.d [root@web01 init.d]# echo $PWD /etc/init.d [root@web01 init.d]# pwd -P #顯示連結對應的原始檔的目錄路徑 /etc/rc.d/init.d [root@web01 init.d]#

1.2 cd:切換目錄

【功能說明】

cd 命令是“change directory

【語法格式】

cd [option] [dir]

cd [選項] [目錄]

【選項說明】
cd 命令的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-P

如果切換的目標目錄是一個連結,則會直接切換軟連線指向的真正物理目標目錄,和pwd命令的-p選項功能類似,該引數不常用

-L

功能與-P相反,如果切換的目標目錄是一個軟連線,則直接切換到軟連線所在的目錄,和pwd命令的-L 選項功能類似,該引數常用

-

當只使用“-”選項時,將會從當前目錄切換到系統環境變數“OLDPWD”對應值的目錄路徑,即當前使用者上一次所在的目錄路徑。

~

當只使用“~”選項時,將會從當前目錄切換到系統環境變數“HOME”對應值得目錄路徑,即當前使用者的家目錄所在的路徑

..

當前使用“..”選項時,將會從當前目錄切換到當前目錄的上一級目錄所在路徑

【使用範例】

進入目錄

[root@web01 ~]# pwd
/root
[root@web01 ~]# cd /usr/local        #切換到/usr/local目錄
[root@web01 local]# pwd
/usr/local

切換到當前目錄的上一級目錄

[root@web01 local]# pwd
/usr/local
[root@web01 local]# cd ..
[root@web01 usr]# pwd
/usr

返回當前使用者上一次所在的目錄

[root@web01 usr]# pwd
/usr
[root@web01 usr]# cd -
/usr/local
[root@web01 local]# 

進入當前使用者的家目錄

[root@web01 local]# pwd
/usr/local
[root@web01 local]# cd ~
[root@web01 ~]# pwd
/root

1.3tree:以樹形結構顯示目錄下的內容

【功能說明】

tree 命令中文“樹”的意思。

【語法格式】

 tree  [option]  [directory] 
 tree  [選項]    [目錄] 

【選項說明】

Tree 命令的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-a

顯示所有檔案,包括隱藏檔案(以“.”開頭的檔案)

-d

只顯示目錄(*

-f

顯示每個檔案的全路徑

-i

不顯示樹枝,常與-f引數配合使用

-L level

遍歷目錄的最大層數,level為大於0的正數(*

-F

在執行檔案、目錄、Socket、符號連結、管道名稱等不同型別檔案的結尾,各自加上“*”、“/”、“=”、“@”、“|”號,類似ls命令的-F選項

【使用範例】

安裝檢視安裝版本

[root@client ~]# rpm -qa tree
tree-1.5.3-3.el6.x86_64
[root@client ~]# yun -y install tree

解決亂碼問題

[root@client ~]# tree /boot      
/boot
|-- System.map-2.6.32-504.el6.x86_64
|-- config-2.6.32-504.el6.x86_64
|-- efi
|   `-- EFI
|       `-- redhat
|           `-- grub.efi
[root@client ~]# LANG=en_US_UTF-8
[root@client ~]# tree /boot
/boot
├── config-2.6.32-504.el6.x86_64
├── efi
│ └── EFI
│     └── redhat
│         └── grub.efi

不帶任何引數

[root@web01 ~]# tree
.
├── aa
├── anaconda-ks.cfg
├── eth0 -> /etc/sysconfig/network-scripts/ifcfg-eth0
├── eth1 -> /etc/sysconfig/network-scripts/ifcfg-eth1
├── install.log
└── install.log.syslog
0 directories, 6 files

以樹型結構顯示目錄下的所有內容

[root@web01 ~]# tree -a
.
├── aa
├── anaconda-ks.cfg
├── .bash_history
├── .bash_logout
├── .bash_profile
├── .bashrc
├── .cshrc
├── eth0 -> /etc/sysconfig/network-scripts/ifcfg-eth0
├── eth1 -> /etc/sysconfig/network-scripts/ifcfg-eth1
├── install.log
├── install.log.syslog
├── .tcshrc
└── .viminfo
0 directories, 13 files

只列出根目錄下第一層目錄結構

[root@web01 ~]# tree -L 1 /
/
├── app
├── backup
├── bin
├── boot
省略.......若干......
├── server
├── srv
├── sys
├── tmp
├── usr
└── var
26 directories, 0 files

只顯示所有的目錄(不顯示檔案和其他檔案型別)

[root@web01 ~]# tree -d /etc/
/etc/
├── abrt
│ └── plugins
├── acpi
│ ├── actions
│ └── events
├── alsa
├── alternatives
248 directories

-f 選項和-i 選項的使用

使用-f選項課顯示完整的路徑名稱,使用-i選項則不顯示樹枝部分。

[root@web01 ~]# tree -L 1 -f /boot
/boot
├── /boot/config-2.6.32-504.el6.x86_64
├── /boot/efi
├── /boot/grub
├── /boot/initramfs-2.6.32-504.el6.x86_64.img
├── /boot/initrd-2.6.32-504.el6.x86_64kdump.img
├── /boot/lost+found
├── /boot/symvers-2.6.32-504.el6.x86_64.gz
├── /boot/System.map-2.6.32-504.el6.x86_64
└── /boot/vmlinuz-2.6.32-504.el6.x86_64
3 directories, 6 files
[root@web01 ~]# tree -L 1 -fi /boot
/boot
/boot/config-2.6.32-504.el6.x86_64
/boot/efi
/boot/grub
/boot/initramfs-2.6.32-504.el6.x86_64.img
/boot/initrd-2.6.32-504.el6.x86_64kdump.img
/boot/lost+found
/boot/symvers-2.6.32-504.el6.x86_64.gz
/boot/System.map-2.6.32-504.el6.x86_64
/boot/vmlinuz-2.6.32-504.el6.x86_64

使用tree命令區分目錄和檔案的方法

[root@web01 ~]# tree -L 1 -F /boot 
/boot
├── config-2.6.32-504.el6.x86_64
├── efi/
├── grub/
├── initramfs-2.6.32-504.el6.x86_64.img
├── initrd-2.6.32-504.el6.x86_64kdump.img
├── lost+found/
├── symvers-2.6.32-504.el6.x86_64.gz
├── System.map-2.6.32-504.el6.x86_64
└── vmlinuz-2.6.32-504.el6.x86_64*

3 directories, 6 files
[root@web01 ~]# tree -L 1 -F /boot/|grep /$
/boot/
├── efi/
├── grub/
├── lost+found/

1.4mkdir:建立目錄

【功能說明】

Mkdir 命令是“make dirctories

【語法格式】

mkdir     [OPTION]...     [DIRECTORY]...
mkdir     [選項]...        [ 目錄]...

【選項說明】

Mkdir命令的引數選項和說明

引數選項

解釋說明(帶*的為重點)

-p

遞迴建立目錄,遞迴的意思是父目錄及其子目錄的子目錄(*

-m

設定新建立的預設目錄對應的許可權

-v

顯示建立目錄的過程

【使用範例】

Mkdir 命令建立目錄

[root@web01 ~]# tree -d
.
0 directories
[root@web01 ~]# mkdir data
[root@web01 ~]# tree -d   
.
└── data
1 directory
[root@web01 ~]# 

使用-p引數遞迴建立目錄

[root@web01 ~]# mkdir  /hh/han   
mkdir: 無法建立目錄"/hh/han": 沒有那個檔案或目錄
[root@web01 ~]# mkdir -p yhh/han 
[root@web01 ~]# tree -d
.
├── data
└── yhh
    └── han
3 directories

-v引數顯示建立目錄過程

[root@web01 ~]# mkdir -pv yan/yui/huihuang
mkdir: 已建立目錄 "yan"
mkdir: 已建立目錄 "yan/yui"
mkdir: 已建立目錄 "yan/yui/huihuang"

建立目錄時可使用-m引數設定目錄的預設許可權

[root@web01 ~]# mkdir dir1
[root@web01 ~]# ls -ld dir1
drwxr-xr-x 2 root root 4096 1月  18 12:17 dir1
[root@web01 ~]# mkdir -m 333 dir2
[root@web01 ~]# ls -ld dir2
d-wx-wx-wx 2 root root 4096 1月  18 12:18 dir2

【技巧性範例】

同時建立多個目錄及其多級子目錄

[root@web01 ~]# mkdir -pv yhh/{dir_1,dir_2}/{dir_2.1,dir_2.2}
mkdir: 已建立目錄 "yhh/dir_1"
mkdir: 已建立目錄 "yhh/dir_1/dir_2.1"
mkdir: 已建立目錄 "yhh/dir_1/dir_2.2"
mkdir: 已建立目錄 "yhh/dir_2"
mkdir: 已建立目錄 "yhh/dir_2/dir_2.1"
mkdir: 已建立目錄 "yhh/dir_2/dir_2.2"
[root@web01 ~]# tree -d yhh
yhh
├── dir_1
│   ├── dir_2.1
│   └── dir_2.2
├── dir_2
│   ├── dir_2.1
│   └── dir_2.2
└── han
7 directories
[root@web01 ~]# mkdir -p test/dir{1..5} test1/{a..g}
[root@web01 ~]# tree -d test test1
test
├── dir1
├── dir2
├── dir3
├── dir4
└── dir5
test1
├── a
├── b
├── c
├── d
├── e
├── f
└── g
12 directories
View Code

1.5touch:建立空檔案或改變檔案的時間戳屬性

【功能說明】

Touch命令有兩個功能:一是建立新的空檔案,二是改變已有檔案的時間戳屬性。

【語法格式】

Touch  [OPTION]... [FILE]...
touch  [選項]... [檔案]...

【選項說明】

Touch命令引數選項及說明

引數選項

解釋說明(帶*的為重點)

-a

只更改指定檔案的最後訪問時間

-d STRING

使用字串STRING代表的時間作為模板設定指定檔案的時間屬性

-m

只更改指定檔案的最後修改時間

-r file

將指定檔案的時間屬性設定為與模板檔案file的時間屬性相同

-t STAMP

使用[[CC]YY]MMDDhhmm[.ss]格式的時間設定檔案的時間屬性。格式的含義從左到右依次為:世紀、年、月、日、時、分、秒

【使用範例】

建立檔案(檔案不存在)

[root@web01 test]# touch yhh.txt

[root@web01 test]# ls

yhh.txt

[root@web01 test]# touch a.txt b.txt

[root@web01 test]# ls

a.txt  b.txt  yhh.txt

[root@web01 test]# touch stu{01..05}

[root@web01 test]# ls

a.txt  b.txt  stu01  stu02  stu03  stu04  stu05  yhh.txt
View Code

更改檔案的時間戳屬性

[root@web01 test]# stat yhh.txt

  File: "yhh.txt"

  Size: 0               Blocks: 0          IO Block: 4096   普通空檔案

Device: fd00h/64768d    Inode: 1703979     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-01-18 14:41:46.865161519 +0800

Modify: 2018-01-18 14:41:46.865161519 +0800

Change: 2018-01-18 14:41:46.865161519 +0800

[root@web01 test]# touch -a yhh.txt

[root@web01 test]# stat yhh.txt    

  File: "yhh.txt"

  Size: 0               Blocks: 0          IO Block: 4096   普通空檔案

Device: fd00h/64768d    Inode: 1703979     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-01-18 14:51:17.706155181 +0800

Modify: 2018-01-18 14:41:46.865161519 +0800

Change: 2018-01-18 14:51:17.706155181 +0800

[root@web01 test]# touch -m yhh.txt

[root@web01 test]# stat yhh.txt    

  File: "yhh.txt"

  Size: 0               Blocks: 0          IO Block: 4096   普通空檔案

Device: fd00h/64768d    Inode: 1703979     Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2018-01-18 14:51:17.706155181 +0800

Modify: 2018-01-18 14:51:54.638152169 +0800

Change: 2018-01-18 14:51:54.638152169 +0800
View Code

指定時間屬性建立/修改檔案(指定選項-d指定建立檔案後的檔案修改時間)

[root@web01 test]# ls -lh yhh.txt

-rw-r--r-- 1 root root 0 1月  18 14:51 yhh.txt

[root@web01 test]# touch -d 20201001 yhh.txt

[root@web01 test]# ls -lh yhh.txt           

-rw-r--r-- 1 root root 0 10月  1 2020 yhh.txt

利用選項-r,修改yhh.txt的時間屬性,使其和a.txt的時間屬性一致

[root@web01 test]# ls -lh yhh.txt

-rw-r--r-- 1 root root 0 10月  1 2020 yhh.txt

[root@web01 test]# touch a.txt yhh.txt

[root@web01 test]# ls -lh yhh.txt

-rw-r--r-- 1 root root 0 1月  18 14:59 yhh.txt
View Code

還可以利用選項-t ,將檔案設定201712312234.50時間格式

[root@web01 test]# touch -t 201712312234.50 yhh.txt
[root@web01 test]# ls -lh yhh.txt
-rw-r--r-- 1 root root 0 12月 31 22:34 yhh.txt

【擴充套件知識】

Linux檔案有3鍾型別的時間戳:

Access: 2018-01-18 14:51:17.706155181 +0800 #最後訪問檔案的時間

Modify: 2018-01-18 14:51:54.638152169 +0800 #最後修改檔案的時間

Change: 2018-01-18 14:51:54.638152169 +0800 #最後改變檔案的時間

對應ls 命令檢視上述時間選項如下:

[root@web01 test]# ls -lt yhh.txt #修改檔案內容,檔案的修改時間會改變
-rw-r--r-- 1 root root 0 12月 31 22:34 yhh.txt
[root@web01 test]# ls -lc yhh.txt #修改檔案內容,移動檔案或改變檔案屬性等
-rw-r--r-- 1 root root 0 1月  18 15:02 yhh.txt
[root@web01 test]# ls -lu yhh.txt #檢視檔案內容時,檔案的訪問時間會改變
-rw-r--r-- 1 root root 0 12月 31 22:34 yhh.txt

1.6ls:顯示目錄下的內容及相關屬性資訊

【功能說明】

ls 命令可以理解“list”縮寫。

【語法格式】

ls         [OPTION]...     [FILE]...
Ls         [選項]...         [檔案]...

【選項說明】

ls命令的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-l

使用長格式列出檔案及目錄資訊

-a

顯示目錄下的所有檔案,包括以“.”字元開始的隱藏檔案(*

-t

根據最後的修改時間(mtime)排序,預設是以檔名排序

-r

以相反次序排列

-F

在條目加上檔案型別的指示符(*/=@|、其中的一個)(*

-p

只在目錄後面加上“/

-i

顯示inode節點資訊

-d

當遇到目錄時,列出目錄本身而非目內容的檔案,並且不跟隨符號連結(*

-h

以人類可讀的資訊顯示檔案或目錄大小。

-A

列出所有檔案,包括隱藏檔案,但不包括“.”、“..”這兩個目錄

-S

根據檔案大小排序

-R

遞迴列出所有子目錄

-x

逐行列出專案而不是逐欄列出

-X

根據副檔名排序

-c

根據狀態改變時間(ctime)排序

-u

根據最後訪問時間(atime)排序

--color={never,always,auto}

不同的檔案型別顯示不同的顏色引數,never表示不顯示,always表示總是顯示,auto表示自動顯示

-full-time

以完整的時間格式輸出

--time-style={full-iso,long-iso,locale}

以不同的格式輸出,long-iso效果最好

--time={atime,ctime}

按照不同的時間屬性輸出,atime表示按訪問時間,ctime 表示按改變許可權屬性時間,如果不加此引數,則預設為最後修改時間

【使用範例】

例項前做好一些準備

[root@web01 ~]# mkdir /test
[root@web01 ~]# cd /test
[root@web01 test]# touch file1.txt file2.txt file3.txt
[root@web01 test]# mkdir dir1 dir2 dir3
[root@web01 test]# tree
.
├── dir1
├── dir2
├── dir3
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 3 files
View Code

基礎範例

直接執行ls命令,不帶任何引數

[root@web01 test]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt

使用-a引數顯示所有檔案,特別是隱藏檔案

[root@web01 test]# ls -a
.  ..  dir1  dir2  dir3  file1.txt  file2.txt  file3.txt  .file4.txt

使用-l引數顯示詳細資訊

[root@web01 test]# ls -l
總用量 12
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir1
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir2
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir3
-rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
View Code

顯示完整時間屬性的引數--time-style=long-iso

[root@web01 test]# ls -l --time-style=long-iso
總用量 12
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
View Code

執行ls命令,帶顯示內容的訪問時間屬性的引數

[root@web01 test]# stat file1.txt
  File: "file1.txt"
  Size: 0               Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d    Inode: 262147      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-01-18 15:48:38.247743143 +0800
Modify: 2018-01-18 15:48:38.247743143 +0800
Change: 2018-01-18 15:48:38.247743143 +0800
[root@web01 test]# date
2018年 01月 18日 星期四 16:04:59 CST
[root@web01 test]# cat file1.txt
[root@web01 test]# stat file1.txt
  File: "file1.txt"
  Size: 0               Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d    Inode: 262147      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-01-18 16:05:17.699956221 +0800
Modify: 2018-01-18 15:48:38.247743143 +0800
Change: 2018-01-18 15:48:38.247743143 +0800
[root@web01 test]# ls -l --time-style=long-iso --time=atime   
總用量 12
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 16:05 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
[root@web01 test]# ls -l --time-style=long-iso 
總用量 12
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
View Code

執行ls命令,帶-F引數(這一點與tree命令的-F很類似)

[root@web01 test]# ls -F
dir1/  dir2/  dir3/  file1.txt  file2.txt  file3.txt
[root@web01 test]# ls -F|grep /
dir1/
dir2/
dir3/

使用-d引數只顯示目錄本身的資訊

[root@web01 test]# ls -l dir1
總用量 0
[root@web01 test]# ls -ld dir1
drwxr-xr-x 2 root root 4096 1月  18 15:48 dir1

使用-R引數遞迴檢視目錄

[root@web01 test]# mkdir dir1/sub1/test -p
[root@web01 test]# ls -R dir1
dir1:
sub1
dir1/sub1:
test
dir1/sub1/test:
View Code

【技巧性示範】

使用ls命令別名的相關知識及設定ls別名

[root@web01 test]# alias lst='ls -l --time-style=long-iso'
[root@web01 test]# alias |grep lst
alias lst='ls -l --time-style=long-iso'
[root@web01 test]# lst
總用量 12
drwxr-xr-x 3 root root 4096 2018-01-18 16:29 dir1
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir2
drwxr-xr-x 2 root root 4096 2018-01-18 15:48 dir3
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file1.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file2.txt
-rw-r--r-- 1 root root    0 2018-01-18 15:48 file3.txt
View Code

查詢最近更新過的檔案

[root@web01 ~]# ls -lrt /etc|tail -1
-rw-r--r--   1 root root      0 1月  18 16:35 test.txt

【生產案例】

生產場景資料庫備份,獲取資料庫名錶

[root@web01 test]# ls -lhi
總用量 12K
262150 drwxr-xr-x 3 root root 4.0K 1月  18 16:29 dir1
262151 drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir2
262152 drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir3
262147 -rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
262148 -rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
262149 -rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
View Code

第一列:inode索引節點編號

第二列:檔案型別及許可權

第三列:硬連結個數

第四列:檔案或目錄所屬的使用者

第五列:檔案或目錄所屬的組

第六列:檔案或目錄的大小

第七、八、九列:檔案或目錄的修改時間

第十列:實際的檔案或目錄名

1.7cp:複製檔案或目錄

【功能說明】

cp命令可以理解為英文單詞copy縮寫。

【語法格式】

 cp         [OPTION]...     [SOURCE]...     [DIRECTORY]
 cp         [選項]...         [原始檔]...     [目標檔案]

【選項說明】

引數選項

解釋說明(帶*的為重點)

-p

複製檔案保持原始檔的所有者、許可權資訊及時間屬性

-d

如果複製的原始檔的符號連結,那麼複製符號連結本身,而且保留符合連結所指向的目標檔案或目錄

-r

遞迴複製目錄,即複製目錄下的所有層級的子目錄及檔案

-a

等同於上面的pdr3各引數的功能總和

-i

覆蓋已有檔案前提示使用者確認

-t

預設情況下命令格式是“cp”原始檔,目標檔案,使用用-t 可以顛倒順序,格式變為“cp -t”目標檔案 原始檔

【使用範例】

[root@web01 test]# pwd
/test
[root@web01 test]# ll -h
總用量 12K
drwxr-xr-x 3 root root 4.0K 1月  18 16:29 dir1
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir2
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir3
-rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
[root@web01 test]# cp file1.txt file4.txt
[root@web01 test]# cp -a file1.txt file5.txt
[root@web01 test]# ll -h
總用量 12K
drwxr-xr-x 3 root root 4.0K 1月  18 16:29 dir1
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir2
drwxr-xr-x 2 root root 4.0K 1月  18 15:48 dir3
-rw-r--r-- 1 root root    0 1月  18 15:48 file1.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file2.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file3.txt
-rw-r--r-- 1 root root    0 1月  18 17:17 file4.txt
-rw-r--r-- 1 root root    0 1月  18 15:48 file5.txt
View Code

使用-i引數的例子

[root@web01 test]# cp -i file1.txt file5.txt
cp:是否覆蓋"file5.txt"? y
[root@web01 test]# alias cp
alias cp='cp -i'

使用-r引數複製目錄

[root@web01 test]# cp dir1 dir4
cp: 略過目錄"dir1"
[root@web01 test]# cp dir1 dir4 -r
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt

【技巧性範例】

命令cp覆蓋檔案之前不提示是否覆蓋的幾種方法

[root@web01 test]# /bin/cp file1.txt file4.txt            #全路徑
[root@web01 test]# \cp file1.txt file4.txt             #命令開頭用反斜線
[root@web01 test]# unalias cp                    #重啟會失效
[root@web01 test]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'            #刪除這個
alias mv='mv -i'   

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
View Code

快速備份檔案

[root@web01 test]# cp /etc/ssh/ssh
ssh_config            ssh_host_dsa_key      ssh_host_key          ssh_host_rsa_key      
sshd_config           ssh_host_dsa_key.pub  ssh_host_key.pub      ssh_host_rsa_key.pub  
[root@web01 test]# cp /etc/ssh/sshd_config{,.org}
[root@web01 test]# ls -l /etc/ssh/sshd_config{,.org}
-rw------- 1 root root 3877 12月  5 10:47 /etc/ssh/sshd_config
-rw------- 1 root root 3877 1月  18 21:21 /etc/ssh/sshd_config.org

1.8mv 移動或重新命名檔案

【功能說明】

Mv 命令可以理解為英文“move”的縮寫。

【語法格式】

 mv         [OPTION]...  [SOURCE ]     [DEST]
 mv         [命令]...       [ 原始檔]    [目標檔案]

【選項說明】

Mv 命令的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-f

若目標檔案存在,則不會詢問而是直接覆蓋

-i

若目標檔案存在,則會詢問是否直接覆蓋

-n

不覆蓋已經存在檔案

-t

指定mv的目標目錄,適用於移動多個原始檔到一個目錄的情況,此時目標目錄在前,原始檔在後,和cp命令的-t選項功能一致。

-u

在原始檔比目標檔案新,或者目標檔案不存在時才會進行移動

【使用範例】

給檔案改名字:

[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
[root@web01 test]# mv file5.txt file6.txt
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file4.txt  file6.txt
[root@web01 test]# mv file4.txt file6.txt  
mv:是否覆蓋"file6.txt"? y
[root@web01 test]# alias mv
alias mv='mv -i'
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file1.txt  file2.txt  file3.txt  file6.txt
[root@web01 test]# \mv file1.txt file6.txt 
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file2.txt  file3.txt  file6.txt
View Code

移動檔案:

[root@web01 test]# ls dir1
sub1
[root@web01 test]# mv file6.txt dir/ 
mv: 無法將"file6.txt" 移動至"dir/": 不是目錄
[root@web01 test]# mv file6.txt dir1/
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file2.txt  file3.txt
[root@web01 test]# ls dir1 
file6.txt  sub1
View Code

移動多個檔案:

[root@web01 test]# ls dir1 
file6.txt  sub1
[root@web01 test]# touch yhh{00..05}
[root@web01 test]# mv yhh00 yhh01 yhh03 yhh04 dir1/
[root@web01 test]# ls
dir1  dir2  dir3  dir4  file2.txt  file3.txt  yhh02  yhh05
[root@web01 test]# ls dir1
file6.txt  sub1  yhh00  yhh01  yhh03  yhh04

將源和目標調換移動到檔案目錄(-t引數)

[root@web01 test]# mv dir1/ yhh02 yhh05
mv: 目標"yhh05" 不是目錄
[root@web01 test]# mv -t  dir1/ yhh02 yhh05
View Code

移動目錄的例子:

[root@web01 test]# ls dir1
file6.txt  sub1  yhh00  yhh01  yhh02  yhh03  yhh04  yhh05
[root@web01 test]# mv dir1 dir5
[root@web01 test]# ls dir5
file6.txt  sub1  yhh00  yhh01  yhh02  yhh03  yhh04  yhh05
[root@web01 test]# mv dir3 dir5
[root@web01 test]# ls dir5
dir3  file6.txt  sub1  yhh00  yhh01  yhh02  yhh03  yhh04  yhh05
View Code

命令mv 小總結:

原始檔

目標檔案

結果

一個普通檔案A

目錄B

檔案A移動到目錄B

多個檔案A1A2......

目錄B

在目錄B下有檔案A1A2......,有兩種寫法

一個普通檔案A

普通檔案C

將檔案A重新命名為檔案C,如果檔案C已經存在則會提醒是否覆蓋。

多個普通檔案A1A2.....

目錄B

若目錄B不會存在,則將目錄D改名為B;若目錄B存在,則將D移動到B

一個目錄D

目錄B

如目錄B不存在,則將目錄D改成為B;若目錄B存在,則將D移動到B中。

多個目錄D1D2......

目錄B

如果目錄B不存在則會報錯;若目錄B存在則將D1D2.....移動到目錄B中。

一個目錄D

普通檔案C

報錯,提示不能將目錄複製成檔案

多個目錄D1D2......

普通檔案C

報錯,提示不能將目錄複製成檔案

1.9 rm:刪除檔案或目錄

【功能說明】

命令rm可以理解為英文單詞remove的縮寫。

【語法格式】

rm [OPTION]... [FILE]...
rm          [選項]...            [檔案或目錄]...

【選項說明】

命令rm選項說明

引數選項

解釋說明(帶*的為重點)

-f

強制刪除,忽略不存在檔案,不提示確認(*

-i

在刪除前需要確認

-I

在刪除超過三個檔案或者遞迴刪除前要確認

-r

遞迴刪除目錄及內容(*

【使用範例】

[root@web01 ~]# mkdir -p /data/{dir1,dir2,dir3}
[root@web01 ~]# touch /data/{file1.txt,file2.txt,file3.txt}
[root@web01 ~]# tree /data
/data
├── dir1
├── dir2
├── dir3
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 3 files
View Code

不帶引數刪除

[root@web01 ~]# cd /data
[root@web01 data]# ls
dir1  dir2  dir3  file1.txt  file2.txt  file3.txt
[root@web01 data]# rm file3.txt
rm:是否刪除普通空檔案 "file3.txt"?n
[root@web01 data]# alias rm
alias rm='rm -i'
View Code

強制刪除

[root@web01 data]# rm -f file3.txt
[root@web01 data]# ls
dir1  dir2  dir3  file1.txt  file2.txt

遞迴刪除

[root@web01 data]# mkdir -p dir1/a/b
[root@web01 data]# tree dir1
dir1
└── a
    └── b

2 directories, 0 files
[root@web01 data]# rm dir1
rm: 無法刪除"dir1": 是一個目錄
[root@web01 data]# rm  -r dir1
rm:是否進入目錄"dir1"? y
rm:是否進入目錄"dir1/a"? y
rm:是否刪除目錄 "dir1"?y
rm: 無法刪除"dir1": 目錄非空
View Code

【刪除的實戰經驗】

1、mv替代rm,不要著急刪除,而是先移到回收站/tmp.

2、刪除前務必備份,最好是異機備份,若出現問題隨時可以回滾。

3、如果非要刪除,那麼請用find替代rm,包括通過系統定時任務等清理檔案方法。

4、如果非要通過rm刪除,那先切換到當前目錄再刪除,能不用萬用字元的就不用萬用字元。對檔案的刪除禁止使用“rm -rf 檔名”,因為“rm -rf ” 誤刪目錄時並不會有提示,非常危險。最多使用“rm -rf 檔名”,推薦用“rm 檔名”。

5、慎用rsync --delete

1.10 rmdir:刪除空目錄

【功能說明】

rmdir 命令用於刪除空目錄(remove empty directiories)當目錄不為空時,命令不起作用。

【語法格式】

rmdir     [OPTION]...     [DIRECTORY]...
rmdir     [選項]...         [目錄]...

【選項說明】

命令rmdir的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-p

遞迴刪除目錄,當子目錄刪除後其父目錄為空時,也一併刪除。如果整個路徑被刪除或者由於某種原因保留部分路徑,則系統在標準輸出上顯示相應的資訊。

-v

顯示命令的執行過程

【使用範例】

[root@web01 data]# tree dir1
dir1
└── a

1 directory, 0 files
[root@web01 data]# rmdir dir1
rmdir: 刪除 "dir1" 失敗: 目錄非空

引數-p 遞迴刪空目錄

[root@web01 data]# rmdir -pv dir1
rmdir: 正在刪除目錄 "dir1"
rmdir: 刪除 "dir1" 失敗: 目錄非空
[root@web01 data]# rmdir -pv dir1/a
rmdir: 正在刪除目錄 "dir1/a"
rmdir: 正在刪除目錄 "dir1"