When I create a hard link using ln
:
ln testfile.txt testfile2.txt
The file hardlink, confirmed by the same inode numbers for both aliases in the inode table with ls -li
, has the same initial permissions for the hardlinked file as the file with the original name:
1222285 -rw------- 2 cs9****-** cs****** 98 Apr 9 15:00 testfile.txt
1222285 -rw------- 2 cs9****-** cs****** 98 Apr 9 15:00 testfile2.txt
When I create a symbolic link, however:
ln -s testfile.txt testfile3.txt
and confirm again with the previous ls
command, it show that it is a soft link with l
in the permissions list. However, my symlink has all permissions for all user types (group, user, and other):
1222285 -rw------- 2 cs9****-** cs****** 98 Apr 9 15:00 testfile2.txt
1222285 -rw------- 2 cs9****-** cs****** 98 Apr 9 15:00 testfile.txt
1222286 lrwxrwxrwx 1 cs9****-** cs****** 12 Apr 9 15:08 testfile3.txt -> testfile.txt
This brings me to my questions:
Why are the initial permissions of a symbolic link all-permissive? I understand that is is a different filetype, but why does it start out with all initial permissions? From a question on UNIX/Linux.SE, I discovered that the default permissions for a directory are 777, which happen to be the same permissions as my symbolic link. Are the permissions of a directory and a symbolic link somehow related?
How can I create symlinks that have the same initial permissions as the original? Specifically, the reason I want do this is that I want to write a shell script to go into a single file system and make hard links and soft links depending on the type of file, and I want it to preserve the permissions.