Trying to figure out how to copy from multiple sources to one desitnation
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.
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.
#!/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
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
#!/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).
#!/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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.