LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-20-2011, 10:09 AM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Question 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
 
Old 10-20-2011, 10:25 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
1 members found this post helpful.
Old 11-15-2011, 10:42 PM   #3
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Smile

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to search for directories containing certain files then taring directory linuxuser111 Linux - General 1 03-16-2011 08:12 PM
Bash script rename files based on directory name cupofnestor Linux - General 7 03-31-2010 09:20 AM
How can I write a script that will go through the files in a directory and .... yazuka Programming 12 11-26-2008 11:14 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 01:51 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration