kjo
kjo

Reputation: 35311

How to test whether a path is a symbolic link?

MATLAB has a built-in predicate function, isdir, that takes as string as its argument, and returns true iff this argument represents the path to a directory.

I'm looking for a similar predicate, taking the same type of argument, but returning true iff this argument represents the path to a (Unix) symbolic link. (This returned value should be true for any symlink, irrespective of what type of file its target is, or whether this target exists at all.)

If MATLAB has no such predicate, then I'd like to know how best to implement it.

Upvotes: 1

Views: 954

Answers (2)

beaker
beaker

Reputation: 16791

unix('test -L slowlink.m')

Returns 1 if the file either does not exist or is not a link, 0 if it is a link (backwards, I know).

Upvotes: 1

Hennadii Madan
Hennadii Madan

Reputation: 1623

Did not test this since don't have matlab on this machine, BUT, let;s say your path string is in 'path_string' variable, then:

function a_winrar_is_you = issymlink(path_string)

[status, out] = unix(strcat('file ',path_string));
if ~isempty(strfind(out,'symbolic'))
    a_winrar_is_you = true;
else
    a_winrar_is_you = false;
end

end

Note that this is buggy, namely you'll have to check the exit status of file command, and it will give false positives on paths that contain 'symbolic', but I leave it up to someone less lazy than me (you) to polish it.

Upvotes: 0

Related Questions