附錄練習 11-13 笨辦法學Python3

附錄練習 11 移動文件 (mv)

在這個練習中,你將會學習如何使用 mv 命令把一個文件從一個地方移動到另一個地方。

55.12.1 跟我做

Linux/macOS

練習 11 會話

$ cd temp
$ mv awesome.txt uncool.txt
$ ls
newplace uncool.txt
$ mv newplace oldplace
$ ls
oldplace uncool.txt
$ mv oldplace newplace
$ ls
newplace uncool.txt
$

Windows

練習 11 Windows 會話

> cd temp
> mv awesome.txt uncool.txt
> ls


Directory: C:\Users\zed\temp


Mode    LastWriteTime   Length  Name
----    -------------   ------  ----
d----   12/22/2011  4:52    PM      newplace
d----   12/22/2011  4:52    PM      something
-a---   12/22/2011  4:49    PM  0   iamcool.txt


-a---   12/22/2011  4:49    PM  0   neat.txt
-a---   12/22/2011  4:49    PM  0   thefourthfile.txt
-a---   12/22/2011  4:49    PM  0   uncool.txt


> mv newplace oldplace
> ls


Directory: C:\Users\zed\temp


Mode    LastWriteTime   Length  Name
----    -------------   ------  ----
d----   12/22/2011  4:52    PM      oldplace
d----   12/22/2011  4:52    PM      something
-a---   12/22/2011  4:49    PM  0   iamcool.txt
-a---   12/22/2011  4:49    PM  0   neat.txt
-a---   12/22/2011  4:49    PM  0   thefourthfile.txt
-a---   12/22/2011  4:49    PM  0   uncool.txt


> mv oldplace newplace
> ls newplace


Directory: C:\Users\zed\temp\newplace


Mode    LastWriteTime   Length  Name
----    -------------   ------  ----
-a---   12/22/2011 4:49 PM  0   awesome.txt


> ls    


Directory: C:\Users\zed\temp


Mode    LastWriteTime   Length  Name
----    -------------   ------  ----
d----   12/22/2011  4:52    PM      newplace
d----   12/22/2011  4:52    PM      something
-a---   12/22/2011  4:49    PM  0   iamcool.txt
-a---   12/22/2011  4:49    PM  0   neat.txt
-a---   12/22/2011  4:49    PM  0   thefourthfile.txt
-a---   12/22/2011  4:49    PM  0   uncool.txt



>

55.12.2 你學到的

移動文件,或者重命名,很簡單:給出原來的名字和新的名字即可。

55.12.3 附加練習

  • 將 newplace 目錄下的一個文件移動到另一個目錄下,然后再移動回來。

附錄練習 12 瀏覽文件 (less, MORE)

做這個練習需要用到目前為止已經學過的一些命令。你還需要一個能創建文本文檔(.txt)的文本編輯器,以下是你要做的:

  • 打開你的文本編輯器,在新文件中輸入一些東西。在 macOS 下,你可以用 TextWrangler,在 Windows 系統下你可以用 Notepad++,在 Linux 下可以用 Gedit。其他任何文本編輯器也都可以。
  • 把這個文件保存到桌面,然后命名為 test.txt。
  • 在 Shell 中用你學到的命令把這個文件復制到當前的工作目錄—— temp 目錄下。

做完這些,再完成下面的練習。

55.13.1 跟我做

Linux/macOS

練習 12 會話

$ less test.txt [displays file here]
$

就是這些,輸入 q 即可退出 less 瀏覽模式。

Windows

練習 12 Windows 會話

> more test.txt [displays file here]
>
警告!
在前面的練習結果中,我用了 [displays file here] 來指代程序的輸出結果,因為有些輸出結果比較復雜。你要知道你的輸出結果不是這個。

55.13.2 你學到的

這只是查看文件內容的一種方法。它很有用,因為當文件有很多行的時候,它可以翻頁。在附加練習部分你會做更多的操作。

55.13.3 附加練習

  • 再次打開你的文本文件,通過復制粘貼的方法把內容擴充到 50-100 行。
  • 再把它復制到 temp 目錄下。
  • 現在再做一遍練習,這一遍可以翻頁,Unix 系統可以用 空格鍵和 w 來上下翻頁,Windows 系統直接用空格鍵即可。
  • 再看看你創建的其他一些空文件。
  • cp 命令會覆蓋一些已經存在的文件,所以要復制的時候要小心。

附錄練習 13 Stream 文件 (cat)

在做這個練習之前你需要再多做一些準備工作,以便在練習中使用。用編輯器創建另一個名為 test2.txt 的文件,但是這次直接把它保存在 temp 目錄下。

55.14.1 跟我做

Linux/macOS

練習 13 會話

$ less test2.txt [displays file here]
$ cat test2.txt I am a fun guy.
Don't you know why? Because I make poems, that make babies cry.
$ cat test.txt
Hi there this is cool.
$

Windows

練習 13 Windows 會話

> more test2.txt [displays file here]

> cat test2.txt I am a fun guy.
Don't you know why? Because I make poems, that make babies cry.
> cat test.txt
Hi there this is cool.
>

55.14.2 你學到的

你已經學習了第一個命令,這個命令只是為了讓你檢查一下那個文件確實在。然后你把這個文件 cat 到屏幕,cat 命令是把整個文件內容全部呈現到屏幕上,沒有翻頁或者停止。

55.14.3 附加練習

  • 再創建幾個文件并使用 cat 命令。
  • Unix:試試 cat test.txt test2.txt ,看看會發生什么。
  • Windows: 試試 cat test.txt,test2.txt,看看會發生什么。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容