Page 1 of 1

readlink question

Posted: 17 Mar 2016, 15:56
by Rava
Hi, readlink --help or man readlink say
-f, --canonicalize canonicalize by following every symlink in
every component of the given name recursively;
all but the last component must exist
-e, --canonicalize-existing canonicalize by following every symlink in
every component of the given name recursively,
all components must exist
Me thinks the ones who coded readlink had a good reason why all but the last components of --canonicalize must exist. What would be a reason to have the last one non-existing or possible non-existing? I am quite sure the programmers had a good reason to make it so.

Re: readlink question

Posted: 23 Mar 2016, 16:49
by port
for example to be able to create a new file:

Code: Select all

touch `readlink -f mydir/mylink/newfile`
newfile does not exist when executing readlink but the path to it is a valid canonical path, so you can safely create the file with touch

if you use simply readlink or readlink -e you will get an error

of course, I'm supposing mydir and mylink currently exists, i.e.

Code: Select all

% mkdir -p mydir/tmp/myfolder
% ln -s tmp/myfolder mydir/mylink
% touch `readlink -f mydir/mylink/newfile`
% ls mydir/mylink
newfile