Linux從0到1④檔案管理(建立複製移動刪除檢視)
阿新 • • 發佈:2018-11-19
建立
1.檔案(touch)
- touch命令用於建立空白檔案或者設定檔案時間
- 格式:touch [選項] [檔案]
[[email protected] ~]#touch file1.txt //無則建立,有則修改時間
[[email protected] ~]#touch file2 file3
[[email protected] ~]#touch /home/file4.txt
[[email protected] ~]#touch /home/{file5,file6}
[[email protected] ~]#touch file{7..15} //建立檔案file7,file8...file15
[[email protected] ~]#touch file{a..d} //建立檔案filea,fileb,filec,filed
[[email protected] ~]#touch file{e,f,g}
2. 目錄(mkdir)
- 用於建立空白的的目錄
- 格式:mkdir [選項] 目錄
- 相關引數:-p 遞迴建立父目錄
[[email protected] ~]#mkdir dir1 //建立目錄dir1
[ [email protected] ~]#mkdir /home/dir2 /home/dir3
[[email protected] ~]#mkdir /home{dir4,dir5}
[[email protected] ~]#mkdir -v /home/dir8/tong
[[email protected] ~]#mkdir -pv /home/dir8/tong/dir9
複製
- 用於複製檔案或目錄
- 格式:cp [選項] 原始檔 目標檔案
- 相關引數:-p 保留原始檔案的屬性
-d 物件為“連結檔案”,則保留該“連結檔案”的屬性
-r 遞迴持續複製(用於目錄)
-i 若目標檔案存在則詢問是否覆蓋
-v 顯示具體操作過程
-a = -pdr
三種情況:
1)目標檔案是目錄,則會把原始檔複製到該目錄中
[[email protected] ~]# cp -v anaconda-ks.cfg /home/
‘anaconda-ks.cfg’ -> ‘/home/anaconda-ks.cfg’
[[email protected] ~]# ls /home/
alice anaconda-ks.cfg
2)目標檔案也是普通檔案,則會詢問是否要覆蓋它
[[email protected] ~]# cp -v file1.txt /home/file04.txt
cp: overwrite ‘/home/file04.txt’? y
‘file1.txt’ -> ‘/home/file04.txt’
3)目標檔案不存在,則正常複製檔案
移動(mv)
- 用於剪下檔案(會把原始檔刪除掉,只保留剪下後的檔案)或者重新命名
(在同一個目錄中對一個檔案進行剪下=重新命名) - 格式:mv [選項] 原始檔 [目標檔案|目標檔名]
[[email protected] ~]# mv file1.txt /day4/file01.txt //將file1.txt移到/day4目錄下並重命名file01.txt
[[email protected] ~]# ls /day4/
file01.txt
[[email protected] ~]# mv /day4/file01.txt /day4/file02.txt //重新命名
[[email protected] ~]# ls /day4/
file02.txt
刪除(rm)
- 用於刪除檔案或目錄
- 格式:rm [選項] 檔案
- 相關引數:
-r 遞迴
-f 強制刪除
-v 顯示刪除的詳細過程
手動刪除:
[[email protected] ~]# rm -rfv /day4/
removed ‘/day4/file02.txt’
removed directory: ‘/day4/’
[[email protected] ~]# type -a cp //cp預設是cp -i
cp is aliased to `cp -i'
cp is /usr/bin/cp
指令碼刪除
檢視檔案內容
- cat:正序直接顯示檔案內容
[[email protected] ~]# cat file1.txt //顯示檔案內容
hello
world
[[email protected] ~]# cat -n file1.txt //-n顯示行號
1 hello
2 world
[[email protected] ~]# cat -A file1.txt //-A顯示包括控制字元(換行符,製表符)
hello$
world$
- tac:逆序顯示檔案內容
[[email protected] ~]# tac file1.txt
world
hello
- less more head
[[email protected] ~]# head -2 /etc/passwd //顯示檔案的前兩行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
- tail
[[email protected] ~]# tail -f /var/log/secure //-f 動態檢視檔案的尾部
- grep:可根據檔案內容進行過濾
[[email protected] ~]# grep 'root' /etc/passwd //過濾檔案中root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep '^operator' /etc/passwd //過濾以operator開頭的行
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep 'bash$' /etc/passwd //過濾檔案中以bash結尾的行
root:x:0:0:root:/root:/bin/bash
alice:x:1000:1000::/home/alice:/bin/bash
檔案時間
訪問時間:atime 即檢視內容的時間,RHEL以後會延後修改時間
修改時間:mtime 即修改內容的時間
改變時間:ctime 即改變檔案屬性的時間,例如許可權等
刪除時間:dtime 即檔案被刪除的時間
[[email protected] ~]# stat /etc/hostname //檢視檔案詳細屬性
.......... //省略
Access: 2018-09-29 14:14:58.119000683 +0800
Modify: 2018-09-22 23:48:41.144019526 +0800
Change: 2018-09-22 23:48:41.144019526 +0800
Birth: -
檢視檔案型別
- 方法一:
ls -l 檔名 //檢視第一個字元
- 普通檔案
d 目錄檔案(藍色)
b 裝置檔案(塊裝置)儲存裝置硬碟,U盤
c 裝置檔案(字元裝置)印表機,終端
s 套接字檔案
p 管道檔案
l 連結檔案
[[email protected] ~]# ls -l file1.txt //ls -l 相當於 ll
-rw-r--r--. 1 root root 12 Sep 29 22:49 file1.txt
[[email protected] ~]# ll anaconda-ks.cfg
-rw-------. 1 root root 1243 Aug 4 16:32 anaconda-ks.cfg
[[email protected] ~]# ll /dev/sda
brw-rw----. 1 root disk 8, 0 Sep 29 22:38 /dev/sda
- 方法二:
file 檔名
[[email protected] ~]# file anaconda-ks.cfg
anaconda-ks.cfg: ASCII text
[[email protected] ~]# file /dev/sda
/dev/sda: block special