1
bcxx 2014-10-02 08:51:22 +08:00 1
纯 shell 的话就用 date 来求 time difference 然后删除就好了吧
当然你想好看点可以直接用 python 之类的脚本来写 datetime ~_~ |
2
fising 2014-10-02 08:56:08 +08:00 1
find ./ -mtime 1 -delete
|
3
fising 2014-10-02 08:57:57 +08:00 1
未测试,反正命名是 find xxxx -delete
|
4
fising 2014-10-02 09:04:56 +08:00 1
find ./ -mtime +1 -delete
or find ./ -mtime +1 -exec rm -rf {} \; |
5
Oleg 2014-10-02 09:06:26 +08:00 1
#!/bin/sh
find /dirname -mtime +1 -name "%Y%m%d*_1000005.mp4" -exec rm -f {} \; (未测试) |
6
fising 2014-10-02 09:09:09 +08:00 2
楼主你可以通过
find ./ -mtime +1 -print 命令来显示符合删除条件的文件列表,用于测试 |
7
andybest OP @fising @Oleg 多谢,mtime 参数非常灵,可在 OpenWrt 上竟不支持 mtime 参数:
root@OpenWrt:# find --help BusyBox v1.19.4 (2014-03-23 07:54:36 CET) multi-call binary. Usage: find [PATH]... [OPTIONS] [ACTIONS] Search for files and perform actions on them. First failed action stops processing of current file. Defaults: PATH is current directory, action is '-print' -follow Follow symlinks -xdev Don't descend directories on other filesystems -maxdepth N Descend at most N levels. -maxdepth 0 applies actions to command line arguments only -mindepth N Don't act on first N levels -depth Act on directory *after* traversing it Actions: ( ACTIONS ) Group actions for -o / -a ! ACT Invert ACT's success/failure ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2 ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2 Note: -a has higher priority than -o -name PATTERN Match file name (w/o directory name) to PATTERN -iname PATTERN Case insensitive -name -path PATTERN Match path to PATTERN -ipath PATTERN Case insensitive -path -regex PATTERN Match path to regex PATTERN -type X File type is X (one of: f,d,l,b,c,...) -perm MASK At least one mask bit (+MASK), all bits (-MASK), or exactly MASK bits are set in file's mode -user NAME/ID File is owned by given user -group NAME/ID File is owned by given group -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.)) +/-N: file size is bigger/smaller than N -prune If current file is directory, don't descend into it If none of the following actions is specified, -print is assumed -print Print file name -print0 Print file name, NUL terminated -exec CMD ARG ; Run CMD with all instances of {} replaced by file name. Fails if CMD exits with nonzero |
10
ksunliang 2014-10-02 10:14:45 +08:00 1
v$ ls
20140928161800_0_0_1000005.mp4 20140930162011_0_0_1000005.mp4 20140928162003_0_0_1000005.mp4 20140930162019_0_0_1000005.mp4 20140928162006_0_0_1000005.mp4 20141001161800_0_0_1000005.mp4 20140928162011_0_0_1000005.mp4 20141001162003_0_0_1000005.mp4 20140928162019_0_0_1000005.mp4 20141001162006_0_0_1000005.mp4 20140929161800_0_0_1000005.mp4 20141001162011_0_0_1000005.mp4 20140929162003_0_0_1000005.mp4 20141001162019_0_0_1000005.mp4 20140929162006_0_0_1000005.mp4 20141002161800_0_0_1000005.mp4 20140929162011_0_0_1000005.mp4 20141002162003_0_0_1000005.mp4 20140929162019_0_0_1000005.mp4 20141002162006_0_0_1000005.mp4 20140930161800_0_0_1000005.mp4 20141002162011_0_0_1000005.mp4 20140930162003_0_0_1000005.mp4 20141002162019_0_0_1000005.mp4 20140930162006_0_0_1000005.mp4 v$ rm -f $(ls *.mp4| grep -v "$(date +%Y%m%d)") 20140928161800_0_0_1000005.mp4 20140928162003_0_0_1000005.mp4 20140928162006_0_0_1000005.mp4 20140928162011_0_0_1000005.mp4 20140928162019_0_0_1000005.mp4 20140929161800_0_0_1000005.mp4 20140929162003_0_0_1000005.mp4 20140929162006_0_0_1000005.mp4 20140929162011_0_0_1000005.mp4 20140929162019_0_0_1000005.mp4 20140930161800_0_0_1000005.mp4 20140930162003_0_0_1000005.mp4 20140930162006_0_0_1000005.mp4 20140930162011_0_0_1000005.mp4 20140930162019_0_0_1000005.mp4 20141001161800_0_0_1000005.mp4 20141001162003_0_0_1000005.mp4 20141001162006_0_0_1000005.mp4 20141001162011_0_0_1000005.mp4 20141001162019_0_0_1000005.mp4 v$ ls 20141002161800_0_0_1000005.mp4 20141002162011_0_0_1000005.mp4 20141002162003_0_0_1000005.mp4 20141002162019_0_0_1000005.mp4 20141002162006_0_0_1000005.mp4 |
12
jasontse 2014-10-02 12:21:37 +08:00 via iPad
|
13
achenge07 2014-10-02 13:01:08 +08:00
确定有毫秒信息吗? 我看每个相隔2分零三秒,24小时能拍n=24*60*60/123 = 702.43902439024段,保留最新的703个呗
|
14
kfll 2014-10-02 17:38:42 +08:00 via Android
楼主要是嫌麻烦的话,可以先清理一次,然后cron每天清理前一天的
|
15
xylophone21 2014-10-02 18:23:40 +08:00 1
@kfll 说的没错
如果不需要特别精确的话 用date组出20140927字符串,然后rm 20140927*即可。(略改需求) 如果要求精确高的话,列出所有文件,一个一个比吧,希望组出一个rm命令还是比较麻烦的。 |
16
msg7086 2014-10-02 21:55:20 +08:00 1
保留最新的703个是个不错的想法,而且其实不一定是703个吧,保留1000个不行么 -_-
直接 ls *.mp4 | head -n -703 然后扔给rm不就行了。 |
17
walleL 2014-10-02 23:30:57 +08:00 1
root@OpenWrt:~# ls *.mp4 -1
20140928162206_0_0_1000005.mp4 20141001152206_0_0_1000005.mp4 20141001154006_0_0_1000005.mp4 20141001162206_0_0_1000005.mp4 20141002162206_0_0_1000005.mp4 root@OpenWrt:~# sh d.sh /bin/rm -vf 20140928162206_0_0_1000005.mp4 /bin/rm -vf 20141001152206_0_0_1000005.mp4 root@OpenWrt:~# date Thu Oct 2 15:25:37 UTC 2014 root@OpenWrt:~# cat d.sh #!/bin/sh CUT_TIME=$(date -d@$(($(date +%s)-86400)) +%s) ls *_0_0_1000005.mp4 | while read FILE;do FILE_TIME=$(echo "$FILE" | cut -c1-12 | xargs date +%s -d) test $FILE_TIME -lt $CUT_TIME && echo /bin/rm -vf "$FILE" done root@OpenWrt:~# |