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="xstep" data-source="post: 1006826" data-attributes="member: 11647"><p>Here is my version...</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>#loop through each entry in the TV_DIR finding folders </p><p>#remove spaces because for loop uses them as name delimeters.</p><p>for dir in `find . -type d -name "Season*" | sed -e 's/ /~~/'`; do</p><p></p><p> #remove my special characters.</p><p> dir=`echo $dir | sed 's/~~/ /'`</p><p> #change single digit to two digits.</p><p> newname=`echo $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 folderA folderA seems to have special meaning in mv command so bypass those same name cases.</p><p> if [ "$newname" != "$dir" ]; then</p><p> mv "$dir" "$newname"</p><p> fi</p><p></p><p> done</p><p></p><p>echo "Job Done."</p><p>[/CODE]</p></blockquote><p></p>
[QUOTE="xstep, post: 1006826, member: 11647"] Here is my version... [CODE]#!/bin/bash # Script to pad a 0 to Season numbers TV_DIR=~/Desktop/TV cd "$TV_DIR" #loop through each entry in the TV_DIR finding folders #remove spaces because for loop uses them as name delimeters. for dir in `find . -type d -name "Season*" | sed -e 's/ /~~/'`; do #remove my special characters. dir=`echo $dir | sed 's/~~/ /'` #change single digit to two digits. newname=`echo $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 folderA folderA seems to have special meaning in mv command so bypass those same name cases. if [ "$newname" != "$dir" ]; then mv "$dir" "$newname" fi 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