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: 1005503" data-attributes="member: 38595"><p>I'm a complete newbie to shell/bash scripting (only been going a couple of days) and am trying to get to grips with it.</p><p></p><p>I'm trying to create a script that loops through a folder finding any sub folders that are called Season 1-9 and appends a 0 to the beginning of the number.</p><p></p><p>E.g Season 1 -> Season 01</p><p>Season 2 -> Season 02</p><p>Season 11 -> Season 11</p><p></p><p>I think this code is right for looping through find only folders and leaving files alone. Just can't seem to work out how to use sed and regular expressions to replace the text in the file names.</p><p></p><p>Here's my code so far and would love some pointers.</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> #here I want to search all folders in this format Season 1-9 and change to Season 01 etc</p><p> echo "$dir"</p><p></p><p> </p><p> done</p><p></p><p>echo "Job Done."[/CODE]</p></blockquote><p></p>
[QUOTE="gilc, post: 1005503, member: 38595"] I'm a complete newbie to shell/bash scripting (only been going a couple of days) and am trying to get to grips with it. I'm trying to create a script that loops through a folder finding any sub folders that are called Season 1-9 and appends a 0 to the beginning of the number. E.g Season 1 -> Season 01 Season 2 -> Season 02 Season 11 -> Season 11 I think this code is right for looping through find only folders and leaving files alone. Just can't seem to work out how to use sed and regular expressions to replace the text in the file names. Here's my code so far and would love some pointers. [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 #here I want to search all folders in this format Season 1-9 and change to Season 01 etc echo "$dir" 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