Batch Create Symlinks, Files with Spaces

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
redbiscuit

Batch Create Symlinks, Files with Spaces

Post by redbiscuit »

Greetings,

I am trying to create symlinks for multiple directories/files some of which have spaces in the name. The purpose is to keep a clean library, while allowing rtorrent to continue seeding. Thus, I have a watch folder and my library folder and I want to create symlinks in the library folder from the completed files in the watch folder. Here is the script I am using now:

Code: Select all

findcommand=”find /mnt/Media/Music/.watch –maxdepth 1  –not –iname “*.torrent””
for item in ‘$findcommand’;
do
ln –s $item /mnt/Media/Music/;
done
This works great for files without spaces in the name; however, for files with spaces in the name, it creates a link for each word.
An example:
.../Music/.watch has folders:
/Rush, /Credence Clearwater Revival, and /Jim.Croce

Running the scripts yields:
.../Music/Link to Rush, /Link to Credence, /Link to Clearwater, /Link to Revival, and /Link to Jim.Croce
Obviously the /Credence, /Clearwater, and /Revival links don't work because those directories don't exist.

So, I ask: what am I doing wrong/how can I fix this?

As a follow up, is there a way I could automate this, either by scheduling the script to run on a timer/at a set time, or by having it check for a state change in the folder and adding a symlink each time a torrent is completed (i.e. a new folder/file is added to the .watch directory)?

Thanks in advance.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Batch Create Symlinks, Files with Spaces

Post by Pilosopong Tasyo »

redbiscuit wrote:what am I doing wrong/how can I fix this?
You will have to change a few things. First, redirect the output of the find command to a file instead of assigning it to a variable. Redirecting to a file ensures each result gets written in its own line.

Here's an example for the ls command on my end:

Code: Select all

administrator@dg31pr ~/Public $ ls ~/Dropbox
Archives                                  Mga Awitin    Public
CTP-2011                                  My Templates  rx-radio.sh
Deployment Guide 2012 - Ubuntu 12.04 LTS  Notices       Work
fix the dock.desktop                      Photos
Assigning the listing to a variable results in:

Code: Select all

administrator@dg31pr ~/Public $ DROPBOX_LIST=`ls ~/Dropbox`

administrator@dg31pr ~/Public $ echo $DROPBOX_LIST
Archives CTP-2011 Deployment Guide 2012 - Ubuntu 12.04 LTS fix the dock.desktop Mga Awitin My Templates Notices Photos Public rx-radio.sh Work
Redirecting, however:

Code: Select all

administrator@dg31pr ~/Public $ ls ~/Dropbox > DROPBOX_LIST_FILE

administrator@dg31pr ~/Public $ cat DROPBOX_LIST_FILE 
Archives
CTP-2011
Deployment Guide 2012 - Ubuntu 12.04 LTS
fix the dock.desktop
Mga Awitin
My Templates
Notices
Photos
Public
rx-radio.sh
Work
After saving the results to a file, use while read to extract the required information one line at a time from the file.

Using the example above, we have:

Code: Select all

while read LIST
do
  echo "value of LIST is $LIST"
done < DROPBOX_LIST_FILE
resulting in:

Code: Select all

value of LIST is Archives
value of LIST is CTP-2011
value of LIST is Deployment Guide 2012 - Ubuntu 12.04 LTS
value of LIST is fix the dock.desktop
value of LIST is Mga Awitin
value of LIST is My Templates
value of LIST is Notices
value of LIST is Photos
value of LIST is Public
value of LIST is rx-radio.sh
value of LIST is Work
Since at any given iteration, $LIST may contain spaces, don't forget to enclose it in double quotation marks to ensure the spaces within the filename is treated as part of the whole parameter instead of being treated as separate parameters.

For example:

Code: Select all

value of LIST is fix the dock.desktop
the following line:

Code: Select all

cp $LIST /tmp
expands to

Code: Select all

cp fix the dock.desktop /tmp
resulting in:

Code: Select all

cp: cannot stat `fix': No such file or directory
cp: cannot stat `the': No such file or directory
cp: cannot stat `dock.desktop': No such file or directory
which is NOT what we want. So the rectify, enclose the variable in double quotation marks:

Code: Select all

cp "$LIST" /tmp
which translates to:

Code: Select all

cp "fix the dock.desktop" /tmp
As for automation, you can use cron to schedule your script. GIYF. :wink:

HTH.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
ZubenElgenubi

Re: Batch Create Symlinks, Files with Spaces

Post by ZubenElgenubi »

Change this:

Code: Select all

ln –s $item /mnt/Media/Music/;
to this:

Code: Select all

ln –s "$item" /mnt/Media/Music/;
and try again ;)
Locked

Return to “Scripts & Bash”