LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-02-2010, 04:28 PM   #1
Drigo
Member
 
Registered: May 2009
Posts: 111

Rep: Reputation: 17
Angry Using ls and renaming...... HELP


ok...I wanted to rename files in subdirectories using a simple bash script. Here is what I have so far:

for FILE in $(find ./ -name PDI_*); #any other commands helpful for this?
?? Something elsE? #do mv $FILE "${FILE//PDI_11420_}" ;

done


Here is a simple example of what I want to do. Basically here add NewText_ to all the files in the subdirectories with extension *.nii:


Input:

Parent_directory
Subdirectory_A
File1.nii
File2.nii
File3.nii
File4.nii
other.xxx
Subdirectory_B
File1.nii
File2.nii
File3.nii
File4.nii
othera.yyy
Subdirectory_C
File1.nii
File2.nii
File3.nii
File4.nii
.
.
.


What I want (output):


Parent_directory
Subdirectory_A
NewText_File1.nii
NewText_File2.nii
NewText_File3.nii
NewText_File4.nii
Subdirectory_B
NewText_File1.nii
NewText_File2.nii
NewText_File3.nii
NewText_File4.nii
Subdirectory_C
NewText_File1.nii
NewText_File2.nii
NewText_File3.nii
NewText_File4.nii
 
Old 03-02-2010, 04:39 PM   #2
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
What's about this?
Code:
find -name "*.nii" | while read name; do mv $name `dirname "$name"`/NewText_`basename $name`; done
Using $(...) if you expect large output from ... is not a good idea in general, because it will make your command very long and eventually exceed the max. command line length.
 
1 members found this post helpful.
Old 03-03-2010, 09:34 AM   #3
Drigo
Member
 
Registered: May 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Thank you...it did the trick@!
I am assuming 'dirname and 'basename are built-in strings right? I would like to understan a little bit the code so I can play around with it in different files that I have.
 
Old 03-03-2010, 04:52 PM   #4
irmin
Member
 
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
dirname and basename are coreutilitities:
see dirname(1) and basename(1) for details.

Here's a summary:

dirname returns the directory of its argument:
dirname /bin/ls => /bin
dirname ../../abc/def => ../../abc

basename the actual filename with the directory stripped off
basename /bin/ls => ls
basename ../../abc/def => def

Now to the code:
find -name "*.nii" will write all files matching the pattern to stdout
this output is read in the while loop with read line by line, where each line is stored in name.

For each name mv will be called with original filename and the modified filename.

I hope this helps.
 
  


Reply



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
Renaming files mashcaster Linux - General 3 05-08-2009 03:14 PM
Renaming files leupi Linux - General 5 09-16-2008 03:22 AM
Renaming Zmyrgel Linux - General 1 08-06-2006 07:59 AM
Renaming Folders Tr@ce Linux - Newbie 1 02-19-2005 02:08 PM
Need help with renaming jpgs joe_stevensen Programming 2 06-26-2004 04:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:15 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
Open Source Consulting | Domain Registration