How does symlink work on macOS

XiaoPeng
cloud-tech
Published in
1 min readDec 20, 2020

I am working on a WordPress project. I accidentally removed a folder when deleting plugin. Let’s say our plugin’s name is simple-code-block. We built the project at ~/workplaces/wordpress/plugins/simple-code-block. The Wordpress is installed at /Applications/MAMP/htdocs/wordpress/. I to be able to load the plugin directly from the development folder I created a symlink. When I delete the plugin the content in the folder is removed.

So I did a test on how symlink works:

// 1. create two testing folders
$ mkdir -p symlinks/wordpress/wp-content/plugins
$ mkdir -p symlinks/workspsaces/simple-code-block
$ touch symblinks/workspaces/simple-code-bloci/a-file.php
// 2. create a symblink in the wordpress plugins folder
$ cd symlink/w/w/p/ && ln -s ../../../workspaces/simple-code-block
// 3. rename wordpress, and check if symblink works
$ mv symlinks/wordpress symblinks/wordpress_2
$ ls symblinks/wordpress_2/simple-code-block
> a-file.php
// 4. remove wordpress_2 and check if the scb folder is removed
$ rm -rf symblinks/wordpress_2
$ ls symblinks/workspaces/simple-code-block
> a-file.php

Explanation

A symbolic link, also termed a soft link, is a special kind of FILE that points to another file.

So when you rename the parent folder, the symlink is kept. When you delete the parent folder, the symlink is deleted, but the target folder is not deleted.

--

--