How to copy the top 10 most recent files from one directory to another?

前端 未结 4 570
盖世英雄少女心
盖世英雄少女心 2021-02-14 10:04

Al my html files reside here :

/home/thinkcode/myfiles/html/

I want to move the newest 10 files to /home/thinkcode/Test

I

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 10:58

    ls -lt *.html | head -10 | awk '{print $NF}' | xargs -i cp {} DestDir
    

    In the above example DestDir is the destination directory for the copy.

    Add -t after xargs to see the commands as they execute. I.e., xargs -i -t cp {} DestDir.

    For more information check out the xargs command.

    EDIT: As pointed out by @DennisWilliamson (and also checking the current man page) re the -i option This option is deprecated; use -I instead..

    Also, both solutions presented depend on the filenames in questions don't contain any blanks or tabs.

提交回复
热议问题