大网数据广告活动
服务器运维问答

How can Iinux powershell test if a file is a symbolic link?

· 52CE 我爱测

On Linux, I want to replace a bash script with powershell. The script needs to test a file to determine if it is a symbolic link. In bash, one can use the -L operator: if [[ -L "/tmp/mysteryfile" ]] ; then ; echo "It is a link" ;…

问题原文

原帖链接 · Charlweed · CC BY-SA 4.0

On Linux, I want to replace a bash script with powershell. The script needs to test a file to determine if it is a symbolic link. In bash, one can use the -L operator:

if [[ -L "/tmp/mysteryfile" ]] ; then ; echo "It is a link" ; fi

How can I do the same in powershell on Linux?

已采纳解答

原帖链接 · LotPings · CC BY-SA 4.0

On my Ubuntu 19.04 this:

PS> gci /bin/bz*|select Mode,LastWriteTime,Length,Name,Attributes,LinkType|ft -auto

yields

Mode   LastWriteTime     Length Name           Attributes LinkType
----   -------------     ------ ----           ---------- --------
--r--l 04.07.19 14:50:14  38984 bzcat            ReadOnly HardLink
-----l 04.07.19 14:50:14      6 bzcmp        ReparsePoint SymbolicLink
--r--- 04.07.19 14:50:14   2227 bzdiff           ReadOnly 
-----l 04.07.19 14:50:14      6 bzegrep      ReparsePoint SymbolicLink
--r--- 13.08.18 13:29:27   4877 bzexe            ReadOnly 
-----l 04.07.19 14:50:14      6 bzfgrep      ReparsePoint SymbolicLink
--r--- 04.07.19 14:50:14   3641 bzgrep           ReadOnly 
--r--l 04.07.19 14:50:14  38984 bzip2            ReadOnly HardLink
--r--- 04.07.19 14:50:14  14328 bzip2recover     ReadOnly 
-----l 04.07.19 14:50:14      6 bzless       ReparsePoint SymbolicLink
--r--- 04.07.19 14:50:14   1297 bzmore           ReadOnly 

So the Mode l denotes a link, as does the Attribute ReparsePoint denote a SymbolicLink.

But only the LinkType differentiates between Hard-/SymbolicLinks

if((Get-Item "/tmp/mysteryfile").LinkType -eq 'SymbolicLink'){"It is a SymbolicLink"}

or

if($LT=(Get-Item "/tmp/mysteryfile").LinkType){"It is a $LT"}

To see some stats:

PS> gci /bin -rec -force -ea 0|Where LinkType|Group LinkType -NoElement               

Count Name
----- ----
    3 HardLink
  276 SymbolicLink

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。