Bash Symlink – How to Create and Overwrite If One Exists

bashsymlink

I want to take down data in /path/to/data/folder/month/date/hour/minute/file and symlink it to /path/to/recent/file and do this automatically every time a file is created.

Assuming I will not know ahead of time if /path/to/recent/file exists, how can I go about creating it (if it doesn't exist) or replacing it (if it does exist)? I am sure I can just check if it exists and then do a delete, symlink, but I'm wondering if there is a simple command which will do what I want in one step.

Best Answer

Please read the manual.

ln -sfn /new/target /path/to/symlink

$ man ln

-n, --no-dereference
treat LINK_NAME as a normal file if it is a symbolic link to a directory

Related Question