Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

copy

ls

awk

Al my html files reside here :

/home/thinkcode/myfiles/html/

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

I have this so far. Please correct me. I am looking for a one-liner!

ls -lt *.htm | head -10 | awk '{print "cp "$1" "..\Test\$1}' | sh
like image 725
ThinkCode Avatar asked May 15 '12 15:05

ThinkCode


People also ask

Which command is used to display the 10 newest files in the current directory?

List files, displaying the most recently created or changed files first, by using the ls -t command.

How do I copy recent files in Linux?

To copy files and directories use the cp command under a Linux, UNIX-like, and BSD like operating systems. cp is the command entered in a Unix and Linux shell to copy a file from one place to another, possibly on a different filesystem.


1 Answers

ls -lt *.htm | head -10 | awk '{print "cp " $9 " ../Test/"$9}' | sh
like image 96
aphex Avatar answered Sep 26 '22 02:09

aphex