Skip to content
Advertisement

Linux bash script symlink with spaces

I am trying to make a script which will find all the folders and files within a given hard drive. then i want to make the symlinks to another folder. However the ln command fails if the folder or file contains space, can someone help me to modify the ln -sf $MOVIE_FILE_LINKS -t $MOVIES_LIB command to make the folders with spaces work? Thanks

#! /bin/bash
# Version: 2017.02.21

SRC="/mnt/hdd_1.5T
/mnt/hdd_4T"

MOVIES_TEXT="Movies.*"
MOVIES_LIB="/mnt/Library.Movies/"

rm -rf $MOVIES_LIB    #need more elegant solution
mkdir -p $MOVIES_LIB

clear

MOVIE_DIRS=$(find $SRC -maxdepth 1 -type d -name "$MOVIES_TEXT")
MOVIE_DIR_LINKS=$(find $MOVIE_DIRS -maxdepth 1 -type d | sort | awk '$0 !~ last "/" {print last} {last=$0} END {print last}')
MOVIE_FILE_LINKS=$(find $MOVIE_DIRS -maxdepth 1 -type f -name "*.*")

echo "$MOVIE_DIR_LINKS"
#ln -sf $MOVIE_LINKS $MOVIES_LIB;
echo "$MOVIE_FILE_LINKS"
#ln -sf $MOVIE_FILE_LINKS -t $MOVIES_LIB;

Advertisement

Answer

You can try to use find‘s -exec action:

find $MOVIE_DIRS -maxdepth 1 -type d -exec ln -sf -t $MOVIES_LIB {} +
find $MOVIE_DIRS -maxdepth 1 -type f -name "*.*" -exec ln -sf -t $MOVIES_LIB {} +
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement