I am working on a script to run on my Synology DS 1019+ that takes all the subfolders within a certain directory, creates the subfolders in another directory, then creates hard links of all the .mkv files within the subfolders in the newly created subfolders in the other location. However, if the subfolder already exists in the second location, then I want it to just create the hard link.
Due to the nature of my files, the folder structure and files within said folders will be different for each instance this script will be used for. Is there a way to make this script loop so that each loop takes the next folder within Location A and performs the mkdir of each subfolder and hard link task (to the files within said subfolder)?
Here is what I have currently:
#! /bin/bash
echo "Enter Movie Collection:"
read MOVIE_DIR
echo "Enter Bonus Feature Disc: "
read BONUS_DIR
cd "/volume1/Plex/Movies/$MOVIE_DIR/"
for dir in */.; do
if [[ ! -e "$dir"/*/ ]]; then
mkdir "$dir"/*/
fi
if [[ ! -d "$dir"/*/ ]]; then
ln /volume1/Plex/"Bonus Feature Discs"/$BONUS_DIR/*/*.mkv -t ./"$dir"/*/.
fi
done
I am not skilled at all when it comes to loops, especially nested loops, so I am not sure where to begin to troubleshoot this. Currently, instead of the folders in location A being replicated (if not already existing) in Location B, and the files within the folders in Location A being hardlinked, I am getting an empty folder with a name of "_2X68P~X" within the Location B's main directories (for example, in my test, Instead of "TEST 1" getting a "featurette" folder with a hardlinked TEST.mkv within it, I just get that garbage data folder within "TEST 1" that is empty.
I have attempted to use basename as well as dirname, but I have yet to find a way to have it loop so that it cycles through each folder within a directory. I have also attempted to use cd /the/directory/path/ "${PWD##*/}"
EDIT: Over the course of the several hours after posting this, I did figure out a solution. The code I had above simply was not logically sound, thanks to too many areas that were not specific, which meant nothing would happen. I ended up having to restart from the ground up. Here is the code that I ended up with, and this code does do the job I need it to do. It might not be the most elegant method for doing this, but it does work well enough based on the tests I've ran.
#! /bin/bash
#Ask the user to input the directories of both the bonus disc's files and where the movies are located that the bonus disc's contents will be needed.
echo "Enter the name of the Movie Collection and press [ENTER]: "
read MOVIE_DIR
echo "Enter the name of the Bonus Feature Disc and press [ENTER]: "
read BONUS_DIR
#This goes to the location of the bonus disc specified by the end user. I believe this part is necessary for creating the text document below, but it might not be.
cd "/volume1/Plex/Bonus Feature Discs/$BONUS_DIR/" || return
#This creates a text document that has each directory within the specified Bonus Disc directory as a separate line
ls -d -- */ >> /volume1/Plex/"Bonus Feature Discs"/output.txt
echo ls -d -- */
#This goes to the movie directory specified by the end user. This cd is definitely required
cd "/volume1/Plex/Movies/$MOVIE_DIR/" || return
#the for loop loops through every movie that resides in the movie collection folder
for dir in *; do
#this while loop reads the text document and will use each line from the document as a variable.
while IFS=' ' read -r line; do
name="$line"
#A directory with the name of the line of the text document will be created in each movies directory only if it doesn't already exist
mkdir -p "$dir/$name"
#this will create the hard links to every single video that resides in the folders within the bonus features disc into the corresponding folders for each movie
if [[ ! -e "/volume1/Plex/Movies/$MOVIE_DIR/$name" ]]; then
ln "/volume1/Plex/Bonus Feature Discs/$BONUS_DIR/$name"*.mkv -t ./"$dir/$name"
fi
done < "/volume1/Plex/Bonus Feature Discs/output.txt"
done
echo "Linking Completed"
echo $dir
#finally, once all the work is complete, the script will delete the text document that is no longer needed
rm "/volume1/Plex/Bonus Feature Discs/output.txt"