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

for file in  indexes_without_ts/*.sql
do
   cp -p "$file" process_indexes/"$file"
done


my current code gives me the following error.

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

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:

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

Please try this:

$ for i in indexes_without_ts/*.sql; do [[ -f $i ]] && cp $i process_indexes/; done