【摘要】Linux如何刪除大量小文件?本文介紹了兩種方法。
【作者】趙靖宇
環(huán)境:
RHEL 6.5 + Oracle 11.2.0.4
需求:
使用df -i巡檢發(fā)現(xiàn)Inodes使用率過高,需要清理刪除文件來解決。如果Inodes滿,該目錄將不能寫,即使df -h查看還有剩余空間。
1.問題現(xiàn)象
Oracle的adump下記錄的是sys的登陸審計信息,特點是小碎文件非常多,經(jīng)常會遇到使用rm -rf *命令刪除不了,報錯-bash: /bin/rm: Argument list too long。
這是因為通配符*在執(zhí)行時會替換為具體的文件名,例如rm -rf file1 file2 file3 ...,如果文件數(shù)量過多,就容易出現(xiàn)這個錯誤。
比如在下面的環(huán)境中,adump目錄下文件已達到114萬+,執(zhí)行rm -rf *命令時就會報這個錯誤:
[oracle@jystdrac2 adump]$ pwd
/opt/app/oracle/admin/crmdb/adump
[oracle@jystdrac2 adump]$ ls|wc -l
1149787
[oracle@jystdrac2 adump]$ rm -rf *
-bash: /bin/rm: Argument list too long
[oracle@jystdrac2 adump]$ du -sh
4.4G
2.解決方案
清楚了問題現(xiàn)象,解決方案就從除去rm -rf *命令的方式之外,還有哪些方法可用,如果通過網(wǎng)絡(luò)搜索,可能會找到結(jié)合find命令再去執(zhí)行rm的方式,但其實效率非常差,具體寫法這里就不列出了,因為我們通常也不會這樣處理。那么如何較為效率的刪除大批小文件呢?結(jié)合網(wǎng)絡(luò)的經(jīng)驗,并實測驗證,最終總結(jié)了兩種常見的解決方案,效率上也都尚可。
方案一:巧用rsync的方式達到刪除目的
建立一個空文件夾,使用rsync --delete-before -d <空文件夾> <需要清理刪除小文件的目錄>命令最終達到刪除大批小文件的目的。下面演示具體操作:???????
[oracle@jystdrac2 adump]$ mkdir /data/null
[oracle@jystdrac2 adump]$ ls -l /data/null
total 0
[oracle@jystdrac2 ~]$ nohup rsync --delete-before -d /data/null/ /opt/app/oracle/admin/crmdb/adump/ &
使用man rsync查看rsync命令相關(guān)的參數(shù)說明如下:???????
-d, --dirs transfer directories without recursing
--delete-before receiver deletes before transfer (default)
方案二:使用find命令的delete參數(shù)
使用find <需要清理刪除小文件的目錄> -type f -delete命令直接刪除大批小文件。
使用man find查看find命令相關(guān)的參數(shù)說明如下:???????
-type c
File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to
search for symbolic links when -L is in effect, use -xtype.
s socket
D door (Solaris)
-delete
Delete files; true if removal succeeded. If the removal failed, an error message is issued. If -delete fails, find’s exit status will be nonzero
(when it eventually exits). Use of -delete automatically turns on the ‘-depth’ option.
Warnings: Don’t forget that the find command line is evaluated as an expression, so putting -delete first will make find try to delete everything
below the starting points you specified. When testing a find command line that you later intend to use with -delete, you should explicitly spec-
ify -depth in order to avoid later surprises. Because -delete implies -depth, you cannot usefully use -prune and -delete together.
下面演示具體操作:
[oracle@jystdrac1 adump]$ nohup find /opt/app/oracle/admin/crmdb/adump/ -type f -delete &
可以參考下面的命令來簡單監(jiān)控刪除過程中Inodes使用率的變化:
while true; do df -i /; sleep 10; done
比如我這里節(jié)點jystdrac1使用的find方法,節(jié)點jystdrac2使用的rsync方法,實際觀察Inodes釋放速度區(qū)別并不大:???????
# 使用的find方法,觀察Inodes釋放速度:
[oracle@jystdrac1 ~]$ while true; do df -i /; sleep 10; done
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1519124 287772 85% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1519015 287881 85% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1513880 293016 84% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1511132 295764 84% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1502434 304462 84% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1494583 312313 83% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1489111 317785 83% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 1487629 319267 83% /
# 使用的rsync方法,觀察Inodes釋放速度:
[oracle@jystdrac2 ~]$ while true; do df -i /; sleep 10; done
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 963029 843867 54% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 955037 851859 53% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 953088 853808 53% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 950523 856373 53% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 948754 858142 53% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 944613 862283 53% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 942619 864277 53% /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/mapper/vg_linuxbase-lv_root 1806896 938510 868386 52% /
既然兩種方式差異不算大,那就根據(jù)需求或個人習(xí)慣選擇即可。我自己更傾向于使用方案二,因為這樣無需創(chuàng)建空目錄,操作上也更直觀。
最后再總結(jié)下刪除大量小文件的方法:???????
# 方案一:
mkdir <空文件夾>
rsync --delete-before -d <空文件夾> <需要清理刪除小文件的目錄>
# 方案二:
find <需要清理刪除小文件的目錄> -type f -delete
相對來說這兩種方式都比較效率,但由于整體小文件也是比較多,所以實際可以選擇nohup放到后臺執(zhí)行。文章來源:http://www.zghlxwxcb.cn/news/detail-713962.html
原題:Linux如何刪除大量小文件文章來源地址http://www.zghlxwxcb.cn/news/detail-713962.html
到了這里,關(guān)于Linux 刪除大量小文件的兩種方案 | 運維進階的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!