I recently started using Hazel. Hazel is a macOS app for managing your files.
Today, cloud providers like Apple, Google, Microsoft and Dropbox offer a decent amount of space for very cheap. For example, most of them provide 200GB for just ~3£ per month. There is no excuse, therefore, not to upgrade to such plan and start storing your documents there. This way we can have a backup of our data on the cloud, and also anywhere access to our data.
I wanted to achieve the following workflow:
- Keep all my documents (which they are always under
~/Documents) on the cloud so I can access them from any device.
- Keep symbolic links of those files from the cloud to my local
~/Documentsdirectory so I can easily access the data from the terminal.
Some conventions before we start:
local directory refers to the directory on the
computer that is not on the cloud (in this specific case will be
remote directory refers to the directory on the cloud that holds the files
local directory and then creates symbolic links of its files to the
Rules for the local directory
Add the local folder you want to sync (i.e in my case is
~/Documents) in Hazel’s Folders
section. In this folder we need to define two rules:
- Move any document from that local directory to the remote directory.
- Delete any documents/aliases from the local directory if they do not exist on the remote directory.
Local Directory, Rule #1: Move files from local directory to remote directory
For the first rule we need to move files from the local directory to the remote directory:
The embedded script has the following code:
if [[ ! -L "$1" ]] # If the file is not a symbolic link. then exit 0 else exit 1 fi
Local Directory, Rule #2: Delete any symbolic links that do not exist on the remote directory
At some point, we might delete some files from the remote directory and we need a way to also wipe the symbolic links of those files from the local directory. Unfortunately, there is no “delete-file” trigger for Hazel so we had to find a hack around it.
The embedded script for the second rule is:
fname=$(basename "$1") if [ ! -e "/Users/rafael/Library/Mobile Documents/com~apple~CloudDocs/documents/$fname" ]; then rm -rf $1 fi
Here we are basically saying the following: “If the file/directory currently being processed by Hazel does not exist on the remote directory, then delete the symbolic link from the local directory.”
So in this rule, we are constantly verifying whether what we have in our local directory (which will always be a symbolic link to the remote directory) actually exist on the remote directory. If not, then this means that the file has been deleted from the remote directory and hence we should delete the symbolic link from the local directory.
Rules for the remote directory
For the remote directory we have only one rule:
The embedded script is:
ln -s "$1" /Users/rafael/Documents/
We are basically creating a symbolic link for every document in the remote directory to the local directory.
Note that Hazel provides a way to create aliases but these are Finder aliases which they are not a UNIX concept and hence won’t work with the terminal. Therefore, in here we are creating a symbolic link instead that has the advantage that you can access the directory/file pointed by the symbolic link directly from the terminal and the finder.