LinuxQuestions.org
Review your favorite Linux distribution.
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 01-21-2013, 03:34 AM   #1
ads1980
LQ Newbie
 
Registered: Jan 2013
Location: Colchester
Distribution: Centos, Ubuntu, Mint
Posts: 6

Rep: Reputation: Disabled
Trying to figure out how to copy from multiple sources to one desitnation


Ok so this is the problem I have and can only figure out a bit of it, as I haven't used Linux in about 10 years so my brain is very rusty!

I have a list of file names (over 5000!) I know that these files are located over 3 different files shares in either of the 2 directories in each files share, I then want to use this list (listoffiles.txt) to find the files in each location that makes a new (locationoffiles.txt) which is then passed to something like rsync to do the copy to a new destination I want to keep the directory structure.

what would be the correct script to do this???

as an added bonus I would like to add some user interaction if I need use this script again for something similar.

so I guess ask for the listoffiles.txt location and also the destination.

Many thanks for any help.

Ads
 
Old 01-21-2013, 03:50 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Make sure that all files in listoffiles.txt have mentioned with their full path as:-
Code:
/home/uesr/sample_dir1/file1
/home/uesr/sample_dir2/file2
...
Not just,
Code:
file1
file2
...
If it is, then, you can:-
Code:
#!/bin/bash
LIST=/path/to/listoffiles.txt  # Use full path of listoffiles.txt
DESTDIR=/path/to/destdir  # Mention path of destination dir.
while read -r file
do
cp $file $DESTDIR
echo "File $file copied."
done < $LIST
 
Old 01-21-2013, 03:59 AM   #3
ads1980
LQ Newbie
 
Registered: Jan 2013
Location: Colchester
Distribution: Centos, Ubuntu, Mint
Posts: 6

Original Poster
Rep: Reputation: Disabled
Many thanks for your prompt reply, but that's my first problem the list of files only has the file name not it's source as the list was generated by some software that told me only the filename as it doesn't know the location of the file, so that's why I thought I would need the find command first to get the location of file printed to a new .txt file

so I guess something like:

Quote:
#!/bin/bash
for f in `cat /path/to/listoffiles.txt`
do
find . -name $f -printf %p /path/to/locationoffiles.txt
LIST=/path/to/locationoffiles.txt # Use full path of locationoffiles.txt
DESTDIR=/path/to/destdir # Mention path of destination dir.
while read -r file
do
cp $file $DESTDIR
echo "File $file copied."
done < $LIST

Last edited by ads1980; 01-21-2013 at 04:39 AM.
 
Old 01-21-2013, 04:52 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Try it out:-

Code:
#!/bin/bash
LIST=/path/to/listoffiles.txt
NEWLIST=/path/to/newlistoffiles.txt
DESTDIR=/path/to/destdir

while read -r file
do
find / -name "$file" -print > $NEWLIST
while read -r newfile
do
cp $newfile $DESTDIR
echo "Copying done: $newfile"
done < $NEWLIST
done < $LIST
Note:
1. Become root and execute this script because find command may give you permission denied error for some files since you're seaching files in "/" directory (and this is because you don't know where they file are).
2. The find command may take a long time in searching for files (since no. of files in large), so script might take long time to complate.
3. First test this script on a list of few files and if everything goes fine, you can do it for whole listoffile.txt file. (I have tested it).
 
Old 01-21-2013, 04:57 AM   #5
ads1980
LQ Newbie
 
Registered: Jan 2013
Location: Colchester
Distribution: Centos, Ubuntu, Mint
Posts: 6

Original Poster
Rep: Reputation: Disabled
great thank you for you help, I'll do a small test and report back.
 
Old 01-22-2013, 11:10 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Make sure you properly quote all of your variables, particularly if the files could contain whitespace.

Code:
cp "$newfile" "$DESTDIR"
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
1 members found this post helpful.
Old 02-19-2013, 10:59 AM   #7
ads1980
LQ Newbie
 
Registered: Jan 2013
Location: Colchester
Distribution: Centos, Ubuntu, Mint
Posts: 6

Original Poster
Rep: Reputation: Disabled
HI forgot to report back , I managed to narrow down where to search as I had mounted my search drives to /mnt so added /mnt after find
 
Old 02-22-2013, 08:57 AM   #8
ads1980
LQ Newbie
 
Registered: Jan 2013
Location: Colchester
Distribution: Centos, Ubuntu, Mint
Posts: 6

Original Poster
Rep: Reputation: Disabled
ok if I just wanted to create the newlist how would I amend the code above?

Quote:
#!/bin/bash

while read -r file
do
find /directory/location -iname "$file" -print > /location/of/new/list.txt
done < /location/of/original/file/list.txt
 
Old 02-22-2013, 10:05 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Code:
#!/bin/sh --
LIST="/path/to/listoffiles.txt"; NEWLIST="/path/to/newlistoffiles.txt"

cat "${LIST}" | while read ITEM; do
 find /mnt/one/path /mnt/two/otherpath -type f -name "${ITEM}"
done > "${NEWLIST}"
exit 0
*Note you're looking for files so narrow it down with "-type f", note Linux and UNIX have case sensitivity (as in -name vs -iname) and also note the 'cp' command shivaa posted doesn't keep the directory structure like you asked. Depending on the amount of files, the file size, the amount of changes and the location your backup goes to you can use rsync, or tar on both ends of a pipe or cpio.
 
Old 02-22-2013, 10:21 AM   #10
ads1980
LQ Newbie
 
Registered: Jan 2013
Location: Colchester
Distribution: Centos, Ubuntu, Mint
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hey thanks unSpawn, so can you replace cp with rsync? sorry for the stupid questions, would you have to tell rsync to only process 200 at a time as I think there is limit right?
 
Old 02-22-2013, 11:09 AM   #11
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
No such limit I'm aware of.
 
  


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
[SOLVED] copy multiple files to multiple directories Sanderson Linux - Newbie 15 01-22-2013 10:17 AM
How to make a figure span multiple column in Latex lunarbof Programming 5 04-11-2011 04:33 AM
[SOLVED] how to copy sources from CD to HDD olcal Linux From Scratch 7 01-24-2011 02:06 PM
SVN working copy locked error updating e17 sources. The_Seraphim Linux - Software 0 04-28-2009 03:53 AM
how do I copy lfs sources from LiveCD to hard disk olcal Linux From Scratch 7 02-21-2008 11:45 AM

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

All times are GMT -5. The time now is 10:37 PM.

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