LinuxQuestions.org
Review your favorite Linux distribution.
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 02-10-2010, 09:24 AM   #1
J2O1988
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Renaming files in subdirectories


Hi,

I'm currently trying to make a script to rename all the files with one provided file extension to a second provided file extension. I've achieved this by commanding "sh newext doc txt" with the following which works perfectly:

#!/bin/sh

for f in *$1;
do
mv "$f" "`basename "$f" $1`$2";
done;

However, I'd like to be able to modify what I've written so far, so that I can choose whether to convert file extensions in a subdirectory or not. For example, I could enter "sh newext -r doc txt" and the subdirectory's files would also be affected by my script, or enter "sh newext -n doc txt" which would only affect the directory I'm in.

I searched the forums and found a few things that I'm now looking in to, but anything else would be much appreciated. Thanks.
 
Old 02-10-2010, 09:52 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You could use "rename doc txt *.doc" to change all of the extensions in a directory.

For subdirectories, you could use the find command to return a list of subdirectories in a outer loop.

You can use find to locate files and execute the rename command:

This will do it for a single directory.
find <dir> -maxdepth 1 -type f -execdir rename doc txt '{}' \;

This will do it for subdirectories as well:
find <dir> -type f -execdir rename doc txt '{}' \;

Note: Some distro's have a perl like syntax for rename instead of the rename supplied by the linux-utils package.

Last edited by jschiwal; 02-10-2010 at 05:42 PM.
 
Old 02-10-2010, 05:39 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
I think you mean maxdepth instead of maxdir there http://linux.die.net/man/1/find
 
Old 02-10-2010, 05:43 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
chrism01: Yes, thanks. I corrected it in my post. I use that option daily, so I don't know why I typed dir instead of depth.
 
Old 02-13-2010, 01:39 AM   #5
sumeet inani
Member
 
Registered: Oct 2008
Posts: 908
Blog Entries: 26

Rep: Reputation: 49
J2O1988 has an interesting idea
i would like to illustrate for benefit of all
(1)It is said that debian distros have rename command available by default.I use ubuntu 8.04.For others get http://tips.webdesign10.com/files/rename.pl.txt & make it executable then move to /usr/bin folder.
(2)I thought about converting all file names to lower case as this makes filename auto-completion easier.(I don't remember the case of starting letter of file.Though alternative for bash is
add to ~/.inputrc "set completion-ignore-case On" without quotes)
I ran
Code:
find /data/sumeet/txt -type f -iname '*.*' -execdir rename -v  's/(.+)\.(.+)/\L$1\.$2\E/' '{}' ';'
Little Explanation
Code:
-execdir run command from subdirectory containing matched file
-v verbose output of rename (You can use -n to do test run without affecting change when in doubt)
\L lowercase till \E
\. represents period as simply . represents "any character" in perl expression(evident from .+ usage in parentheses)
s means substitute
$1 and $2 represents content of first & second parentheses respectively.
+ means repetition allowed 1 or more times
You can read more at
(1)http://www.anaesthetist.com/mnm/perl....htm#regex.htm
(2)http://www.troubleshooters.com/codec...rl/perlreg.htm
(3)http://tips.webdesign10.com/how-to-b...n-the-terminal
If this helps you than thank me by pressing thumbs up button.Thank You.

Last edited by sumeet inani; 02-13-2010 at 01:47 AM.
 
  


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
[SOLVED] Need to concatenate many files with the same name occurring in many subdirectories kmkocot Linux - Newbie 11 12-15-2009 07:21 PM
listing subdirectories files ramesh_manu Red Hat 2 02-18-2007 01:46 PM
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 09:18 PM
Unzipping all .gz files in all subdirectories darin3200 Linux - General 10 04-24-2004 09:58 PM
delete files / keep subdirectories? lhorstman Linux - Newbie 2 01-10-2003 03:01 PM

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

All times are GMT -5. The time now is 09:34 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