LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 02-12-2015, 07:55 AM   #1
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Rep: Reputation: Disabled
copy many files in to many directories


Hello every one,

I need to copy many files in to many folders in once, for example I have files result_1 result_2 result_3 and want to copy them to directories 1 2 3 respectively, how can I do that, I have tried cp, echo and find syntax but did not work out.
Any idea?
 
Old 02-12-2015, 08:09 AM   #2
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Just three files to just three directories. If you give big sample data, it might be of help.
 
Old 02-12-2015, 08:12 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Anyway for these 3 files and dirs here it is:

Code:
for id in {1..3}; do
echo result_${id}
cp result_"${id}" "${id}"
done
 
1 members found this post helpful.
Old 02-12-2015, 08:12 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Code:
while read
	do
		filename=$(basename "$REPLY")
		folder=${REPLY#?*_}
		mkdir $folder
		mv $REPLY  $folder
	done < <(find -iname "*_*")
Simple script will do it
I will leave error checking and file globing white space etc for you to do yourself.
 
Old 02-12-2015, 09:01 AM   #5
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
This code worked very well,

for id in {1..3}; do
echo result_${id}
cp result_"${id}" "${id}"
done

Thank you, but I need to change the name of the files after I moved them to the directories from result_# to just result, is it possible with this code to do that?
They are over 50 files and dirs.
 
Old 02-12-2015, 09:21 AM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Yes just rename them in the mv command see the man page and also have a look at the advanced bash scripting guide, renaming files as you move/copy them is pretty basic we aint going to do all the work for you, you have enogh info here to complete the script, if you get stuck on a particular bit then say so and we will point you in the right direction, what won't happen is getting a complete script written for you you have to do some of theis yourself.
 
Old 02-13-2015, 02:35 AM   #7
Ave3
LQ Newbie
 
Registered: Oct 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
I definitely know about renaming files, mv, cp and rename command, what I needed to rename all the files result_# to result in once, it is a big process what I do if I rename it individually that means I have to rename 5000 (50 x 100)times the files, I have tried find syntax which I find very strong but it didn't work with this case.
I surely try every thing and when things are not working out then I come to this forum.
 
Old 02-13-2015, 05:18 AM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
If you don't want to rename them as you go ( 5000 file moves is not dificult in BASH ), mv them athen use find with the -exec switch and rename to rename the files, but I would do it all from the mv in the script.
 
  


Reply



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
How to copy files to multiple directories kobeson Linux - Newbie 5 01-13-2014 12:19 PM
Copy Directories and its files to a new directory just as it is arundhyoti Linux - Newbie 7 07-13-2012 03:50 AM
script to find new files and copy them into different directories brunoschwartz Linux - Newbie 1 10-29-2010 07:38 PM
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 04:57 PM
Copy files from different directories fiomba Linux - Software 3 02-11-2005 03:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:05 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
Open Source Consulting | Domain Registration