天天看點

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二

首先請看我的腦圖

④ MANIPULATINGFILES AND DIRECTORIES

本章将介紹五個最常用的Linux系統指令行指令:cd、mv、mkdir、rm、ln。實際上在圖形化界面中我們對檔案的操作非常容易,而Linux系統大多數也具有圖形化界面,那麼我們為什麼還要去學這些command line呢?稍安勿躁,通過這一章的深層學習,我們就會明白指令行操作的簡潔與強大。

書中舉了一個例子:The answer is power and flexibility. While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command-line programs. For example, how could we copy all the HTML files from one directory to another—but only those that do not exist in the destination directory or are newer than the versions in the destination directory? Pretty hard with a file manager. Pretty easy with the command line:

cp -u *.html destination

是不是看不太明白?不明白就對啦,這裡運用了wildcard(通配符),我們羅列幾個表給諸位。

Before we begin using our commands, we need to talk about the shell feature that makes these commands so powerful. Because the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are called wildcards. Using wildcards (also known as globbing) allows you to select filenames based on patterns of characters.

表一

wildcard Matches
* Any characters
? Any single character
[ ] Any character that is a member of the set characters
[! ] Any character that is not a member of the set characters
[[:class:]] Any character that is a member of the specified class

表二

Character Class
[:alnum:] Any alphanumeric character
[:alpha:] Any alphabetic character
[:digit:] Any numeral
[:lower:] Any lowercase letter
[:upper:] Any uppercase letter

實際上表内的這種規範化,是對檔案的相對精密的查找,或者說對檔案的範圍的規定,書中還舉了幾個例子來幫助我們消化了解:

Pattern
g* Any file beginning with g
b*.txt Any file beginning with b followed by any characters and ending with .txt
Data??? characters and ending with .txt Data??? Any file beginning with Data followed by exactly three characters
[abc]* Any file beginning with either a, b, or c
BACKUP.[0-9][0-9][0-9] Any file beginning with BACKUP. followed by exactly three numerals
[[:upper:]]* Any file beginning with an uppercase letter
[![:digit:]]* Any file not beginning with a numeral
*[[:lower:]123] Any file ending with a lowercase letter or the numerals 1, 2, or 3

同時通配符對GUI界面也依然适用,如下圖所說:

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二

通配符将會在第七章中詳細為大家解釋,我們繼續往下走。

mkdir操作

即簡單的建立目錄操作,操作指令如下:

輸入:[me@linuxbox ~]$ mkdir test01 test02

驗證:[me@linuxbox ~]$ ls

輸出:test01 test02

cp操作

The cp command copies files or directories. It can be used two different ways:

cp item1 item2

to copy the single file or directory item1 to file or directory item2 and:

cp item... directory

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二
2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二
上表是對cp操作的一些細化描述 (表二是重點,可以詳細參閱)

mv操作

The mv command performs both file moving and file renaming, depending on how it is used. In either case, the original filename no longer exists after the operation. mv is used in much the same way as cp:

mv item1 item2

to move or rename file or directory item1 to item2 or

mv item... directory

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二
2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二
同理,上表則是對mv操作的一些細化描述 (表二是重點,可以詳細參閱)

rm操作

The rm command is used to remove (delete) files and directories, like this:

rm item...

2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二
2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二
依然是對rm操作的細化描述,同時關于rm操作,書中特别舉了個例子:
2020-2021-1學期 20192428《THE LINUX COMMAND LINE 》讀數筆記二

TIP:Whenever you use wildcards with rm (besides carefully checking your typing!), test the wildcard first with ls. This will let you see the files that will be deleted. Then press the up arrow key to recall the command and replace the ls with rm.

因為Linux太過信任你了,是以你的rm操作是不可恢複的,是以書中建議在rm操作時先将rm更換為ls操作,這樣可以先勘察到你所選中的檔案,在确定所選擇範圍正确後再進行rm操作。

ln操作

The ln command is used to create either hard or symbolic links. It is used in one of two ways:

ln file link

to create a hard link and

ln -s item link

to create a symbolic link where item is either a file or a directory.

第四章的最後,書主要是對以上五條指令操作的聯系與運用,各位如果感興趣可以多花一些功夫去攻讀一下!