Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-10-2012, 11:13 AM
|
#1
|
|
LQ Newbie
Registered: Feb 2006
Location: Brooklyn, NY
Distribution: Ubuntu 11.10
Posts: 19
Rep:
|
How do I rename files/folders recursively using a simple script?
I need to rename files (mp3 files) and subdirectories, removing the apostrophe from both. Currently I am using the rename command however it does not rename any of my subdirectories.
sudo rename "s/'*//g" *
I have thousands of files which need renaming with hundreds of subdirectories.
Can someone please show me a simple way to rename files/folders recursively in unix?
Thanks in advance!
|
|
|
|
07-10-2012, 11:27 AM
|
#2
|
|
Senior Member
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,888
Rep:
|
Perhaps find could help you?
man find
|
|
|
|
07-10-2012, 02:22 PM
|
#3
|
|
LQ Newbie
Registered: Feb 2006
Location: Brooklyn, NY
Distribution: Ubuntu 11.10
Posts: 19
Original Poster
Rep:
|
Thanks eantoranz.
I did the following and it appears to work:
sudo find . -name "*'*" | rename "s/'*//g"
Before I run this on my file system, can you think of anything that can go wrong with the command above?
|
|
|
|
07-10-2012, 02:26 PM
|
#4
|
|
Senior Member
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,062
|
Quote:
Originally Posted by ixcel87
Thanks eantoranz.
I did the following and it appears to work:
sudo find . -name "*'*" | rename "s/'*//g"
Before I run this on my file system, can you think of anything that can go wrong with the command above?
|
The potential of spaces not being handled correctly. Might be better to utilize exec in find as it will pass the whole file as an argument rather than leaving it up to bash.
Code:
find . -name "*'*" -exec rename "s/'*//g" {} \;
|
|
|
|
07-10-2012, 03:16 PM
|
#5
|
|
LQ Newbie
Registered: Feb 2006
Location: Brooklyn, NY
Distribution: Ubuntu 11.10
Posts: 19
Original Poster
Rep:
|
Thanks sag47.
Works like a charm, although I have to run it twice!
|
|
|
|
07-11-2012, 11:02 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589
|
You should probably add the -depth option to the command, so that it processes files before the directories that contain them.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:51 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|