【问题标题】:Unix command to Gzip the files and move it to home directory用于 Gzip 文件并将其移动到主目录的 Unix 命令
【发布时间】:2013-02-23 10:06:33
【问题描述】:

以下 Unix 命令用于获取最近 30 分钟修改文件的列表,这些文件完美运行。

touch -t 02231249.00 /tmp/last30min
find /mydirectory -type f -newer /tmp/last30min
rm /tmp/last30min

谁能给我提供gzip这些文件并将其移动到主目录或tmp目录的命令。

感谢您的帮助!!!。

【问题讨论】:

  • gzip <file> && mv <file>.gz ~

标签: unix command gzip


【解决方案1】:

用空字符分隔的 find 命令的参数(如果您的文件名包含空格很重要)传送到 xargs 以完成这项工作

find /mydirectory -type f -newer /tmp/last30min -print0 | xargs -0 -I{} sh -c 'gzip "{}"; mv "{}".gz ~'

其中-I{} 告诉xargs 将命令中的每个{} 替换为输入行,即find 找到的当前文件。

如果您使用的是 Z shell (zsh),那就更简单了,一切都可以在 oneliner 中完成:

for i (/mydirectory/**/*(mm-30)) { gzip $i && mv $i.gz ~ }

这里**递归搜索,(mm-30)表示m在最后(-) 30个minutes中修改。

【讨论】:

    【解决方案2】:

    您的触摸命令无法正常工作,我检查了时间戳,它们适用于我所在时区的以下日期:

    $ touch -t 02231249.00 /tmp/last30min
    $ perl -e'print scalar localtime((stat("/tmp/last30min"))[9])'
    Sat Feb 23 12:49:00 2013jamie@jamie-Ideapad-Z570:~/temp$ 
    

    我认为这个命令会满足你的要求

    for f in `find . -mmin -30 -print`;do echo $f;gzip -c $f > $HOME/$f.gz;done
    

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 2018-06-28
      • 2013-08-26
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      相关资源
      最近更新 更多