Symlinks are different than aliases, although they seem to serve the same purpose (more or less/I think). I need to be able to tell if a file is an alias, and
开发者_如何学编程if [ -h /path/to/file ]
doesn't work. Is there anything like this for aliases? Google was most unhelpful as aliases are apparently the name for something else in bash altogether.
Thanks!
The Finder stores the information that a file is an alias in the ResourceFork of the file. To read this metadata, I would use spotlight to determine the kind of the file; the following command will return the kind of the file, so you could then compare it in an if-statement.
mdls -raw -name kMDItemKind /path/to/test.pdf returns PDF (Portable Document Format)
mdls -raw -name kMDItemKind /path/to/test.pdf\ Alias returns Alias
An other way would incorporate Applescript, which is executable on the command line via osascript. To return the kind of a file, run:
tell application "Finder" to get kind of ((POSIX file "/path/to/test.pdf\ Alias") as alias)
I'm not quite clear if you're asking about:
- aliases (a way of making one command actually run something else)
- symlink (a way of making a shortcut to a file)
- hard link (two file names both pointing to the same file contents)
The command you were trying:
if [ -h /path/to/file ]
helps you figure out if a file is a symlink or not, e.g.:
$ touch newfile
$ ln -s newfile newlink
$ for f in newfile newlink; do
if [ -h "$f" ]; then
echo "$f is a symlink"
else
echo "$f is not a symlink"
fi
done
newfile is not a symlink
newlink is a symlink
If you mean: "how can I find out whether typing some command will run an alias", then you can use type
or alias
, e.g.
$ type ls
ls is aliased to `ls --color=auto --format=across'
$ type less
less is /usr/bin/less
If you're asking about hard links, find -inum
and ls -i
can help, but that is a more advanced topic.
Asmus' solution did not quite work for me, but he got me started. Here is what worked for me (macOS 10.13 , High Sierra, assuming you execute it in bash):
alias=$( mdls -name kMDItemKind "$file" )
if [[ "$alias" = *Alias* ]]
then
echo "$file is an Alias"
fi
HTH.
You could use the /usr/bin/getfileinfo
utility:
if ((`getfileinfo -aa /path/to/file`))
then #
fi
精彩评论