Copy files from input file with dir structure

hi,

I want to copy files from source directory based on input file (or output of previous command) and i want to have the SAME DIRECTORY STRUCTURE.
Note that i will have other files and directories which i dont want to copy to destination.

For example, dir source has following content:

/xyz/Test/testsrc.c /xyz/nmake/build.mak /abc/1.c /abc/1.h ../proj_src/2.c ../proj_src/2.h /abc/svn/.svn  xyz/dump/dump.log

input file contains:

/xyz/Test/testsrc.c /xyz/nmake/build.mak /abc/1.c

first i split fields in to new line using awk , make a substitution with sed.
right now i am able to copy all the files to destination directory to read from file using while loop but i dont get the files as per dir structure.

Actual output:

Dest/testsrc.c Dest/build.mak Dest/1.c 

Expected output:

Dest/xyz/Test/testsrc.c Dest/xyz/nmake/build.mak Dest/abc/1.c 

I appreciate your help.

Something like this?

while read file
do
  dest=Dest${file}
  cp "$file" "$dest"
done < inputfile

hi,

it didnt work .

ls source && cat source/input
abc  Dest  input  xyz
/xyz/Test/testsrc.c
/xyz/nmake/build.mak
/abc/1.c

code:

while read file; do dest=/Dest${file}; cp "$file" "$dest"; done < input

error info:

cp: cannot stat `/xyz/Test/testsrc.c': No such file or directory
cp: cannot stat `/xyz/nmake/build.mak': No such file or directory
cp: cannot stat `/abc/1.c': No such file or directory

what am i missing here?

Do the files exist in those directories?

yes it is :cool:

BTW, there must be a slash before the destination directory assuming you have a directory with the fullname /Dest/xyz/Test/ :

while read file
do
  dest=/Dest${file}
  cp "$file" "$dest"
done < inputfile

Is /xyz/Test/ the fullname of the directory or is this a subdirectory?

Can you copy the files manually?

hi,

working one (not expected result)

while read file; do dest=Dest; cp "$file" "$dest"; done < input
Input contains:
xyz/Test/testsrc.c
xyz/nmake/build.mak
abc/1.c

im confused why it did not work when i have slash at first in input file!
i gave full path for dest and it didnt work either.

What are the full names of the source and destination directories?

hi,
I apologize if i was not clear.

source and destination dir paths :

/home/guest/source
/home/guest/source/Dest

But you have paths with other names in your input file?

/xyz/Test/testsrc.c /xyz/nmake/build.mak /abc/1.c

What are the full names of the source and destination directories?

I gave source & destination directory paths as follows:

/home/guest/source
/home/guest/source/Dest

under source i have

/xyz/Test/testsrc.c /xyz/nmake/build.mak /abc/1.c /abc/1.h ../proj_src/2.c ../proj_src/2.h /abc/svn/.svn  xyz/dump/dump.log

input file contains:

/xyz/Test/testsrc.c /xyz/nmake/build.mak /abc/1.c

now i want to copy above mentioned files (as per input file) to /home/guest/source/dest so result should be

/home/guest/source/dest/xyz/Test/testsrc.c 
/home/guest/source/dest/xyz/nmake/build.mak
/home/guest/source/dest/abc/1.c 

input file is located here: /home/guest/source

:b:

Try this:

Source="/home/guest/source/"
Dest="/home/guest/source/Dest/"

tr ' ' '\n' < ${Source}inputfile | while read file
do
  cp ${Source}${file} ${Dest}${file}
done

Hi Franklin,
Thanks a lot for your solutions.

it didn't work !!

#!/bin/bash
Source="/home/guest/source/"
Dest="/home/guest/source/Dest/"
tr ' ' '\n' < ${Source}input | while read file
do
  cp ${Source}${file} ${Dest}${file}
done

input

/xyz/Test/testsrc.c /xyz/nmake/build.mak /abc/1.c

got error as follows:

cp: cannot create regular file `/home/guest/source/Dest//xyz/Test/testsrc.c': No such file or directory
cp: cannot create regular file `/home/guest/source/Dest//xyz/nmake/build.mak': No such file or directory
cp: cannot create regular file `/home/guest/source/Dest//abc/1.c': No such file or directory

tried removing forward slash at the beginning of input file!
input

xyz/Test/testsrc.c xyz/nmake/build.mak abc/1.c

error:

cp: cannot create regular file `/home/guest/source/Dest/xyz/Test/testsrc.c': No such file or directory
cp: cannot create regular file `/home/guest/source/Dest/xyz/nmake/build.mak': No such file or directory
cp: cannot create regular file `/home/guest/source/Dest/abc/1.c': No such file or directory

I have those input files in Source directory and i also have necessary permissions to write in Dest as well.

any idea ?

Does it work if you copy the files manually?

hi,
manually means copy paste in nautilus or below one

$ cp /home/guest/source/xyz/Test/testsrc.c /home/guest/source/Dest/
$ ls /home/guest/source/Dest/
testsrc.c

both works .

i wanted to have output as
/home/guest/source/Dest/xyz/Test/testsrc.c

if i had to copy different files under different directories then i had to create all those dirs & subdirs manually .
i dont want to do that manually.

Do i need to check the presence of dir like xyz Test abc etc (obviously it wont be for the first time) and create those dirs and then run cp command?

Check this first with the echo command, if it's correct you can use the cp command:

Source="/home/guest/source"
Dest="/home/guest/source/Dest/"

tr ' ' '\n' < ${Source}inputfile | while read file
do
  echo cp ${Source}${file} ${Dest}
#  cp ${Source}${file} ${Dest}
done

hi,

echo command works as expected but not cp.
seems problem with creation of folder .
i created test-script file and tried to copy to Dest dir and it worked. However it didnt work for other files because there is no directory at Dest with the name xyz,Test,nmake !

$ /run.sh 
/home/guest/source/xyz/Test/testsrc.c /home/guest/source/Dest/xyz/Test/testsrc.c
cp: cannot create regular file `/home/guest/source/Dest/xyz/Test/testsrc.c': No such file or directory
/home/guest/source/xyz/nmake/build.mak /home/guest/source/Dest/xyz/nmake/build.mak
cp: cannot create regular file `/home/guest/source/Dest/xyz/nmake/build.mak': No such file or directory
/home/guest/source/abc/1.c /home/guest/source/Dest/abc/1.c
cp: cannot create regular file `/home/guest/source/Dest/abc/1.c': No such file or directory
/home/guest/source/test-script /home/guest/source/Dest/test-script

You can use mkdir with the -p option (man mkdir):

Source="/home/guest/source"
Dest="/home/guest/source/Dest/"

tr ' ' '\n' < ${Source}inputfile | while read file
do
  mkdir -p ${Dest}
  cp ${Source}${file} ${Dest}
done

Hi Franklin,
below one creates a dir "Dest" which is not required.

mkdir -p ${Dest}
Expected output was to paste a file from /home/guest/source/xyz/Test/testsrc.c to /home/guest/source/Dest/xyz/Test/
dont i need to create dir

mkdir /home/guest/source/Dest/xyz/
mkdir /home/guest/source/Dest/xyz/Test/
then testsrc.c should be copied to /home/guest/source/Dest/xyz/Test/

similarly for other two input lines
mkdir for
/home/guest/source/Dest/xyz/nmake
/home/guest/source/Dest/abc
source has many dirs and subdirs and copy as per input file (with directory structure) .

---------- Post updated at 09:25 AM ---------- Previous update was at 07:37 AM ----------

hi,
i modified the code something like this:

a=${Dest}${file} 
echo "a has:"$a 
# a has: /home/guest/source/Dest/xyz/Test/testsrc.c
b=`echo ${a%/*}`
echo "b value is:"$b
# b value is: /home/guest/source/Dest/xyz/Test/
# check b exists
if [ -d "${b}" ]; then
echo "does not exist ${b}"
c=`echo ${b%/*}`
if [ -d "${c}" ]; then
echo "c has :"$c
# c has : /home/guest/source/Dest/xyz/
mkdir -p $c
fi
mkdir -p $b
fi
cp ${Source}${file} ${Dest}${file}
done

However if dir (value of b) does not exist echo "does not exist ${b}" didnt pop up :frowning:

i know this is not so good but i want some checks for the presence of directory , create dir c and if dir does not exist reduce one level , check the presence,create dir b and so on.Finally run cp command

with above mentioned code i got expected output ( commented out if condition) but how do i achieve for N number of dirs & subdirs :b:

The statement should be:

if [ ! -d "${b}" ]; then