LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Coying corresponding files (https://www.linuxquestions.org/questions/linux-newbie-8/coying-corresponding-files-707125/)

yanchina12 02-24-2009 08:23 AM

Coying corresponding files
 
I have a folder structure where withing a parent folder called D there are 100s folders each with a name like u0001, u0002 and so on. I have two other folders names qb and kc which are outside D. Within the qb and kc folders there are corresponding files like u0001.qb and u0001.kc and so on. I'm looking for some scipts that can copy the .qb and .kc files to the corresponding folders in D. So the u0001.qb and u0001.kc files will be copied from qb and kc folders to the u0001 folders.Is there anyway that the script runs everyday automatically to update the folders? If one could help me would be obliged. Just I've been trying to help a charity.

Regarrds,
Yan

Tinkster 02-24-2009 02:08 PM

Pseudo code, since I don't know the exact structure of your
directory layout ...
Code:

for i in /path/to/kc/*kc
do
  targetdir=$( basename ${i} .kc )
  cp $i /path/to/${targetdir}/.
done
for i in /path/to/qb/*qb
do
  targetdir=$( basename ${i} .qb )
  cp $i /path/to/${targetdir}/.
done


yanchina12 03-05-2009 05:36 PM

Dear Tinkster ,

Very kind of you for your code . I really appreciate this.Very kind of you.

The directory structure is as follows. If you please tell me where to put the path would be great!

There is a folder called D and within D there are folders with U0001,U00002......( there are 100s of folders)
There is another two folders in the same level as D called KC(contain u0001.kc, u00002 and so on) and QB (contain u0001.qb, U0002.qb and so on). So the folder D, QB and KC all are on the same level
I would like to run the script from the parent folder where is D, QB and KC exists.

In your code, please let me know where to add the path.
I really appreciate your help and would be obliged.
I'm a medic and hence I appreciate help from people like you.

T74marcell 03-05-2009 07:21 PM

The '/path/to' parts need to be replaced with your own full pathnames. Using full pathnames has the advantage that the script can run from any location (not just the parent folder), and that it also works correctly when launched by a cron job.

----------
T74marcell

Arch Linux

Tinkster 03-05-2009 09:40 PM

Quote:

Originally Posted by yanchina12 (Post 3466288)
Dear Tinkster ,

Very kind of you for your code . I really appreciate this.Very kind of you.

The directory structure is as follows. If you please tell me where to put the path would be great!

There is a folder called D and within D there are folders with U0001,U00002......( there are 100s of folders)
There is another two folders in the same level as D called KC(contain u0001.kc, u00002 and so on) and QB (contain u0001.qb, U0002.qb and so on). So the folder D, QB and KC all are on the same level
I would like to run the script from the parent folder where is D, QB and KC exists.

In your code, please let me know where to add the path.
I really appreciate your help and would be obliged.
I'm a medic and hence I appreciate help from people like you.


Try this (make a back-up first ;})
Code:

#!/bin/bash
for i in KC/*kc
do
  temp=$(echo $( basename ${i} .kc ) |  tr '[a-z]' '[A-Z]' )
  target="D/${temp}/."
  cp $i ${target}
done
for i in QB/*qb
do
  temp=$(echo $( basename ${i} .qb ) |  tr '[a-z]' '[A-Z]' )
  target="D/${temp}/."
  cp $i ${target}
done


yanchina12 03-06-2009 06:19 AM

Dear Tinkster,

Many thanks for the code. It gives an error `temp=$' unexpected ? Your help is appreciated. Regards, Yan

Tinkster 03-06-2009 05:59 PM

Try to copy and paste code; it *should* just work - it
works here. If you can't copy and paste for some reason,
make sure there's no space after the $



Cheers,
Tink


All times are GMT -5. The time now is 05:05 PM.