Copying latest 10 files from one folder to another
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Copying latest 10 files from one folder to another
Hello, this is my first forum and first post i need help with a script, i want to copy the latest 10 files from one holder to another. All the files start with the number 2 and this is what i have so far:
lt -lt 2* | head -10
So i want to copy that output of 10 files to another folder
Grail i feel stupid for saying this but apart from know that bash is a type of shell i dont how to look at process substitution. I am really a newbie. Sorry
Process substitution allows you to create a list of arguments, from the output of another command or commands: $( command(s) )
mv $(ls -t | head -n 10) destination_dir/
If there may be spaces or some other special characters in the filenames, you will need to modify this. This is because spaces are used to separate arguments by bash.
One way of dealing with this is to convert new lines to nulls ( \0 ) to separate arguments, and use xargs.
ls -t | head -n 10 | tr '\n' '\0' | xargs -0 -t destination_dir/
The -0 (--null) option causes xargs to use NULLs to separate arguments, and ignore other characters.
the command "tr '\n' '\0'" converts newline characters to NULLs.
Thanks jschiwal, man you sound like you talking spanish, learning linux looks like it will be a daunting task. Thank you for your "mv" solution, the file names are just a list of numbers starting with 2 so i dont think it will give me problems. Thanks again.
Glad you have a solution. I like to give newbies the chance to look stuff up, but I went to bed after my post so didn't see your initial reply.
Also be aware that mv will move the files to the new location whereas cp will copy them, if you also need them left where they started.
Please mark as SOLVED if you have your final solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.