Reputation: 23
I'm currently a bit stuck, trying to adjust filenames within a folder tree if they have one or both of the following:
Remove 'S00' - 'S99' -that part is to be cut out of the filename.
s/[sS](0[1-9]|[1-9][0-9])/
could do that I think
Any free standing double digits get an 'E' in front of them. Find /\b([0-9]{2})\b/
and substitute E$1
somehow
Example: any of these
[lorem ipsum] dolor sit amet 07 [consetetur][1080p].mkv
[lorem ipsum] dolor sit amet S04 07 [consetetur][1080p].mkv
[lorem ipsum] dolor sit amet S04 E07 [consetetur][1080p].mkv
are changed to
within their respective subfolders.
Ultimate goal is to automate the process of renaming files as they appear (that latter bit I have to figure out once I'm done with this) so the kodi scraper can recognize episodes
This is how far I got, as you can see, its still a mess as I've found this rename script here a while ago, but I don't know how to properly apply the regex, I just slapped it in
#!/bin/bash
start_dir="//srv/MEDIA2/"
find "$start_dir" -name '*.*' -type f \
|sort \
|while read name; do
((i++))
mv -i "$name" \
#remove season part
"$(printf 's/[sS](0[1-9]|[1-9][0-9])/' "$(dirname "$name")" $((i)) "$(basename "$name")")"
#prepend E to free standing double digits
"$(printf 's/\b([0-9]{2})\b/' "$(dirname "$name")" $((i)) "$(basename "$name")")"
done
Currently it returns
//home/user/./rnscript.sh: line 14: s/[sS](0[1-9]|[1-9][0-9])/: No such file or directory
//home/user/./rnscript.sh: line 16: s([0-9]{2}/: No such file or directory
If anyone with knowhow in that area could help me out to get this running, it would be greatly appreciated.
Upvotes: 0
Views: 76
Reputation: 22042
Would you please try the following:
#!/bin/bash
shopt -s globstar
start_dir="//srv/MEDIA2/" # double // may be meaningless
for name in "$start_dir"/**/*.*; do # search the directory recursively
[[ -f $name ]] || continue # skip if "$name" is not a file
newname=$(sed -E 's/[sS](0[1-9]|[1-9][0-9]) +//; s/\b([0-9]{2})\b/E\1/' <<< "$name")
if [[ "$name" != "$newname" ]]; then # skip if the filenames are the same
echo mv -- "$name" "$newname"
fi
done
Drop echo
if the output looks good.
We do not have to sort the files as the variable $i
is not used.
[EDIT]
If you want to change S04E02
to E02
as well, please replace the newname=
line above to:
newname=$(sed -E 's/[sS](0[1-9]|[1-9][0-9]) *//; s/\b([0-9]{2})\b/E\1/' <<< "$name")
The difference is just change +
to *
, meaning zero or more whitespaces.
Upvotes: 1