Quote:
|
Remember that the script will run on all kinds of folders, not just the two examples I have provided. So the script has to first check what situation it is and the run the appropriate actions...
|
If thats the case you'll need to set up some type of config file which you can edit to add/remove paths and filenames in. I was bored so I went ahead and wrote something that you could use as a starting point and modify as you need.
The script
symlink reads a config file
symlink.conf which contains 5 entries:
# Copy this to symlink.conf and uncomment the examples or create your own.
# situation = tells program wether to rename the extension when linking.
# FROM = Directory where files are stored.
# TO = Directory where simlinks will be made
# ORIGINAL = The part of the original filename to be removed
# NEW = This will replace the ORIGINAL string in new symlink filename
# situation FROM TO ORIGINAL NEW
# Example 1
# 1 /mnt/e/dir2/show1 /mnt/tv/show/Lost lost.s02e01.hr.hdtv.xvid.nbs Lost
# Example 2
# 2 /mnt/e/dir1/show1 /mnt/tv/show/LasVegas ctu\-lv301\.r?? Las\.Vegas\.part
# Example 3
# 1 /mnt/e/dir1/show2 /mnt/tv/show/Simpsons name.of.show-S03E01 Simpsons
# Example 4
# 2 /mnt/e/dir2/show2 /mnt/tv/show/AnotherWorld S02E01\.r?? part
I have some dummy files setup to demonstrate:
mnt/e/dir1/show1:
ctu-lv301.r00
ctu-lv301.r01
ctu-lv301.r02
ctu-lv301.r03
ctu-lv301.r04
ctu-lv301.r05
ctu-lv301.r06
ctu-lv301.r07
ctu-lv301.r08
ctu-lv301.r09
ctu-lv301.r11
ctu-lv301.r12
ctu-lv301.rar
mnt/e/dir1/show2:
name.of.show-S03E01.part00.rar
name.of.show-S03E01.part01.rar
name.of.show-S03E01.part02.rar
name.of.show-S03E01.part03.rar
name.of.show-S03E01.part04.rar
name.of.show-S03E01.part05.rar
name.of.show-S03E01.part06.rar
name.of.show-S03E01.part07.rar
name.of.show-S03E01.part08.rar
name.of.show-S03E01.part09.rar
name.of.show-S03E01.part11.rar
name.of.show-S03E01.part12.rar
mnt/e/dir2/show1:
lost.s02e01.hr.hdtv.xvid.nbs.part00.rar
lost.s02e01.hr.hdtv.xvid.nbs.part01.rar
lost.s02e01.hr.hdtv.xvid.nbs.part02.rar
lost.s02e01.hr.hdtv.xvid.nbs.part03.rar
lost.s02e01.hr.hdtv.xvid.nbs.part04.rar
lost.s02e01.hr.hdtv.xvid.nbs.part05.rar
lost.s02e01.hr.hdtv.xvid.nbs.part06.rar
lost.s02e01.hr.hdtv.xvid.nbs.part07.rar
lost.s02e01.hr.hdtv.xvid.nbs.part08.rar
lost.s02e01.hr.hdtv.xvid.nbs.part09.rar
lost.s02e01.hr.hdtv.xvid.nbs.part11.rar
lost.s02e01.hr.hdtv.xvid.nbs.part12.rar
mnt/e/dir2/show2:
Another.World.S02E01.r00
Another.World.S02E01.r01
Another.World.S02E01.r02
Another.World.S02E01.r03
Another.World.S02E01.r04
Another.World.S02E01.r05
Another.World.S02E01.r06
Another.World.S02E01.r07
Another.World.S02E01.r08
Another.World.S02E01.r09
Another.World.S02E01.r11
Another.World.S02E01.r12
Another.World.S02E01.rar
So we have both types of situations here. In the 2 #1-Situations you have partXX.rar files, so all we do is rename the symlinks. We also have 2 #2-Situations where we have .rXX + one oddball .rar files, in this situation we have to rename the file extension after we increment the sequence by 1 so we have room for the oddball file with the .rar extension.
And here are partitial directory listings after running the script:
mnt/tv/show/AnotherWorld:
total 14
lrwxrwxrwx 1 admin None 41 Sep 25 07:46 Another.World.part00.rar -> mnt/e/dir2/
show2/Another.World.S02E01.rar
lrwxrwxrwx 1 admin None 41 Sep 25 07:46 Another.World.part01.rar -> mnt/e/dir2/
show2/Another.World.S02E01.r00
...
lrwxrwxrwx 1 admin None 41 Sep 25 07:46 Another.World.part13.rar -> mnt/e/dir2/
show2/Another.World.S02E01.r12
mnt/tv/show/LasVegas:
total 14
lrwxrwxrwx 1 admin None 30 Sep 25 07:46 Las.Vegas.part00.rar -> mnt/e/dir1/show
1/ctu-lv301.rar
lrwxrwxrwx 1 admin None 30 Sep 25 07:46 Las.Vegas.part01.rar -> mnt/e/dir1/show
1/ctu-lv301.r00
...
lrwxrwxrwx 1 admin None 30 Sep 25 07:46 Las.Vegas.part13.rar -> mnt/e/dir1/show
1/ctu-lv301.r12
mnt/tv/show/Lost:
total 13
lrwxrwxrwx 1 admin None 56 Sep 25 07:46 Lost.part00.rar -> mnt/e/dir2/show1/los
t.s02e01.hr.hdtv.xvid.nbs.part00.rar
lrwxrwxrwx 1 admin None 56 Sep 25 07:46 Lost.part01.rar -> mnt/e/dir2/show1/los
t.s02e01.hr.hdtv.xvid.nbs.part01.rar
...
lrwxrwxrwx 1 admin None 56 Sep 25 07:46 Lost.part12.rar -> mnt/e/dir2/show1/los
t.s02e01.hr.hdtv.xvid.nbs.part12.rar
mnt/tv/show/Simpsons:
total 13
lrwxrwxrwx 1 admin None 47 Sep 25 07:46 Simpsons.part00.rar -> mnt/e/dir1/show2
/name.of.show-S03E01.part00.rar
lrwxrwxrwx 1 admin None 47 Sep 25 07:46 Simpsons.part01.rar -> mnt/e/dir1/show2
/name.of.show-S03E01.part01.rar
...
lrwxrwxrwx 1 admin None 47 Sep 25 07:46 Simpsons.part12.rar -> mnt/e/dir1/show2
/name.of.show-S03E01.part12.rar
KNOWN BUGS:
As you can see the script works:
It will give an error if you designate a FROM directory as situation2 and it has more than 1 .rar file. If there are more than one .rar in a situation2 directory files it will only find and link the one which matches the other files in the directory.
And here is the script, and the config file:
symlink
Code:
#!/bin/bash
#symlink
CONFIG=symlink.conf
situation1 () {
[ ! -d $FROM ] && echo "Dir $FROM doesn\'t exist, exiting..." && exit
[ ! -d $TO ] && echo "Making directory $TO" && mkdir -p $TO
for i in $FROM/*\.rar
do
BASE=`basename "$i"`
# For troubleshoot uncomment next line
# echo "linking ln -s "$i" $TO/${BASE/$ORIGINAL/$NEW}"
# For troubleshoot comment next line
ln -s "$i" $TO/${BASE/$ORIGINAL/$NEW}
done
}
situation2 () {
[ ! -d $FROM ] && echo "Dir $FROM doesn\'t exist, exiting..." && exit
[ ! -d $TO ] && echo "Making directory $TO" && mkdir -p $TO
num=0
for i in $FROM/*\.r[0-9][0-9]
do
BASE=`basename "$i"`
num=$(($num+1))
NUM=$(printf "%0.2d" $num)
# For troubleshoot uncomment next line
# echo "linking ln -s $i $TO/${BASE/$ORIGINAL/$NEW$NUM.rar}"
# For troubleshoot comment next line
ln -s $i $TO/${BASE/$ORIGINAL/$NEW$NUM.rar}
done
if [ -f $FROM/*.rar ]
then
BASE=`basename "$i"`
NEW=$NEW"00"
# For troubleshoot uncomment next line
# echo "linking ln -s $FROM/*.rar $TO/${BASE/$ORIGINAL/$NEW.rar}"
# For troubleshoot comment next line
ln -s $FROM/*.rar $TO/${BASE/$ORIGINAL/$NEW.rar}
fi
FILE=${BASE/\.r[0-9][0-9]/\.rar}
if [ -f $FROM/$FILE ]
then
NEW=$NEW"00"
# For troubleshoot uncomment next line
# echo "linking ln -s $FROM/$FILE $TO/${FILE/$ORIGINAL/$NEW.rar}"
# For troubleshoot comment next line
ln -s $FROM/$FILE $TO/${FILE/$ORIGINAL/$NEW.rar}
fi
}
# This part reads the config file and assigns the
# variables used by the rest of the script.
# NOTE: arrays are 0 based therefore array[0]
# is the situation flag.
grep [^#] $CONFIG|while read #Dont read commented lines.
do
array=( $( echo "$REPLY" ) )
situation="${array[0]}"
FROM="${array[1]}"
TO="${array[2]}"
ORIGINAL="${array[3]}"
NEW="${array[4]}"
case $situation in
1 )
situation1
;;
2 )
situation2
;;
esac
done
situation.conf
Code:
# Symlink config file.
# situation = tells program wether to rename the extension when linking.
# FROM = Directory where files are stored.
# TO = Directory where simlinks will be made
# ORIGINAL = The part of the original filename to be removed
# NEW = This will replace the ORIGINAL string in new symlink filename
# situation FROM TO ORIGINAL NEW
# Examples
# 2 mnt/e/dir1/show1 mnt/tv/show/LasVegas ctu\-lv301\.r?? Las\.Vegas\.part
# 1 mnt/e/dir1/show2 mnt/tv/show/Simpsons name.of.show-S03E01 Simpsons
# 1 mnt/e/dir2/show1 mnt/tv/show/Lost lost.s02e01.hr.hdtv.xvid.nbs Lost
# 2 mnt/e/dir2/show2 mnt/tv/show/AnotherWorld S02E01\.r?? part
<edit> The comments about troubleshooting are for trying to get your ORIGINAL and NEW strings set in the config file. Just comment the line with the ln command and uncomment the echo command and you can run the script untill you figure out how to make it work the way you want.
<edit> Version .001 - Fixed KNOWN BUG
RED = Remove
GREEN = Add