Reputation: 35
So I have this idea of moving folders to different places depending on what the name of the source folder is.
I have came up with this little script, which does kind of work. It mismatches some names, and also it sometimes just straight up doesn't work.
script:
#!/bin/bash
for result in $(ls -d /path/to/folders/*/);
do
Size=${#result}
StripFrom=$(expr index "$result" 'S\b[0-9]\b')
Strip=4
Stripped=$(($StripFrom-$Strip))
EndStrip=$(($Size-$Stripped))
EndStrip=-$EndStrip
Serie=${result:23:$EndStrip}
mv $result /path/to/TV/$Serie/
done
What I'm trying to do:
I don't know if I'm going at this the wrong way all together.
23 is the number of characters in the path /path/to/folders/
, by the way.
Upvotes: 1
Views: 92
Reputation: 1053
Maybe to make things easier:
find /path/to/folders -name \*S[0-9]\* | while read $p
do
result=$(basename "$p")
SEASON=$(expr "$result" : '[[:print:]]\+\(S[[:digit:]]*\)')
SHOW=$(expr "$result" : '\([[:print:]]\+\)S[[:digit:]]*') # with the trailing "."
echo "show: $SHOW / season: $SEASON"
done
Upvotes: 0
Reputation: 52112
You can extract the series name with a regular expression:
regex='/([^/]*).S[[:digit:]]{2}[^/]*/$'
for dir in /path/to/folders/*/; do
if [[ $dir =~ $regex ]]; then
mv "$dir" /path/to/TV/"${BASH_REMATCH[1]}"
fi
done
The expression looks for S
followed by two digits between the last two /
of the path and captures everything between the second last /
and the S
, minus the character before the S
.
For an example content of /path/to/folders/
of
folders
├── show1.S01E14.blahblah
├── show2.S11E01.text
└── unrelateddir
the commands issued are
mv /path/to/folders/show1.S01E14.blahblah/ /path/to/TV/show1
mv /path/to/folders/show2.S11E01.text/ /path/to/TV/show2
Remarks for your script:
ls
as in $(ls -d /path/to/folders/*/)
(see here why that isn't a good idea in general), you can use the glob directly.S\b[0-9]\b
would fail to match and S
followed by two digits, as there is no word boundary between the digits.$
signs: Stripped=$(($StripFrom-$Strip))
can be written Stripped=$(( StripFrom - Strip ))
.Upvotes: 1