Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out whether a file is a symbolic link in PowerShell

I am having a PowerShell script which is walking a directory tree, and sometimes I have auxiliary files hardlinked there which should not be processed. Is there an easy way of finding out whether a file (that is, System.IO.FileInfo) is a hard link or not?

If not, would it be easier with symbolic links (symlinks)?

like image 235
Joey Avatar asked May 03 '09 19:05

Joey


People also ask

How do I get a list of symbolic links?

Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.

How do I list all symbolic links in Windows?

In Command Prompt, run this command: dir /AL /S c:\ A list of all of the symbolic links in the c:\ directory will be returned.

How do you check if a directory is a symbolic link bash?

Your Bash script might need to determine if a file is a symlink or not. In Bash you can test this with the -L operator that returns true if the file exists and is a symlink.


1 Answers

Try this:

function Test-ReparsePoint([string]$path) {   $file = Get-Item $path -Force -ea SilentlyContinue   return [bool]($file.Attributes -band [IO.FileAttributes]::ReparsePoint) } 

It is a pretty minimal implementation, but it should do the trick. Note that this doesn't distinguish between a hard link and a symbolic link. Underneath, they both just take advantage of NTFS reparse points, IIRC.

like image 122
Keith Hill Avatar answered Sep 21 '22 18:09

Keith Hill