一、 Linux上的文件管理類命令有哪些,其常用的使用方法及相關示例演示
rmdir: 刪除空目錄,如果目錄裏面有內容,需要刪除的話,則需要使用rm命令
格式: rmdir [options] directory…
選項: -p: 遞歸刪除
示例:在 /tmp下創建了a/b/c的目錄,首先,我們刪除/tmp/a目錄,看系統如何提示。 第二步,使用-p選項刪除這些目錄。
[root@localhost tmp]# mkdir -p /tmp/a/b/c
[root@localhost tmp]# tree /tmp/a
/tmp/a
└── b
└── c
[root@localhost tmp]# rmdir a
rmdir: 刪除 “a” 失敗: 目錄非空
[root@localhost tmp]# rmdir -p a/b/c
mkdir: 創建目錄
格式: mkdir [選項] 目錄…
選項: -m MODE: 創建目錄時,設定目錄的訪問權限,默認權限爲755,可以使用此選項更改目錄的權限,權限可以是8進制數字格式,也可以指定ugo的權限
-p: 遞歸創建目錄,創建一個目錄,如果其父目錄不存在,則遞歸創建其父目錄
示例:
[root@localhost tmp]# mkdir -m 777 a
[root@localhost tmp]# ls -ld a
drwxrwxrwx. 2 root root 6 12月 10 20:06 a
[root@localhost tmp]# mkdir -m u=rwx,g=rw,o=rw b
[root@localhost tmp]# ls -ld b
drwxrw-rw-. 2 root root 6 12月 10 20:07 b
[root@localhost tmp]# mkdir /e/f/g
mkdir: 無法創建目錄”/e/f/g”: 沒有那個文件或目錄
[root@localhost tmp]# mkdir -p /e/f/g
[root@localhost tmp]#
touch: 創建文件或者修改文件的時間屬性
atime和mtime都可以更改,但ctime不能使用命令更改,通過更改atime和mtime,ctime會自動修改。
格式: touch [-acm][-r ref_file(參照文件)|-t time(時間值)] file(文件名)…
用法描述: touch 修改每個指定文件 file 的存取(access)和/或修改(modifica‐ tion)時間戳記. 除非使用-r或-t選項,這些時間戳記都將修改爲當前的時間. 使用-r選項時,這些戳記將按照文件–ref_file的時間戳記來修改(即變得和ref_file時間戳記值相同).使用-t選項,則這些戳記將按照給定的時間值 time 進行修改.同時使用或同時不使用選項-a和-m,存取和修改兩個戳記都將被更改.若只使用選項-a,則只修改存取戳記.同樣,只使用選項-m,則只修改修改戳記.若要修改戳記的文件尚不存在,除非使用-c選項,touch將創建它(作爲空文件,並賦予0666的模式且受umask值的限制)
選項:
-a, –time=atime, –time=access, –time=use
只修改存取時間.
-c, –no-create
如果要修改的文件不存在,此選項使touch不去創建它.
-m, –time=mtime, –time=modify
只修改修改時間.
-r file, –reference=file
使用參照文件 file 的時間戳記值修改指定文件的時間戳記.
-t decimtime
這裏時間值 decimtime 的格式爲MMDDhhmm[[CC]YY][.ss]
從左至右分別是月份,日期,小時,分鐘,可選的世紀和
年,以及可選的秒. touch 將用這組數值修改指定文件的
時間戳記.請注意,這個格式與POSIX標準所規定的格式並
不一樣.
實際的時間格式爲年年年年月月日日時時分分[秒秒]
示例:
1) 使用touch創建文件file1
[root@localhost tmp]# touch file1
[root@localhost tmp]# ll file1
-rw-r–r–. 1 root root 0 12月 10 20:16 file1
[root@localhost tmp]#
[root@localhost tmp]# stat file1
文件:”file1″
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:67167337 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-12-10 20:16:52.075000000 +0800
最近更改:2017-12-10 20:16:52.075000000 +0800
最近改動:2017-12-10 20:16:52.075000000 +0800
創建時間:-
2) 修改file1的atime爲2012年10月20日19點16分
[root@localhost tmp]# touch -a -t 201210201916 file1
[root@localhost tmp]# stat file1
文件:”file1″
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:67167337 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2012-10-20 19:16:00.000000000 +0800
最近更改:2017-12-10 20:16:52.075000000 +0800
最近改動:2017-12-10 20:33:04.285000000 +0800
創建時間:-
3) 使用touch -c,並不真正創建文件,但如果文件存在,會更新文件的三個時間戳
[root@localhost tmp]# touch -c file1
[root@localhost tmp]# stat file1
文件:”file1″
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:67167337 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-12-10 20:34:47.505000000 +0800
最近更改:2017-12-10 20:34:47.505000000 +0800
最近改動:2017-12-10 20:34:47.505000000 +0800
創建時間:-
[root@localhost tmp]# touch -c file2
4) 修改文件file1的mtime時間爲2012年10月20日19點16分
[root@localhost tmp]# touch -m -t 201210201916 file1
[root@localhost tmp]# stat file1
文件:”file1″
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:67167337 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-12-10 20:34:47.505000000 +0800
最近更改:2012-10-20 19:16:00.000000000 +0800
最近改動:2017-12-10 20:42:13.486000000 +0800
創建時間:-
5) 使用file1的時間戳來創建file3,注意ctime沒有同步
[root@localhost tmp]# stat file1
文件:”file1″
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:67167337 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-12-10 20:34:47.505000000 +0800
最近更改:2012-10-20 19:16:00.000000000 +0800
最近改動:2017-12-10 20:42:13.486000000 +0800
創建時間:-
[root@localhost tmp]# touch -r file1 file3
[root@localhost tmp]# stat file3
文件:”file3″
大小:0 塊:0 IO 塊:4096 普通空文件
設備:fd00h/64768d Inode:67167358 硬鏈接:1
權限:(0644/-rw-r–r–) Uid:( 0/ root) Gid:( 0/ root)
環境:unconfined_u:object_r:user_tmp_t:s0
最近訪問:2017-12-10 20:34:47.505000000 +0800
最近更改:2012-10-20 19:16:00.000000000 +0800
最近改動:2017-12-10 21:03:39.538000000 +0800
創建時間:-
cp: 複製文件和目錄
格式: cp SOURCE DEST
說明:
複製文件時,如果目錄是目錄,則將文件複製到目錄下,使用原文件名;如果目標是文件,且文件不存在,則會創建這個文件,將源文件的內容複製到目標文件中; 如果目標是文件且存在,則會提 示是否覆蓋
多源複製,指源爲多個文件時,指定的目標應該是一個目錄, 如果目標目錄不存在,則報錯
如果目標目錄存在,是非目錄文件,則報錯,如果目標目錄存在,是目錄文件,則將源複製到這個目錄下
選項:
-i: 覆蓋之前提醒用戶進行確認,有的版本,使用 cp命令時,別名有加-i選項
-f: force 如果目標文件存在,則強制覆蓋
-r: recursive,遞歸複製目錄及目錄中的內容到目標目錄
-d: 在複製符號鏈接作爲符號鏈接而不是複製它指向的文件,並且保護在副本中原文件之間的硬鏈接
-a: 複製時,儘可能保持文件的結構和屬性,(但不保持目錄 結構)等同於-dpR archive,用於實現歸檔;
–preserv=
mode 權限
ownership 屬主屬組
timestamps 時間戳
context 安全標籤
xattr: 擴展屬性
all: 上述所有屬性
示例:
1) 使用單源複製時,如果目標是目錄,則將文件複製到目錄中,不改名; 如果目標是文件,且不存在,則會創建文件,將源文件中的數據流複製到目標文件;如果目標文件存在,則會提示是否覆蓋
[root@localhost ~]# cp /etc/fstab /tmp
[root@localhost ~]# cd /tmp
[root@localhost tmp]# ls
fstab ks-script-rDmdoC yum.log
[root@localhost tmp]# cp /etc/fstab /tmp/fstab2
[root@localhost tmp]# cp /etc/fstab /tmp/fstab2
cp:是否覆蓋”/tmp/fstab2″?
2) 在CentOS中,cp命令是使用了alias的,默認添加了-i選項,即目標存在時,會提示是否覆蓋
3) 使用-r選項,會強制覆蓋已經存在的目標文件
[root@localhost tmp]# ls /tmp
fstab passwd
[root@localhost tmp]# cp -r /etc/fstab /tmp
cp:是否覆蓋”/tmp/fstab”? n
[root@localhost tmp]# \cp -r /etc/fstab /tmp
4) 帶權限複製文件,複製文件時,默認不復制文件的相關屬性,使用-a選項,帶屬性複製。 非管理員不能複製文件的相關屬性。
[root@localhost tmp]# cp -a fstab2 fstab3
[root@localhost tmp]# ll
總用量 16
-rw-r–r–. 1 root root 541 12月 10 21:54 fstab
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab2
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab3
-rw-r–r–. 1 root root 1151 12月 10 21:38 passwd
[root@localhost tmp]# cp fstab2 fstab4
[root@localhost tmp]# ll
總用量 20
-rw-r–r–. 1 root root 541 12月 10 21:54 fstab
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab2
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab3
-rw-r–r–. 1 root root 541 12月 10 22:05 fstab4
-rw-r–r–. 1 root root 1151 12月 10 21:38 passwd
rm: 移除文件或者目錄
格式: rm [options] file…
選項:
-f: force,在刪除時,不出現提示
-i: 進行確認提示,在刪除文件時,會提示
-r: 遞歸地移除目錄中的內容
示例:
顯示/tmp目錄下的的文件及目錄
[root@localhost ~]# tree /tmp
/tmp
├── a
│ └── b
│ └── c
│ └── d
│ └── e
├── fstab
├── fstab2
├── fstab3
├── fstab4
└── passwd
5 directories, 5 files
使用rm不帶參數來刪除一個目錄,顯示無法刪除
[root@localhost ~]# rm /tmp/a
rm: 無法刪除”/tmp/a”: 是一個目錄
使用fm -rf來強制刪除一個目錄,且遞歸進行刪除
[root@localhost ~]# rm -rf /tmp/a
[root@localhost ~]# tree /tmp
/tmp
├── fstab
├── fstab2
├── fstab3
├── fstab4
└── passwd
mv: 移動文件或者對文件改名
格式: mv [選項]… 源文件 目標文件
mv [選項]… 源文件… 目錄
mv [選項]… –target-directory=DIRECTORY SOURCE…
示例:
#將當前目錄下的fstab移動到當前目錄下,改名叫fstab
[root@localhost tmp]# ll
總用量 20
-rw-r–r–. 1 root root 541 12月 10 21:54 fstab
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab2
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab3
-rw-r–r–. 1 root root 541 12月 10 22:05 fstab4
-rw-r–r–. 1 root root 1151 12月 10 21:38 passwd
[root@localhost tmp]# mv fstab fstab5
[root@localhost tmp]# ll
總用量 20
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab2
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab3
-rw-r–r–. 1 root root 541 12月 10 22:05 fstab4
-rw-r–r–. 1 root root 541 12月 10 21:54 fstab5
-rw-r–r–. 1 root root 1151 12月 10 21:38 passwd
將當前目錄下的fstab5移動到下一級目錄a下
[root@localhost tmp]# mv fstab5 a/
[root@localhost tmp]# ll
總用量 16
drwxr-xr-x. 2 root root 20 12月 11 18:29 a
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab2
-rw-r–r–. 1 liuqing liuqing 541 12月 10 22:05 fstab3
-rw-r–r–. 1 root root 541 12月 10 22:05 fstab4
-rw-r–r–. 1 root root 1151 12月 10 21:38 passwd
[root@localhost tmp]# tree
.
├── a
│ └── fstab5
├── fstab2
├── fstab3
├── fstab4
└── passwd
使用–target-directory長選項移動文件
[root@localhost tmp]# mv –target-directory=’/tmp/a’ fstab3
[root@localhost tmp]# tree a
a
├── fstab3
├── fstab4
└── fstab5