Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Newbie help with bash script to search and replace a filename
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="gilc" data-source="post: 1006496" data-attributes="member: 38595"><p>Thanks for that xstep.</p><p></p><p>It seems to be half working. It's certainly replacing the text as I want and even echoing it back to the screen but I've tried to apply the code to a variable and then use this with a mv command to change the the filename. E.g.</p><p></p><p>[CODE]</p><p>mv $dir $newfilename</p><p>[/CODE]</p><p></p><p>[CODE]usage: mv [-f | -i | -n] [-v] source target</p><p> mv [-f | -i | -n] [-v] source ... directory</p><p>Job Done.</p><p>[/CODE]</p><p></p><p>But it doesn't seem to be working and instead only outputting an error on how to use the mv command. When I try to echo the $newfilename variable this just shows up as empty. </p><p></p><p>Here's my script now and would be interesting to know what the sed stuff does that you suggested as I don't fully understand the line of code you've written.</p><p></p><p>[CODE]#!/bin/bash</p><p># Script to pad a 0 to Season numbers</p><p></p><p>TV_DIR=~/Desktop/TV</p><p></p><p>cd "$TV_DIR"</p><p></p><p>for dir in "`find . -type d `"; do #loop through each entry in the TV_DIR finding folders </p><p> </p><p> newname=$("$dir" | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/')</p><p> echo "This is the new folder name $NEWNAME"</p><p> #here I want to search all folders in this format Season 1-9 and change to Season 01 etc</p><p> </p><p> mv $dir $newname </p><p> </p><p> done</p><p></p><p>echo "Job Done."[/CODE]</p></blockquote><p></p>
[QUOTE="gilc, post: 1006496, member: 38595"] Thanks for that xstep. It seems to be half working. It's certainly replacing the text as I want and even echoing it back to the screen but I've tried to apply the code to a variable and then use this with a mv command to change the the filename. E.g. [CODE] mv $dir $newfilename [/CODE] [CODE]usage: mv [-f | -i | -n] [-v] source target mv [-f | -i | -n] [-v] source ... directory Job Done. [/CODE] But it doesn't seem to be working and instead only outputting an error on how to use the mv command. When I try to echo the $newfilename variable this just shows up as empty. Here's my script now and would be interesting to know what the sed stuff does that you suggested as I don't fully understand the line of code you've written. [CODE]#!/bin/bash # Script to pad a 0 to Season numbers TV_DIR=~/Desktop/TV cd "$TV_DIR" for dir in "`find . -type d `"; do #loop through each entry in the TV_DIR finding folders newname=$("$dir" | sed -e 's/ [0-9]$/zz&/' -e 's/zz / 0/') echo "This is the new folder name $NEWNAME" #here I want to search all folders in this format Season 1-9 and change to Season 01 etc mv $dir $newname done echo "Job Done."[/CODE] [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Newbie help with bash script to search and replace a filename
Top