LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-19-2010, 09:04 AM   #1
kingjaffi
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
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
 
Old 11-19-2010, 09:13 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Have a look at process substitution on your favourite bash page and you should be good
 
Old 11-19-2010, 09:22 AM   #3
kingjaffi
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 11-19-2010, 09:33 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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.
 
Old 11-19-2010, 09:39 AM   #5
kingjaffi
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 11-20-2010, 02:09 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] copying folder structure with files. myusuf3 Linux - Newbie 1 07-13-2010 02:20 PM
copying folder to another withtout overwritting the destinaion files basher400 Linux - General 7 07-19-2009 10:54 AM
Copying Latest Files to Another Folder nformosa Programming 9 04-23-2009 07:40 AM
copying folder and changing extension of files in it? stratotak Linux - Newbie 18 03-10-2009 02:16 AM
automatically inherit parent folder permissions when copying files teixeira Linux - Newbie 3 07-08-2008 01:21 PM


All times are GMT -5. The time now is 06:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration