LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to write a script to rename files in directories to include directory name (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-write-a-script-to-rename-files-in-directories-to-include-directory-name-909164/)

kmkocot 10-20-2011 09:09 AM

Trying to write a script to rename files in directories to include directory name
 
Hi all,

I have a "main" directory containing many sub-directories that are all named like "FirstName_SecondName" (they are scientific names of animals with genus_species). In each of these directories there is a file called estscan_pep.fasta. I would like to copy each of the estscan_pep.fasta files to the "main" directory but replace the "estscan" part with the "FirstName_SecondName" part from the name of the folder in came from. I believe find and xargs are the tools for the job but I can't figure out the syntax. Could anyone give me some suggestions to get me started?

Thanks!
Kevin

colucix 10-20-2011 09:25 AM

If the estscan_pep.fasta files are all at first level below the current directory, you can try something like this:
Code:

while read file; do echo cp -p $file ${file%/*}_pep.fasta; done < <(find . -type f -name estscan_pep.fasta)
this will print out the commands to execute. If they satisfy your requirement, remove the echo statement and run again.

kmkocot 11-15-2011 09:42 PM

Sorry for the delay in replying. That's perfect. I had played with something like this but I had the syntax for "${file%/*}" all wrong.

Thanks!
Kevin


All times are GMT -5. The time now is 11:08 AM.