LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-27-2010, 09:47 PM   #1
lrall
LQ Newbie
 
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
Lightbulb Shell script, recursive rename/string replace?


Hey all- first post. This is probably a bit of a newb question, but I'm quite new to shell scripting and have given this quite a bit of thought before getting stuck

I'm looking to get a shell script to loop through a number of directories and subdirectories, looking for files that contain a particular substring, and renaming the file by replacing the search string with a different substring.

For example if you had a directory full of folders that contained digital photos (along with various other files which would need to remain unaffected), and the intent was to remove the "DSC_" prefix from several thousand files buried within.
You know, hypothetically speaking

I've whipped up a rather long-winded solution that works well for this purpose but chokes on directory names with spaces. I am reasonably sure there's a 2 or 3-liner that would accomplish this exact same task.

Thanks in advance for any help you can give.

________________________

function investigate
{
path=$1
for file in `ls $1`
#for file in *

do
pathfile="$path/$file"
if [ -d "$pathfile" ] #is dir
then
investigate "$pathfile"
else #not a dir
if #...file matches search criteria
then
#...
mv $pathfile $newname #rename it
else #skip it
fi
fi
done
}

investigate .
 
Old 07-27-2010, 10:12 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
find /path -type f -iname "DSC*jpg" | while read -r FILE
do
  newfile="${FILE/DSC_/}"
  mv "$FILE" "$newfile" 
done
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need assistance with shell script - replace a string with a start tag and end tag SupermanInNY Programming 18 01-02-2010 05:44 PM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:23 PM.

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