目錄操作
移動 改名 批量改名,寫腳本
mv [選項] … 源文件或目錄… 目標文件或目錄 單個文件
移動 或者改名
-
-f:強制覆蓋,如果目標文件已經存在,則不詢問,直接強制覆蓋;
-
-i:交互移動,如果目標文件已經存在,則詢問用戶是否覆蓋(默認選項);
-
-n:如果目標文件已經存在,則不會覆蓋移動,而且不詢問用戶;
-
-v:顯示文件或目錄的移動過程;
批量
rename log log.bak* //所有 log文件改成 log.bak
rename 舊字符 新字符 目標文件
[root@test1 opt]# rename txt txt.bak * #將txt改成txt.bak [root@test1 opt]# ls a.txt.bak E.txt.bak j.txt.bak N.txt.bak R.txt.bak w.txt.bak A.txt.bak f.txt.bak J.txt.bak o.txt.bak s.txt.bak W.txt.bak b.txt.bak F.txt.bak k.txt.bak O.txt.bak S.txt.bak x.txt.bak B.txt.bak g.txt.bak K.txt.bak p.txt.bak t.txt.bak X.txt.bak c.txt.bak G.txt.bak l.txt.bak P.txt.bak T.txt.bak y.txt.bak C.txt.bak h.txt.bak L.txt.bak q.txt.bak u.txt.bak Y.txt.bak d.txt.bak H.txt.bak m.txt.bak Q.txt.bak U.txt.bak z.txt.bak D.txt.bak i.txt.bak M.txt.bak rh ? ? ? ? v.txt.bak Z.txt.bak e.txt.bak I.txt.bak n.txt.bak r.txt.bak V.txt.bak ?
rename 舊字符 新字符 目標文件
[root@test1 opt]# rename txt txt.bak *.txt
#把 txt 改成 txt.bak
?刪除文件
rm [選項] 要刪除的文件或目錄
-
-f、強制
-
-i、交互
-
-r 遞歸(刪除目錄需要)
root@localhost ~]# rm -rf / ? ?#不讓執行
[root@localhost ~]# echo * ? ? ?#刪根后查看
[root@localhost ~]# rm -rf /data/*
[root@localhost ~]# rm -rf /data? ? ##之間的區別誤操作,等于刪根
如何刪除特殊的文件
[root@localhost ~]# touch ./-f
# -開頭的文件
[root@localhost ~]# ls
[root@localhost ~]# rm -f -f
#刪除不掉
[root@localhost ~]# rm -f ./-f
#需要加路徑
[root@localhost ~]# touch ./-f
[root@localhost ~]# rm -- -f
rm:是否刪除普通空文件 "-f"?y
刪除文件后空間不釋放: 有別人在使用 這個大文件 造成空間不釋放
[root@localhost ~]# cp /dev/zero /boot/bigfile.img
cp: 寫入"/boot/bigfile.img" 出錯: 設備上沒有空間
cp: 擴展"/boot/bigfile.img" 失敗: 設備上沒有空間
[root@localhost ~]# df
文件系統 ? ? ? ? ? ? ? ? ? 1K-塊 ? ? 已用 ? 可用 已用% 掛載點
/dev/mapper/centos-root 10475520 10293876 181644 ? 99% /
devtmpfs ? ? ? ? ? ? ? ? ?917600 ? ? ? ?0 917600 ? ?0% /dev
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 933524 ? ?0% /dev/shm
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? 9216 924308 ? ?1% /run
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 933524 ? ?0% /sys/fs/cgroup
/dev/sda1 ? ? ? ? ? ? ? ?5232640 ?5232620 ? ? 20 ?100% /boot
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? 12 186696 ? ?1% /run/user/42
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? ?0 186708 ? ?0% /run/user/0
#磁盤使用滿了以后會造成服務的停止業務受影響,是一個重大的事故
[root@localhost ~]# ll -S /boot ? ? ? ? ? ? ##文件大小排序
總用量 5191344
-rw-r--r--. 1 root root 5171437568 8月 ?26 01:00 bigfile.img
-rw-------. 1 root root ? 69266920 8月 ? 6 21:02 initramfs-0-rescue-869dbd607e5e4f758fa152ef2229f2b6.img
-rw-------. 1 root root ? 30802796 8月 ? 6 21:03 initramfs-3.10.0-693.el7.x86_64.img
-rw-------. 1 root root ? 18808968 8月 ? 6 21:06 initramfs-3.10.0-693.el7.x86_64kdump.img
-rw-r--r--. 1 root root ? 10182684 8月 ? 6 21:02 initrd-plymouth.img
-rwxr-xr-x. 1 root root ? ?5877760 8月 ? 6 21:02 vmlinuz-0-rescue-869dbd607e5e4f758fa152ef2229f2b6
-rwxr-xr-x. 1 root root ? ?5877760 8月 ?23 2017 vmlinuz-3.10.0-693.el7.x86_64
-rw-------. 1 root root ? ?3228420 8月 ?23 2017 System.map-3.10.0-693.el7.x86_64
-rw-r--r--. 1 root root ? ? 293027 8月 ?23 2017 symvers-3.10.0-693.el7.x86_64.gz
-rw-r--r--. 1 root root ? ? 140894 8月 ?23 2017 config-3.10.0-693.el7.x86_64
drwx------. 5 root root ? ? ? ? 97 8月 ? 6 21:03 grub2
drwxr-xr-x. 2 root root ? ? ? ? 27 8月 ? 6 20:58 grub
drwxr-xr-x. 3 root root ? ? ? ? 17 8月 ? 6 20:57 efi
#使用另一個xshell打開 ?bigfile文件 也是可以刪除的,但是空間不會釋放
#文件正在使用可以刪除,
[root@localhost ~]# rm -rf /boot/bigfile.img?
[root@localhost ~]# df
文件系統 ? ? ? ? ? ? ? ? ? 1K-塊 ? ? 已用 ? 可用 已用% 掛載點
/dev/mapper/centos-root 10475520 10293964 181556 ? 99% /
devtmpfs ? ? ? ? ? ? ? ? ?917600 ? ? ? ?0 917600 ? ?0% /dev
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 933524 ? ?0% /dev/shm
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? 9216 924308 ? ?1% /run
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 933524 ? ?0% /sys/fs/cgroup
/dev/sda1 ? ? ? ? ? ? ? ?5232640 ?5232620 ? ? 20 ?100% /boot
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? 12 186696 ? ?1% /run/user/42
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? ?0 186708 ? ?0% /run/user/0
[root@localhost ~]# lsof |grep delete?
#過濾出被刪除的文件list open ?file
pulseaudi 1671 ? ? ? ? ? ? ? ? gdm ? ?6u ? ? ?REG ? ? ? ? ? ? ? ?0,4 ? 67108864 ? ? ?23475 /memfd:pulseaudio (deleted)
alsa-sink 1671 1674 ? ? ? ? ? ?gdm ? ?6u ? ? ?REG ? ? ? ? ? ? ? ?0,4 ? 67108864 ? ? ?23475 /memfd:pulseaudio (deleted)
alsa-sour 1671 1677 ? ? ? ? ? ?gdm ? ?6u ? ? ?REG ? ? ? ? ? ? ? ?0,4 ? 67108864 ? ? ?23475 /memfd:pulseaudio (deleted)
vim ? ? ? 7532 ? ? ? ? ? ? ? ?root ? ?3r ? ? ?REG ? ? ? ? ? ? ? ?8,1 5171437568 ? ? ? ? 80 /boot/bigfile.img (deleted)
[root@localhost ~]# w
#查看誰在使用什么進程
?01:09:59 up ?8:49, ?2 users, ?load average: 0.98, 0.57, 0.27
USER ? ? TTY ? ? ?FROM ? ? ? ? ? ? LOGIN@ ? IDLE ? JCPU ? PCPU WHAT
root ? ? pts/0 ? ?192.168.91.1 ? ? 00:54 ? ?7.00s ?0.12s ?0.01s w
root ? ? pts/1 ? ?192.168.91.1 ? ? 01:05 ? ?4:07 ? 4:07 ? 4:06 ?vim /boot/bigfile.img
#另一終端退出使用空間被釋放
[root@localhost ~]# df?
文件系統 ? ? ? ? ? ? ? ? ? 1K-塊 ? ? 已用 ? ?可用 已用% 掛載點
/dev/mapper/centos-root 10475520 10293868 ?181652 ? 99% /
devtmpfs ? ? ? ? ? ? ? ? ?917600 ? ? ? ?0 ?917600 ? ?0% /dev
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 ?933524 ? ?0% /dev/shm
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? 9216 ?924308 ? ?1% /run
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 ?933524 ? ?0% /sys/fs/cgroup
/dev/sda1 ? ? ? ? ? ? ? ?5232640 ? 182388 5050252 ? ?4% /boot
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? 12 ?186696 ? ?1% /run/user/42
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? ?0 ?186708 ? ?0% /run/user/0
[root@localhost ~]# echo"" > /boot/bigfile.img?
#寫入空文件,依賴shell
[root@localhost ~]# df
文件系統 ? ? ? ? ? ? ? ? ? 1K-塊 ? ? 已用 ? ?可用 已用% 掛載點
/dev/mapper/centos-root 10475520 10293892 ?181628 ? 99% /
devtmpfs ? ? ? ? ? ? ? ? ?917600 ? ? ? ?0 ?917600 ? ?0% /dev
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 ?933524 ? ?0% /dev/shm
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? 9216 ?924308 ? ?1% /run
tmpfs ? ? ? ? ? ? ? ? ? ? 933524 ? ? ? ?0 ?933524 ? ?0% /sys/fs/cgroup
/dev/sda1 ? ? ? ? ? ? ? ?5232640 ? 182388 5050252 ? ?4% /boot
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? 12 ?186696 ? ?1% /run/user/42
tmpfs ? ? ? ? ? ? ? ? ? ? 186708 ? ? ? ?0 ?186708 ? ?0% /run/user/0
[root@localhost ~]# rm -rf /boot/bigfile.img
#清空文件