本章主要承接第4章中关于文件系统的话题,探讨 PowerShell 中如何进行基本的文件操做等,也据此做为第一部分的总结。ide
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-Item
或 Get-ItemProperty
:post
# 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 族:code
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 调用二进制便可:ci
# 运行于 Windows 上,将打开 .log 格式绑定的默认应用程序 PS C:\Users\chuny> Invoke-Item .\AMDRM_Install.log
Clear-Item
主要用于 Windows 平台清理某个注册表中的值集合,不经常使用。rem
操做文件属性,依靠 Item
对象属性相关的 getter / setter 函数,即 Get / Set-ItemProperty
:get
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