LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 07-10-2012, 11:13 AM   #1
ixcel87
LQ Newbie
 
Registered: Feb 2006
Location: Brooklyn, NY
Distribution: Ubuntu 11.10
Posts: 19

Rep: Reputation: 0
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!
 
Old 07-10-2012, 11:27 AM   #2
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,888
Blog Entries: 1

Rep: Reputation: 77
Perhaps find could help you?

man find
 
Old 07-10-2012, 02:22 PM   #3
ixcel87
LQ Newbie
 
Registered: Feb 2006
Location: Brooklyn, NY
Distribution: Ubuntu 11.10
Posts: 19

Original Poster
Rep: Reputation: 0
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?
 
Old 07-10-2012, 02:26 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Philly, PA
Distribution: Kubuntu x64, RHEL, Fedora Core, FreeBSD, Windows x64
Posts: 1,062
Blog Entries: 30

Rep: Reputation: 221Reputation: 221Reputation: 221
Quote:
Originally Posted by ixcel87 View Post
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" {} \;
 
Old 07-10-2012, 03:16 PM   #5
ixcel87
LQ Newbie
 
Registered: Feb 2006
Location: Brooklyn, NY
Distribution: Ubuntu 11.10
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks sag47.

Works like a charm, although I have to run it twice!
 
Old 07-11-2012, 11:02 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589

Rep: Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832
You should probably add the -depth option to the command, so that it processes files before the directories that contain them.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] rename files in folders using perl script pablgonz Programming 15 12-18-2011 12:22 PM
script to rename files recursively rs232 Linux - General 3 05-08-2011 02:05 PM
Shell script to delete folders and files dynamically and recursively rjbaca Linux - General 1 06-21-2010 11:26 AM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 04:35 PM
Entering folders recursively to run script on files nested inside, ubuntu 7.10 aidansmoker Linux - Newbie 4 11-11-2007 11:39 PM


All times are GMT -5. The time now is 11:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration