Copy files from input file with dir structure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy files from input file with dir structure
# 15  
Old 11-19-2010
hi,
manually means copy paste in nautilus or below one

Code:
$ 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?

Last edited by dragon.1431; 11-19-2010 at 04:09 AM.. Reason: added more info
# 16  
Old 11-19-2010
Check this first with the echo command, if it's correct you can use the cp command:
Code:
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

# 17  
Old 11-19-2010
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 !

Code:
$ /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

# 18  
Old 11-19-2010
You can use mkdir with the -p option (man mkdir):
Code:
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

# 19  
Old 11-19-2010
Lightbulb

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
Code:
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:
Code:
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 Smilie

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 Smilie

Last edited by dragon.1431; 11-19-2010 at 10:27 AM.. Reason: corrected last line
# 20  
Old 11-19-2010
Quote:
Originally Posted by dragon.1431
However if dir (value of b) does not exist echo "does not exist ${b}" didnt pop up Smilie
The statement should be:
Code:
if [ ! -d "${b}" ]; then

# 21  
Old 11-19-2010
Hi,
with that if condiition i can check the presence of folder .Is there any way to reduce the dir depth one by one level and create dir for those if it is not present till
/home/guest/source/Dest

For example:
Code:
/home/guest/source/Dest/prj/main_src/lib/a.c
/home/guest/source/Dest/prj/test-scripts/z.py

Above mentioned example has dir depths 3,2 after Dest.
dir depth can go till 7 .

Thanks in advance.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to new dir structure.

I am trying to figure out a way to script copying specific files from one dir structure to another. I have a dir structure like this: dira/author 1/book 1/file a.epub /book 2/file b.epub /author 2/book 1/file c.epub /author 3/book 1/file d.epub /book 2/file... (2 Replies)
Discussion started by: arcanas
2 Replies

2. Shell Programming and Scripting

files copy to dir

Hi, I have a directory which is having many files. I want to copy 10files at a time to another directory. For example. First 10 files to one directory, then next 10 files to another directory and so on. Please let me know if any work around there for it. Thanks (4 Replies)
Discussion started by: Anjan1
4 Replies

3. Shell Programming and Scripting

Copy files and subdirs from dir to a new dir

Hello Comunity I am trying to make a bash shell script that it copies files and subdirs(with files) to a new dir. I would like the dest_dir to contain only subdirectories with files not other subdirs inside. it called : cpflatdir src_dir dest_dir Pleaze help me! Thank you in... (2 Replies)
Discussion started by: BTKBaaMMM
2 Replies

4. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

5. Shell Programming and Scripting

Copy Files to Dir and Check If File Exists

Hi everyone. I am trying to write a bash script that will copy files from one directory to another but I need to be able to check the directory that I'm copying the files to and see if the file already exists. If it does I need to add a number at the end of the copied file. Thanks for your help. (3 Replies)
Discussion started by: snag49ers
3 Replies

6. Shell Programming and Scripting

Script to run a command on all txt files present in a dir structure

Hi, I have a directory structure like the one given below root\a\b1 root\a\b2 root\b\b1 root\b\b2 . . . root\j\b1 root\j\b2 Now, there are a txt files in each dir and subdir, there is a root.txt I have to write a script where in i have to run a command called "genrb <filename>"... (6 Replies)
Discussion started by: vikramsinghnegi
6 Replies

7. Shell Programming and Scripting

How to copy specified files from list of files from dir A to dir B

Hello, fjalkdsjfkldsajflkajdskl (3 Replies)
Discussion started by: pmeesara
3 Replies

8. Shell Programming and Scripting

copy files from one dir to another

Hi , I want to copy files from one dir to anothe dir and check if destination dir exists ,if not exist ,has to create but when executing the below schell script ,the destination directory not exist and exit the shell script. #!/bin/sh src_path=/home/owngdw/abc tgt_path=/home/owngdw/abc/xyz if... (6 Replies)
Discussion started by: mohan705
6 Replies

9. UNIX for Dummies Questions & Answers

copy files with directory structure

i have a text file as. /database/sp/NTR_Update_Imsi_List.sql /database/sp/NTR_Update_Imsi_Range_List.sql /database/sp/NTR_Vlr_Upload.sql /database/tables/StatsTables.sql /mib/ntr.mib /mib/ntr.v2.mib /scripts/operations/ntr/IMSITracer.ph /scripts/operations/ntr/IMSITracer.pl ... (3 Replies)
Discussion started by: adddy
3 Replies

10. UNIX for Dummies Questions & Answers

How to copy N files from one dir to another

Hi, I have a script that can only handLE limited number of input files. I need to be able to write a command (or a script) that: 1> copies N number of files from one directory (A) to another (B). 2> Files that are moved need to be renamed. 3> Files picked to be moved have... (1 Reply)
Discussion started by: GMMike
1 Replies
Login or Register to Ask a Question