[問題] 請問 find 排除某些檔案的方式

看板Linux作者 (′‧ω‧‵)時間8年前 (2017/06/30 02:59), 編輯推噓1(107)
留言8則, 3人參與, 最新討論串1/2 (看更多)
小弟想要每日刪除在/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 排除某些特定檔案一次殺光其他檔案嗎?? 感恩~ -- (。 。) (。 。) (。 。) (。 。) ︴︴︴︴ ︴︴︴︴ ︴︴︴︴ ︴︴︴︴ (。 。) (。 。) (。 。) (。 。) ︴︴︴︴ ︴︴︴︴ ︴︴︴︴ ︴︴︴︴ (。 。) (。 。) (。 。) (。 。) ︴︴︴︴ ︴︴︴︴ ︴︴︴︴ ︴︴︴︴ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.34.54.25 ※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1498762767.A.2BE.html

06/30 10:41, , 1F
find /opt/123/log/* -not -type l -delete
06/30 10:41, 1F

06/30 10:42, , 2F
這樣會排除掉 symbol link 的檔案
06/30 10:42, 2F

06/30 10:43, , 3F
不過資料夾下如果只有 regular file 要刪的話建議
06/30 10:43, 3F

06/30 10:43, , 4F
find /opt/123/log/* -type f -delete
06/30 10:43, 4F

06/30 12:25, , 5F
謝謝 那symbol link 指向的檔案也要一起排除呢?
06/30 12:25, 5F

06/30 21:49, , 6F
find /opt/123/log/ -type l | \
06/30 21:49, 6F

06/30 21:49, , 7F
(sed 's/^/! -samefile /;s/$/ -a/';echo ' -type f') | \
06/30 21:49, 7F

06/30 21:49, , 8F
xargs find -L | xargs rm
06/30 21:49, 8F
文章代碼(AID): #1PLKuFA- (Linux)
文章代碼(AID): #1PLKuFA- (Linux)