[20201112]tmpwatch 刪除檔案.txt

lfree發表於2020-11-13

[20201112]tmpwatch 刪除檔案.txt

--//tmpwatch命令用來removes files which haven't been accessed for a period of time。
--//一般釋出版本用來透過crond呼叫tmpwatch刪除一定時間內不使用的檔案。

# cat /etc/cron.daily/tmpwatch
flags=-umc
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
        -X '/tmp/hsperfdata_*' 240 /tmp
/usr/sbin/tmpwatch "$flags" 720 /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 720 "$d"
    fi
done

--//flags=-umc表示
-u, --atime
    Make the decision about deleting a file based on the file's atime (access time). This is the default.
    Note that the periodic updatedb file system scans keep the atime of directories recent.

-m, --mtime
    Make the decision about deleting a file based on the file's mtime (modification time) instead of the atime.

-c, --ctime
    Make the decision about deleting a file based on the file's ctime (inode change time) instead of the atime; for
    directories, make the decision based on the mtime.

# man

 tmpwatch [-u|-m|-c] [-MUXadfqstvx] [--verbose] [--force] [--all]
                [--nodirs] [--nosymlinks] [--test] [--fuser] [--quiet]
                [--atime|--mtime|--ctime] [--dirmtime] [--exclude <path>]
                [--exclude-user <user>] [--exclude-pattern <pattern>]
                <hours> <dirs>

--//可以使用tmpwatch <hours> <dirs> 快速刪除檔案。僅僅需要注意第2個引數是小時,必須換算看看。
--//預設預設是atime.
By default, tmpwatch dates files by their atime (access time), not their mtime (modification time). If files aren't
being removed when ls -l implies they should be, use ls -u to examine their atime to see if that explains the problem.

--//建議加入-m引數。

# tmpwatch -m [time in hours] [directory]

--//簡單測試如下:
# mkdir -p /tmp/test
# touch  -am -d 20100909 aaa
# touch  -am -d 20201113 bbb

# echo  -e "aaa\nbbb" | xargs -IQ stat Q
  File: `aaa'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6802h/26626d    Inode: 11534342    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-09-09 00:00:00.000000000 +0800
Modify: 2010-09-09 00:00:00.000000000 +0800
Change: 2020-11-13 09:08:30.000000000 +0800
  File: `bbb'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 6802h/26626d    Inode: 11534343    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-11-13 00:00:00.000000000 +0800
Modify: 2020-11-13 00:00:00.000000000 +0800
Change: 2020-11-13 09:08:47.000000000 +0800

# tmpwatch -m $((24*2)) /tmp/test
--//表示刪除2天前修改的檔案。

# ls -l /tmp/test
total 0
-rw-r--r-- 1 root root 0 2020-11-13 00:00:00 bbb

--//你還可以透過-t引數先瀏覽看看,測試:
# touch  -am -d 20100909 aaa
# tmpwatch -t -m $((24*2)) /tmp/test
removing file /tmp/test/aaa

# tmpwatch -tv -m $((24*2)) /tmp/test
grace period is 172800
--//24*2*3600 = 172800 ,這個時間竟然是秒數。
cleaning up directory /tmp/test
removing file /tmp/test/aaa

--//這樣可以提到替換使用find命令,簡單實用。
--//另外補充一點預設oracle並不刪除連結,以及pipe,socket型別檔案。如果刪除許多oracle rac可能都出現問題。

#  ls -l /tmp/.oracle/ | head
total 0
srwxrwx--- 1 grid   oinstall 0 2014-12-11 02:30:20 master_diskmon
srwxr-xr-x 1 grid   oinstall 0 2014-12-11 02:30:18 mdnsd
prw-r--r-- 1 root   root     0 2014-10-10 20:53:50 npohasd
srwxrwxrwx 1 grid   oinstall 0 2014-12-11 02:30:26 ora_gipc_gipcd_dm01dbadm01
srwxrwxrwx 1 grid   oinstall 0 2014-12-11 02:30:19 ora_gipc_GPNPD_dm01dbadm01
srwxrwxrwx 1 root   root     0 2014-12-11 02:30:26 ora_gipc_sdm01dbadm01gridgxhospitalCRFM_CLIIPC
srwxrwxrwx 1 root   root     0 2014-12-11 02:30:26 ora_gipc_sdm01dbadm01gridgxhospitalCRFM_SIPC
srwxrwxrwx 1 oracle oinstall 0 2020-06-29 14:37:23 s#101813.1
srwxrwxrwx 1 oracle oinstall 0 2020-06-29 14:37:23 s#101813.2

#  tmpwatch -tv -m 24 /tmp/.oracle/
grace period is 86400
cleaning up directory /tmp/.oracle

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2733909/,如需轉載,請註明出處,否則將追究法律責任。

相關文章