How to copy selective list of files to a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy selective list of files to a directory?
# 1  
Old 03-07-2013
How to copy selective list of files to a directory?

Hi

I have 3 directories
indexes_with_ts
indexes_without_ts
process_indexes

in each directories it contains *.sql

how do I accomplish this:

for all the files found in indexes_without_ts, copy the corresponding file in indexes_with_ts to process_indexes.

i.e.

for indexes_without_ts/abc.sql

copy indexes_ts/abc.sql to process_indexes how do I achieve this?

code is in the current directory which contains all three directories.

my current code is
Code:
for file in  indexes_without_ts/*.sql
do
   cp -p "$file" process_indexes/"$file"
done

my current code gives me the following error.
Code:
cp: cannot create regular file `process_indexes/indexes_without_ts/uk_oe_term_march_cpg.sql': No such file or directory
cp: cannot create regular file `process_indexes/indexes_without_ts/uk_system_entity_code.sql': No such file or directory
cp: cannot create regular file `process_indexes/indexes_without_ts/uk_excel_network_networkno.sql': No such file or directory

thanks
# 2  
Old 03-07-2013
1) make sure you are in the directories that contains the directories
indexes_with_ts
indexes_without_ts
process_indexes

2) since "file" is the name of a unix command, as a best practice, i would suggest you to find another name for your variable

You can give a try to:

Code:
for f in  ./indexes_without_ts/*.sql
do
cp -p ./indexes_with_ts/${f##*/} ./process_indexes/${f##*/}
done


Last edited by ctsgnb; 03-07-2013 at 04:46 AM..
# 3  
Old 03-07-2013
Please try this:
Code:
$ for i in indexes_without_ts/*.sql; do [[ -f $i ]] && cp $i process_indexes/; done


Last edited by Franklin52; 03-07-2013 at 06:56 AM.. Reason: Please use code tags for data and code samples, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash selective copy folders and content to another location

I'm looking for a bash scrypt to copy some folders and some of the content to another location. I'm a teacher and very noobish with programming language anyway what I'm looking for , I have this director structure Main director "Students" with subfolders "john";"daisy";"work" etc .. and some of... (2 Replies)
Discussion started by: brickleul
2 Replies

2. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

3. Red Hat

Unable to copy files due to many files in directory

I have directory that has some billion file inside , i tried copy some files for specific date but it's always did not respond for long time and did not give any result.. i tried everything with find command and also with xargs.. even this command find . -mtime -2 -print | xargs ls -d did not... (2 Replies)
Discussion started by: before4
2 Replies

4. Shell Programming and Scripting

Copy list of files from a keyword list to another directory

Hello, I have a folder with a massive amount of files, and I want to copy out a specific subset of the files to a new directory. I would like to use a text file with the filenames listed, but can't get it to work. The thing I'm hung up on is that the folder names in the path can and do have... (5 Replies)
Discussion started by: twjolson
5 Replies

5. Shell Programming and Scripting

Copy files on a list to another directory

Hi. I have a list with file names like testfile1.wav testfile2.wav testfile3.wav and a folder that contains a large number of wav files (not only the ones on the list). I would like to copy the files whose names are on the list from the wav file directory to a new directory. I... (5 Replies)
Discussion started by: Bloomy
5 Replies

6. Shell Programming and Scripting

Copy selective lines from text file

Hello, I have a text file which I need to check for presence of certain tags, and then copy a subsequent portion of text into another file. The tag matching canbe done with Grep but I do not know how to copy selective lines from one file to another. Is it possible do that? I checked up some... (8 Replies)
Discussion started by: ajayram
8 Replies

7. UNIX for Dummies Questions & Answers

How can i copy a list of files with different names into others directory have the same name?

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (31 Replies)
Discussion started by: t17
31 Replies

8. Shell Programming and Scripting

how can i copy a list of files with different names into others directory have the same name

dear all. how can i copy a list of files with different names into others directory have the same name like i have 3 files 10_10 10_10_11 10_10_11_12 and i have 3 directories 10_10 10_10_11 10_10_11_12 how can i make a loop to cp this files into the directory have the same name like... (0 Replies)
Discussion started by: t17
0 Replies

9. Shell Programming and Scripting

find list of files from a list and copy to a directory

I will be very grateful if someone can help me with bash shell script that does the following: I have a list of filenames: A01_155716 A05_155780 A07_155812 A09_155844 A11_155876 that are kept in different sub directories within my current directory. I want to find these files and copy... (3 Replies)
Discussion started by: manishabh
3 Replies

10. Shell Programming and Scripting

Copy files from one directory to another

I need to copy about 13 Tb of data from one directory and subdirectories to the other (another mount point). If I run this as a cron, say between 10 pm and 7 am, not all of the files will be copied over. Is there a way of 'resuming' the copy the following evenings until all files are copied over? (0 Replies)
Discussion started by: hd2006
0 Replies
Login or Register to Ask a Question