PowerShell 学习笔记 - 5 操作文件

PowerShell 学习笔记 - 5 操作文件

本章主要承接第4章中关于文件系统的话题,探讨 PowerShell 中如何进行基本的文件操作等,也据此作为第一部分的总结。

Location 与 Item

PowerShell 中将 UNIX 常用的 cd / pwd / pushd / popd 通过别名的方式提供了出来,也标识出路径所对应的名词应当是 Location

# PowerShell Core 下的导航
PS /> Get-Alias cd

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cd -> Set-Location


PS /> Get-Alias pwd

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           pwd -> Get-Location
PS /> Get-Alias popd

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           popd -> Pop-Location


PS /> Get-Alias pushd

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           pushd -> Push-Location

Get-Location 所返回的对象并不包含详细的作为文件对象的信息,用于获取详细信息的的即为 Get-ItemGet-ItemProperty

# Get-Location 返回的对象不包含详细的文件信息,只是提供该 Location 相对于 Provider 下的信息
PS /root> Get-Location | Get-Member -MemberType Property
   TypeName: System.Management.Automation.PathInfo
Name         MemberType Definition
----         ---------- ----------
Drive        Property   System.Management.Automation.PSDriveInfo Drive {get;}
Path         Property   string Path {get;}
Provider     Property   System.Management.Automation.ProviderInfo Provider {get;}
ProviderPath Property   string ProviderPath {get;}

# Get-Item 用于获取路径对应的对象,其所携带的便该对象的详细信息
PS /root> Get-Item -Path .| Get-Member -MemberType Property
   TypeName: System.IO.DirectoryInfo
Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   datetime CreationTime {get;set;}
CreationTimeUtc   Property   datetime CreationTimeUtc {get;set;}
Exists            Property   bool Exists {get;}
Extension         Property   string Extension {get;}
FullName          Property   string FullName {get;}
LastAccessTime    Property   datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property   datetime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   datetime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   datetime LastWriteTimeUtc {get;set;}
Name              Property   string Name {get;}
Parent            Property   System.IO.DirectoryInfo Parent {get;}
Root              Property   System.IO.DirectoryInfo Root {get;}

通过 Get-ChildItem 亦可获得该路径下的所有子对象,并可对其进行操作:

PS /> Get-ChildItem -Path .
    Directory: /
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----l           8/4/18  10:04 PM                bin
d-----          9/19/18   5:52 AM                dev
d-----          9/15/18   5:52 AM                etc
d-----          4/11/18   4:59 AM                home
d----l           8/4/18  10:04 PM                lib
d----l           8/4/18  10:04 PM                lib64
d-----          4/11/18   4:59 AM                media
d-----          4/11/18   4:59 AM                mnt
d-----          9/13/18  11:37 PM                opt
d-r---          9/19/18   5:52 AM                proc
d-r---          9/15/18   5:52 AM                root
d-----          9/18/18   3:51 AM                run
d----l           8/4/18  10:04 PM                sbin
d-----          4/11/18   4:59 AM                srv
d-r---          9/19/18   5:52 AM                sys
d-----          9/19/18   5:52 AM                tmp
d-----           8/4/18  10:04 PM                usr
d-----           8/4/18  10:04 PM                var
------           8/4/18  10:05 PM          12005 anaconda-post.log

# 从 ChildItem 中选取 Item
PS /> (Get-ChildItem -Path .)[0]
    Directory: /
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d----l           8/4/18  10:04 PM                bin

# 也可附加 -Recursive 以进行递归搜索,这样将获取该路径下的所有 Item
# 例如在 /opt 下有 526 个 Item
PS /> (Get-ChildItem -Path /opt -Recurse).Length
526

# 同时也可附加 -Include 以筛选部分需要的 Item
# 例如在 /opt 下面有 400 个 DLL 库
PS /> (Get-ChildItem -Path /opt -Recurse -Include *.DLL).Length
400

文件(对象)操作

文件(对象)操作均依赖于 *-Item cmdlet 族:

PS /tmp> Get-Command -noun item

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Clear-Item                                         6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Copy-Item                                          6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-Item                                           6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Invoke-Item                                        6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Move-Item                                          6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          New-Item                                           6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Remove-Item                                        6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Rename-Item                                        6.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Set-Item                                           6.1.0.0    Microsoft.PowerShell.Management

文件或路径等创建,可使用 New-Item

# 创建 Directory 类型的文件,即为目录
PS /tmp> New-Item -ItemType Directory newDir
    Directory: /tmp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----

# 创建 File 类型的文件,即为普通文件
PS /tmp> New-Item -ItemType File newFile
    Directory: /tmp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          9/19/18   6:38 AM              0 newFile

# 创建 SymbolicLink 类型的文件,即为符号链接
PS /tmp> New-Item -Name yum.log.link -Type SymbolicLink -Value ./yum.log
    Directory: /tmp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-----l          9/19/18   6:42 AM             12 yum.log.link

文件或路径等移动 / 复制 / 删除 / 重命名等,可使用 Move / Copy / Remove / Rename-Item

PS /tmp> Move-Item -Path ./newFile -Destination ./newDir/
PS /tmp> Copy-Item -Path ./newDir/newFile -Destination ./

# newDir 非空,所以在删除时可以加上递归,以省略到确认过程
PS /tmp> Remove-Item -Force ./newDir/

Confirm
The item at /tmp/newDir/ has children and the Recurse parameter was not specified. If you continue, all children will
be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
PS /tmp> Remove-Item -Recurse ./newDir/

PS /tmp> Rename-Item -Path ./newFile -NewName nextFile
PS /tmp> Get-Item ./nextFile
    Directory: /tmp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
------          9/19/18   6:38 AM              0 nextFile

Invoke-Item 用于打开文件,主要见于 Windows 平台,UNIX 平台直接使用 CLI 调用二进制即可:

# 运行于 Windows 上,将打开 .log 格式绑定的默认应用程序
PS C:\Users\chuny> Invoke-Item  .\AMDRM_Install.log

Clear-Item 主要用于 Windows 平台清理某个注册表中的值集合,不常用。

操作文件属性,依靠 Item 对象属性相关的 getter / setter 函数,即 Get / Set-ItemProperty

PS /tmp> (Get-ItemProperty ./yum.log).IsReadOnly
False
PS /tmp> Set-ItemProperty ./yum.log -Name isReadOnly -Value $true
PS /tmp> (Get-ItemProperty ./yum.log).IsReadOnly
True
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值