Sunday, July 23, 2017

Accessing iCloud (and Dropbox) synched data from non-iCloud synched directories

Lately, I've found iCloud to be an amazing tool. Having switched between four computers in the last year (an abnormal year), I found iCloud synchronization provided a painless way to automagically have the same data across multiple computers using the same iCloud account. But what didn't work for me was having to move all those files to /Desktop or /Documents in order for iCloud to sync them. For instance, I prefer keep all development files in my user's root or home directory (ex. /Users/myusername), since the Terminal will launch by default to this folder (this too you can change, but I'd rather not). Fortunately, there's a way around this using symbolic links. Unfortunately, you may still need to move the actual files into iCloud, but you can still work from the User's home directory and have the changes propagated across all computers using iCloud.  You will need to execute the symlink command on each computer after it has finished synching with iCloud. I say "may" need to move the actual files because I didn't have much luck leaving them in the /User/username directory and putting the symlinked file in iCloud. iCloud didn't seem to want to sync the symlinked file.  But there may still be a way around that, like using Dropbox or by altering this technique.


 How to: 

  1. Move the file(s) to an iCloud synched directory (ex. /Users/username/Desktop/desired_directory_or_file) 
  2. Open Terminal.app or your shell of choice. 
  3. Navigate to the directory where you'd like to place the symlinked folder. (ex. /Users/myusername/) 
  4. Run command: `ln -s /path/to/folder symlink_name` (ex. `ln -s /Users/myusername/Desktop/Work/dev dev`) 
  5. Repeat symlink command for each computer you use (depending on the size of the directory contents, you may need to wait for iCloud to finish synching)


Notes on `ln` command and args: 

  • `ln` is a command used to make links between files and folders on your file system as well as external file systems 
  • `-s` flag is required to make a symbolic link 
  • The first argument is the path to the folder/file you wish to use as the original folder/file, where all the files are actually stored. 
  • The second argument is the name of the directory (or symlink) you wish to create. We execute this command from the path where you desire the symlink. In our case, we want the symlink to reside in the Users home (ex. /Users/myusername/).
This also works with Dropbox (and presumably other cloud storage applications), so you're not limited to iCloud synchronization.