請問 find 排除某些檔案的方式 - Linux

Table of Contents


小弟想要每日刪除在/opt/123/log 內的所有檔案,但是要排除連結指向的檔案...

以下是目錄底下的資料:

/opt/123/log/aaa.ERROR -> ERROR20170622-091436.1204
/opt/123/log/aaa.WARNING -> ERROR20170622-211432.4204
/opt/123/log/aaa.INFO -> INFO20170622-221431.1927
/opt/123/log/bbb.ERROR -> ERROR20170622-091432.1477
/opt/123/log/bbb.WARNING -> ERROR20170622-111432.8437
/opt/123/log/bbb.INFO -> INFO20170622-091438.1277


其餘還有一大堆類似的檔案,上面六個檔案檔名會變動也不能搬移 = =

有方法可以寫 shell script 用 find 排除某些特定檔案一次殺光其他檔案嗎??

感恩~

--
(。 。) (。 。) (。 。) (。 。)
︴︴︴︴ ︴︴︴︴ ︴︴︴︴ ︴︴︴︴
(。 。) (。 。) (。 。) (。 。)
︴︴︴︴ ︴︴︴︴ ︴︴︴︴ ︴︴︴︴
(。 。) (。 。) (。 。) (。 。)
︴︴︴︴ ︴︴︴︴ ︴︴︴︴ ︴︴︴︴

--

All Comments

Hazel avatarHazel2017-07-01
find /opt/123/log/* -not -type l -delete
Ophelia avatarOphelia2017-07-03
這樣會排除掉 symbol link 的檔案
Olive avatarOlive2017-07-04
不過資料夾下如果只有 regular file 要刪的話建議
find /opt/123/log/* -type f -delete
Andy avatarAndy2017-07-08
謝謝 那symbol link 指向的檔案也要一起排除呢?
Kumar avatarKumar2017-07-11
find /opt/123/log/ -type l | \
(sed 's/^/! -samefile /;s/$/ -a/';echo ' -type f') | \
xargs find -L | xargs rm