Python学习札记(1):预备知识--PowerShell超超基础入门

写在前面

我学python的指导用书是【learn Python 3 the hard way】,所以写的东西也是基于这本书及windows系统下。
powershell功能多样,本书作者把其作为预备知识,但要求并不高,只需掌握几个基本命令。
本文针对第一次接触编程的新手,大佬勿喷
欢迎指正

常用命令及其用途

  1. hostname:输出本计算机在网络中的名称
  2. get-help、gal、gcm:查找帮助、查找缩写、查找命令
  3. pwd:输出当前工作目录
  4. md:创建目录
  5. cd:到达其他目录
  6. ls:列出目录中的内容
  7. rm:删除目录/文件
  8. pushd:入栈(保存当前位置并到达新位置)
    popd:出栈(回到pushd保存的位置)
  9. ni:创建空文件
  10. cp:复制目录/文录
    11.1 robocopy:更可靠(强大)的复制
  11. mv:移动目录/文件
  12. more:分页查看文本内容
  13. type:查看全部文本内容
  14. exit:退出shell

Tips

  1. Tab 键自动填充代码块
  2. 通配符 ?* 模糊检索文件
  3. powershell中不区分大小写
  4. 所有系统都可使用正斜杠 / 来表示路径,windows中 / \ 都可以
  5. 文件/目录名称中带有空格的需要添加引号,如:"happy new year"
  6. 所有命令若不指定路径则在当前工作目录下生效
  7. 使用 , 分隔多个对象名称可以使命令同时对多个对象生效

命令语法

powershell的语法结构是:cmdlet命令+空格+参数
所有命令都有很多可设置的参数,这里不需要掌握太过复杂的语法
( 参数解读可参考【大方子:powershell 关于命令帮助文件中参数的解读】)
通过这些命令你可以完成文件与文件夹的创建、访问、更改、删除
(以下输出内容中仅包括部分输出内容)

1. hostname:

PS C:\Users\一个特立独行的嗝> hostname
LAPTOP-N4HDHVAN

2. get-help、gal、gcm:

get-help:

检索某确定命令的全称、语法等等
需要注意的是,这里得到的语法远超本书要求,无意深入学习者不必理会

PS C:\Users\一个特立独行的嗝> get-help gal
名称
    Get-Alias
摘要
    Gets the aliases for the current session.
语法
    Get-Alias [-Definition <String[]>] [-Exclude <String[]>] [-Scope <String>] [<CommonParameters>]
    Get-Alias [[-Name] <String[]>] [-Exclude <String[]>] [-Scope <String>] [<CommonParameters>]

更为方便的用法是

PS C:\Users\一个特立独行的嗝> gc -?
名称
    Get-Content
摘要
    Gets the content of the item at the specified location.

gal:Get-Alias

检索缩写对应的全拼

PS C:\Users\一个特立独行的嗝> gal gc                                                        
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           gc -> Get-Content


PS C:\Users\一个特立独行的嗝> gal *i                                                        
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cli -> Clear-Item
Alias           cpi -> Copy-Item
Alias           gci -> Get-ChildItem
Alias           gi -> Get-Item
Alias           gwmi -> Get-WmiObject
Alias           ii -> Invoke-Item

gcm:Get-Command

检索命令

PS C:\Users\一个特立独行的嗝> gcm *location
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-Location                                       3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Get-WinHomeLocation                                2.0.0.0    International
Cmdlet          Pop-Location                                       3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Push-Location                                      3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Set-Location                                       3.1.0.0    Microsoft.PowerShell.Management
Cmdlet          Set-WinHomeLocation                                2.0.0.0    International


PS C:\Users\一个特立独行的嗝> gcm *i
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           cli -> Clear-Item
Alias           cpi -> Copy-Item
Alias           gcai ->                                            1.0.0.0    CimCmdlets
Alias           gci -> Get-ChildItem
Alias           gi -> Get-Item
Alias           gwmi -> Get-WmiObject

3.pwd:print working directory

Get-Location 的别名,实际上并没有实际用处,工作目录一看便知

PS C:\Users\一个特立独行的嗝> pwd
Path
----
C:\Users\一个特立独行的嗝

4.md:Make Directory

mkdir 也是它的别名,创建目录
实际上此命令内部调用的是New-Item命令,指定参数–type的值为“Directory”,所以当你检索md命令时得到的是关于New-Item的帮助

PS C:\Users\一个特立独行的嗝> md -?
名称
    New-Item
摘要
    Creates a new item.
PS C:\Users\一个特立独行的嗝> md temp(默认在当前目录下创建)
   
    目录: C:\Users\一个特立独行的嗝
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:43                temp


PS C:\Users\一个特立独行的嗝> ni temp -type directory(使用New-Item命令创建)
ni : 具有指定名称 C:\Users\一个特立独行的嗝\temp 的项已存在。


PS C:\Users\一个特立独行的嗝> md temp/apple/blue/cat(可以一次性创建多层子目录,如果你指定的目录不存在,PowerShell会自动创建这些目录)
   
    目录: C:\Users\一个特立独行的嗝\temp\apple\blue
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:43                cat


PS C:\Users\一个特立独行的嗝> md D://domglaslzd/apple/blue(可以指定路径)
   
    目录: D:\domglaslzd\apple
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:43                blue

5.cd:change directory

Set-Location 的别名,到达文件系统的另外一个位置

PS C:\Users\一个特立独行的嗝> cd temp(到达temp下)
PS C:\Users\一个特立独行的嗝\temp> cd apple/blue/cat(可以指定路径)
PS C:\Users\一个特立独行的嗝\temp\apple\blue\cat> cd..(回到父目录)
PS C:\Users\一个特立独行的嗝\temp\apple\blue> cd ../../..(回退相应的目录)
PS C:\Users\一个特立独行的嗝> cd /(进入当前盘的根目录)
PS C:\> cd ~(回到默认工作目录(home目录))
PS C:\Users\一个特立独行的嗝>

6.ls:

Get-ChildItem 的别名,列出目录内容

PS C:\Users\一个特立独行的嗝\temp> ls(默认列出当前目录下的内容)

    目录: C:\Users\一个特立独行的嗝\temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:39                apple


PS C:\Users\一个特立独行的嗝\temp> ls apple/blue(列出指定路径下内容)

    目录: C:\Users\一个特立独行的嗝\temp\apple\blue
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:39                cat


PS C:\Users\一个特立独行的嗝\temp> ls -r apple(遍历指定目录下的对象,不指定路径则默认为当前路径)

    目录: C:\Users\一个特立独行的嗝\temp\apple
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:39                blue


    目录: C:\Users\一个特立独行的嗝\temp\apple\blue
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/2     19:39                cat

关于遍历:
实际上使用了-Recurce参数,Recurce,译作递归
遍历可以理解为 沿着特定搜索路线 依次对树/图中每个节点 做一次访问
在这里即对所有子项依次使用一次ls命令
效果是列出子目录及其下的一切内容

7.rm:Remove-Item

删除

PS C:\Users\一个特立独行的嗝> rm c:/u*/*/t*/a????/blue/cat(删除空文件夹/文件)
PS C:\Users\一个特立独行的嗝> rm -r temp/apple(删除含有子项的文件夹)

8.pushd、popd:Push-Location、Pop-Location

保存当前位置并到达新位置、回到pushd保存的位置

PS C:\Users\一个特立独行的嗝> pushd d:/domglaslzd/apple/blue
PS D:\domglaslzd\apple\blue> popd
PS C:\Users\一个特立独行的嗝>

9.ni:New-Item

创建新文件(默认)/文件夹/符号链接
(-type参数只能指定 “file”、“directory” 或 “symboliclink”)

PS C:\Users\一个特立独行的嗝> cd temp
PS C:\Users\一个特立独行的嗝\temp> ls(没有输出说明不含文件)
PS C:\Users\一个特立独行的嗝\temp> ni 1,2.txt,3.mp3,4.docx

    目录: C:\Users\一个特立独行的嗝\temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2020/2/3      0:31              0 1
-a----         2020/2/3      0:31              0 2.txt
-a----         2020/2/3      0:31              0 3.mp3
-a----         2020/2/3      0:31              0 4.docx

10.cp:Copy-Item

复制文件/文件夹

PS C:\Users\一个特立独行的嗝> cp temp temp/temp1(无法复制子项,相当于新建文件夹)
PS C:\Users\一个特立独行的嗝> cp temp/temp1 temp/apple(同一路径下的复制需重命名)
PS C:\Users\一个特立独行的嗝> ls temp

    目录: C:\Users\一个特立独行的嗝\temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/3      0:58                apple
d-----         2020/2/3      0:58                temp1
-a----         2020/2/3      0:31              0 1
-a----         2020/2/3      0:31              0 2.txt
-a----         2020/2/3      0:31              0 3.mp3
-a----         2020/2/3      0:31              0 4.docx


PS C:\Users\一个特立独行的嗝> cp temp/2* temp/temp1
PS C:\Users\一个特立独行的嗝> cp -r temp/temp1 temp/apple(复制包含子项的目录)
PS C:\Users\一个特立独行的嗝> ls -r temp/apple

    目录: C:\Users\一个特立独行的嗝\temp\apple
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/3      1:02                temp1

    目录: C:\Users\一个特立独行的嗝\temp\apple\temp1
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----         2020/2/3      0:31              0 2.txt(可见将子项也复制了)

robocopy:不需掌握

11.mv:Move-Item

移动

PS C:\Users\一个特立独行的嗝> ls temp

    目录: C:\Users\一个特立独行的嗝\temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/3      1:02                apple
d-----         2020/2/3      1:01                temp1
-a----         2020/2/3      0:31              0 1
-a----         2020/2/3      0:31              0 2.txt
-a----         2020/2/3      0:31              0 3.mp3
-a----         2020/2/3      0:31              0 4.docx


PS C:\Users\一个特立独行的嗝> mv temp/2* temp/5.txt(同路径下等于重命名)
PS C:\Users\一个特立独行的嗝> mv temp/temp1 d:/domglaslzd
PS C:\Users\一个特立独行的嗝> ls temp

    目录: C:\Users\一个特立独行的嗝\temp
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         2020/2/3      1:02                apple(可见temp1已移走了)
-a----         2020/2/3      0:31              0 1
-a----         2020/2/3      0:31              0 3.mp3
-a----         2020/2/3      0:31              0 4.docx
-a----         2020/2/3      0:31              0 5.txt

12.more:Get-Content

逐页查看文本内容

PS C:\Users\一个特立独行的嗝> more temp/5*
happy new year
happy new year
happy new year
happy new year

13.type:

cat 也是别名
查看全部文本内容,事实上调用的也是Get-Content命令

PS C:\Users\一个特立独行的嗝> type temp/5*
happy new year
happy new year
happy new year
happy new year

14.exit:

退出Power Shell

PS C:\Users\一个特立独行的嗝\temp> exit
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值