【问题标题】:Listing last 5 recent files and move them to another directory列出最近 5 个最近的文件并将它们移动到另一个目录
【发布时间】:2016-09-06 16:20:22
【问题描述】:

我正在尝试实现一个单行命令来列出目录中的最后 5 个新文件并将这些文件移动到另一个位置。 现在我可以列出它们,但还没有找到移动它们的方法,有什么提示吗?

ls -1t *.txt | head -5

我有:

$ ls -1t *.txt | head -5
record_-_53810.20160511_-_1053+0200.txt
record_-_53808.20160511_-_1048+0200.txt
record_-_53570.20160510_-_1508+0200.txt
record_-_53568.20160510_-_1503+0200.txt
record_-_53566.20160510_-_1458+0200.txt

【问题讨论】:

    标签: grep command-line-arguments ls mv solaris-10


    【解决方案1】:

    只需通过管道发送至xargs:

    ls -1t *.txt | head -5 | xargs -i mv {} another_dir/
    

    或者使用扩展本身:

    mv $(ls -1t *.txt | head -5) another_dir/
    

    甚至循环:

    while IFS= read -r file;
    do
       mv "$file" another_dir/
    done < <(ls -1t *.txt | head -5)
    

    【讨论】:

    • @hectorlerma 抱歉,未经测试。查看有效的更新。我还添加了另一种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2022-06-19
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多