How to set link (hard link and symbolic link) on Mac OS

Today, I tried to make my work flow of writing blog easier. As my typora setting is when I drop a image to typora, it will make a copy to ./images. However, hexo need the image source in ../images. So before I just use manual copy to make it. Today, I just want to simplify this process. I tried use command ln to do this, however, it does not work for git, which lead me to make a version fallback(also, I have made some other wrong commands to try to change it back). So my every old post added a label Edited on 2023-12-17 . Though it is not worked for, I learned how to use ln to create hard link and symbolic link. Now I am sharing this with you.

Hard link

Hard link can only set with file, not folder. You can understand it as it create a new link to a block on hard drive, which means even though the old file is deleted, the hard link won’t be affected as the block still has a link so the block won’t be cleared by the system.

Usage

1
2
3
4
5
6
ln origin_file_path hard_link_path
#eg:
# use real path
ln ~/Dropbox/surge.conf ~/.surge.conf
# use relative path
ln ../surge.conf surge.conf

Symbolic link (Soft link)

Soft link can set with not only file, but also folder. This is similar to the shortcut, which means if the original file or path is deleted, the soft link won’t work anymore. Remember, if you want to set soft link with folder, you must use realpath.

Usage

1
2
3
4
5
6
ln -s origin_file_path hard_link_path
#eg:
# use real path (with file or folder)
ln ~/Dropbox/ ~/Surge
# use relative path (can only use with file)
ln ../surge.conf dropbox.conf

Reference

  1. https://slarker.me/mac-file-link/
  2. https://www.jianshu.com/p/2cb576d064c0