Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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?

Old 10-20-2011, 09:25 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
If the estscan_pep.fasta files are all at first level below the current directory, you can try something like this:
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, 09:42 PM   #3
Registered: Dec 2007
Location: Queensland, Australia
Posts: 123

Original Poster
Rep: Reputation: 15

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.



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 07:12 PM
Bash script rename files based on directory name cupofnestor Linux - General 7 03-31-2010 08:20 AM
How can I write a script that will go through the files in a directory and .... yazuka Programming 12 11-26-2008 10:14 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM > Forums > Linux Forums > Linux - Newbie

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

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