ITPub博客

首页 > 数据库 > Oracle > [20151103]find+ls+rm .txt

[20151103]find+ls+rm .txt

原创 Oracle 作者:lfree 时间:2015-11-03 10:20:01 0 删除 编辑

[20151103]find+rm.txt

--今天检查机器发现一台机器的dataguard的磁盘快满了,要删除一些arcvhive log腾出一些空间.
--犯了一点小错误自己做一个记录.

--在删除前我做一下检查,我清楚记得要查询N天以前修改的文件使用-mtime +N命令:
--我执行如下,实际上记录是事后的,+N的时间不是原来的.

$ find  -mtime +49 -exec ls -l \;
...
-rw-r----- 1 oracle oinstall 1776432640 Nov  2 20:28 2_768_862334375.dbf
-rw-r----- 1 oracle oinstall      19968 Nov  2 20:28 2_769_862334375.dbf

--奇怪!结果我看见了前天的archivelog 文件.
--在这里浪费了N多时间,看文档才发现我打错了命令.

--我先执行如下:
$ ls -l |wc
    133    1058   10575

$ find  -mtime +49 -exec ls -l \; |wc
    532    4760   38604

--感觉奇怪,什么后面的比前面的多.看文档才发现我打错了命令.

$ find  -mtime +49 -print
./1_648_862334375.dbf
./1_647_862334375.dbf
./2_668_862334375.dbf
./2_669_862334375.dbf

--这里输出4行,实际上我想看看日期,-print参数无法看到,我才采用-exec 加命令的格式.结果我忘记加入{}.
--正确的应该执行如下:
$ find  -mtime +49 -exec ls -l {} \; | column -t
-rw-r-----  1  oracle  oinstall  13824      Sep  13  20:07  ./1_648_862334375.dbf
-rw-r-----  1  oracle  oinstall  175732736  Sep  13  20:07  ./1_647_862334375.dbf
-rw-r-----  1  oracle  oinstall  991492096  Sep  13  20:07  ./2_668_862334375.dbf
-rw-r-----  1  oracle  oinstall  49152      Sep  13  20:07  ./2_669_862334375.dbf

--加入column -t 主要是显示的需要整洁.正好4行,这样133*4=532,就很好的解析我前面遇到的情况.
--我上面的命令相当于执行ls -l 4次.(因为输出4行),怪不得我前面执行输出好长....

$ find . -mtime +50 -exec rm {} \;

--还是报错.主要是删除的文件太多.

--google看了一些文档,比较保险的做法是:
$ find . -mtime +50 -print0 |xargs -0 -r rm

Use the -r flag with xargs. If stdin is empty, xargs will not run the command, and exit.

--实际上ls命令linux下许多系统使用的别名.它的时间显示格式我个人非常不习惯,没有年.
--最好修改/etc/profile.d/colorls.sh加入,参考:
--http://blog.itpub.net/267265/viewspace-754738/

alias ll='ls -l --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null
alias l.='ls -d .* --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null
alias ls='ls --color=auto --time-style=+"%Y-%m-%d %H:%M:%S"' 2>/dev/null

--man ls
--time-style=STYLE
    with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT.  FORMAT is interpreted like 'date';
    if FORMAT is FORMAT1<newline>FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is
    prefixed with 'posix-', STYLE takes effect only outside the POSIX locale

--我测试一下使用 --time-style=long-iso也符合需求.

$ find  -mtime +49 -exec ls -l --time-style=long-iso {} \; | column -t
-rw-r-----  1  oracle  oinstall  13824      2015-09-13  20:07  ./1_648_862334375.dbf
-rw-r-----  1  oracle  oinstall  175732736  2015-09-13  20:07  ./1_647_862334375.dbf
-rw-r-----  1  oracle  oinstall  991492096  2015-09-13  20:07  ./2_668_862334375.dbf
-rw-r-----  1  oracle  oinstall  49152      2015-09-13  20:07  ./2_669_862334375.dbf

--这样显示对比前面要清楚许多.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-1821308/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7971650