Linux命令行基礎(chǔ)
本文內(nèi)容根據(jù)Linux Command Line Basics & Excaples進(jìn)行改編
Linux命令行簡(jiǎn)要介紹
AT&T公司于20世紀(jì)70年代發(fā)布了UNIX系統(tǒng)铺然。經(jīng)過(guò)多年的發(fā)展芳来,Unix不再是某一個(gè)具體操作系統(tǒng)的名稱(chēng)纱烘,而是對(duì)遵循Unix規(guī)范纯续、設(shè)計(jì)和哲學(xué)的一類(lèi)操作系統(tǒng)的統(tǒng)稱(chēng)麸俘。還有一些操作系統(tǒng)冒窍,它們遵循Unix設(shè)計(jì)饮怯、有著與Unix類(lèi)似的規(guī)范和標(biāo)準(zhǔn)帘腹,這些操作系統(tǒng)被稱(chēng)為類(lèi)Unix系統(tǒng)(Unix-like),Linux就是其中的一員筒繁。
在設(shè)計(jì)上Unix包含一個(gè)Unix Shell噩凹。它是一種命令行解釋器(CLI)或者Shell,可以讓用戶(hù)通過(guò)輸入命令與系統(tǒng)交互毡咏。Unix Shell既可以直接執(zhí)行用戶(hù)輸入的命令驮宴,也可以從文件中讀取命令執(zhí)行(shell scripting)。最常用的Unix Shell是Bash呕缭,幾乎所有的Linux發(fā)行版中都內(nèi)置有Bash堵泽。通常所說(shuō)的Linux命令行就是Bash命令或Bash腳本。
Linux命令行以強(qiáng)大靈活著稱(chēng)恢总,使用少數(shù)命令就可以執(zhí)行許多任務(wù)迎罗,還可以將許多任務(wù)自動(dòng)化。
Linux命令行基礎(chǔ)
Linux啟動(dòng)后片仿,就會(huì)創(chuàng)建一個(gè)shell會(huì)話(shell session)纹安。shell session是一個(gè)基礎(chǔ)環(huán)境,它允許系統(tǒng)與應(yīng)用、及應(yīng)用間進(jìn)行通訊厢岂」舛剑可以一次打開(kāi)多個(gè)會(huì)話,會(huì)話間可以存在父子關(guān)系塔粒,如果當(dāng)前會(huì)話的父會(huì)話被關(guān)閉可帽,當(dāng)前會(huì)話也會(huì)被終止。
上圖是VSCode遠(yuǎn)程開(kāi)發(fā)模式下窗怒,連接到Windows10 WSL(Ubuntu18.04.2)的截圖映跟。光標(biāo)前面的內(nèi)容格式如下:
username@hostname:locate
,對(duì)應(yīng)到上圖扬虚,即當(dāng)前會(huì)話的用戶(hù)名為wjchi努隙,hostname是DESKTOP-J2OGSVG,當(dāng)前目錄為~辜昵,即當(dāng)前用戶(hù)的家目錄:/home/wjchi荸镊。
下面是一些Linux常用符號(hào)的含義:
SYMBOL | EXPLANATION | EXAMPLES | ||
---|---|---|---|---|
~ | is equal to the current user's home directlry. E.g: /home/someone/ | cd ~ ls ~ | ||
* | A symbol which stands for "everything". Let's say you want to remove all the .jpg files from your Downloads folder which have their name starting with the "E" character, then you can use this symbol to represent all the other letters except E. See the example. | rm ~/Downloads/E.jpg ls /etc/c nano /var/log/nginx/* | ||
& | Run a command in the background. It will return the PID of the newly running process to you and won't show you the output. | sudo apt update & | ||
&& | These symbols written together stand for "and". So if you want to run 2 commands together, you can use it. | sudo apt update && sudo apt upgrade | ||
\ | Allows you to continue writing commands/Bash syntax in new line. | sudo \ apt \ update | ||
.. | In many cases, especially in navigation, the two dots stand for the parent folder. | cd .. | ||
. | In navigation or referring to files/folders, the dot stands for the current folder. | ls . | ||
# | Everything after this symbol in the same line is considered to be a comment, so it won't be processed by the shell. | cd # This commands moves you somewhere. | ||
This is called "Piping", which is the process of redirecting the output of one command to the input of another command. Very useful and common in Linux/Unix-like systems. | cat /etc/profile | grep bash | ||
> | Take the output of a command and redirect it into a file (will overwrite the whole file). | ls ~ > output.txt | ||
< | Read the contents of a file into the input of a command. | grep bash < /etc/profile | ||
>> | Append a text or a command output into the last line of a file. | echo "First Line" > output.txt echo "See this is the last line" >> output.txt |
我們可以使用man
命令或者命令后加上--help
來(lái)查看各個(gè)命令的說(shuō)明,man是manual的簡(jiǎn)寫(xiě)堪置。在命令行輸入:man man
躬存,輸出如下:
Linux中常用導(dǎo)航命令如下:
BASE COMMAND | EXPLANATION | FAMOUS ARGUMENTS & OPTIONS | EXAMPLES |
---|---|---|---|
cd | This command allows you to move into a different directory on your Linux system, which will make it the current folder of where your shell is running. It's just like as if you open a specific folder in any graphical file manager. | . Stands for the current directory. .. Stands for the parent directory. ../.. the parent of the parent directory. | cd /etc/ |
ls | Lists the current files and folders in a specific directory. | -a shows the hidden files too. -l shows metdata about files and folders, such as permissions, last modification date, size and so on. | ls ~ |
pwd | Gives you the current location | - | - |
Linux中常用文件/目錄操作命令入下:
BASE COMMAND | EXPLANATION | FAMOUS ARGUMENTS & OPTIONS | EXAMPLES | |
---|---|---|---|---|
touch | Make a new file. | - | touch text.txt | |
mkdir | Make a new folder | -p Make the requested path regardless if the sub-directories exist or not (because normally, mkdir would return an error if you try to make a 3rd-level directory while the 2nd-level directory doesn't exist). | mkdir newfolder mkdir something\ with\ spaces mkdir -p newfolder/subfolder/subsubfolder | |
rm | Remove a file or a directory. | -rf Adds the ability to remove folders and their contents (because normal rm can't). | rm file.txt rm -rf foldername | |
head | Get the first 10 lines of a text file (or the first n lines of a file) | -n Specify the number of lines to output from the beginning. | head /etc/profile head -n 19 /etc/profile | |
tail | Get the last 10 lines of a text file (or the last n lines of a file). | -n Specify the number of lines to output from the end. | tail /etc/profile tail -n 18 /etc/profile | |
cat | Output all the contents of a file | - | cat /etc/profile | |
grep | Output the lines contain a certain string from a file only. | - | cat /etc/profile | grep "Bash" |
chmod | Change the permissions of a file. | +x Make the file executable 777 Allow the file to accessed, written and executed by everyone (very dangerous!). 755 Allow everyone to read the file, but only the owners to edit and execute it. | chmod +x test.sh chmod 755 test.sh |
Bash配置文件
Unix設(shè)計(jì)哲學(xué)中包含一條準(zhǔn)則:一切皆文件,everything is a file舀锨。這意味著岭洲,鍵盤(pán)、鼠標(biāo)坎匿、會(huì)話盾剩、進(jìn)程、I/O操作等替蔬,無(wú)論是軟件還是硬件都被描述為一個(gè)文件存放于文件系統(tǒng)中告私,你可以像操作普通文件一樣操作它們。Bash包含了許多配置文件承桥,你可以通過(guò)修改這些配置文件對(duì)Bash做些定制驻粟,Bash配置文件包含以下內(nèi)容:
FILE LOCATION | EXPLANATION |
---|---|
/etc/profile | Executes many startup shell scripts that are located both in /etc/profile.d/ and other paths. This file is system-wide file, you better not change stuff here. |
~/.bashrc | Commands that are executed once you enter the system. Usually, most people do modify this file according to their needs. |
~/.bash_logout | Shell commands that are executed when you exit the system. |
~/.bash_history | All the commands that you execute in your shell are saved in this file, it's like a log file for the commands that you write (You can avoid saving a command here by simply putting a whitespace (or multiple ones) before the command you are entering). |
通常我們執(zhí)行命令:ls -Ali
來(lái)列出當(dāng)前目錄中的文件信息,但每次執(zhí)行都需要輸入選項(xiàng)-Ali
凶异,有些繁瑣蜀撑。我們可以修改bashrc來(lái)達(dá)到簡(jiǎn)化的目的:
使用vim打開(kāi)文件:vim ~./bashrc
,在文件中輸入alias la='ls -Ali'
唠帝,然后執(zhí)行source ~/.bashrc
讓修改立即生效即可:
然后在命令行中輸入:la ~ ~/code
可以看到列出了家目錄及家目錄下code文件中的文件信息:
?? 如果直接執(zhí)行
alias la='ls -Ali'
屯掖,那么在終端關(guān)閉后玄柏,la命令也不復(fù)存在
Bash Scripting基礎(chǔ)
Bash腳本通常帶有后綴sh(不帶也行)襟衰,寫(xiě)一段腳本如下:
vim demo.sh
# 這里是注釋
la # 列出當(dāng)前目錄中的文件信息
然后使用source
命令讀取腳本中的命令并執(zhí)行:source demo.sh
。source
命令的簡(jiǎn)寫(xiě)形式為半角句號(hào):.
即命令source demo.sh
等價(jià)于. demo.sh
粪摘。
資料推薦
The Linux Command Line: A Complete Introduction: A famous book to start learning the topic. Made in 544 pages that would explain everything you need about writing Bash commands and scripts. Very recommended to read. (It’s available PDF free from the website).
Linux Command Line Tutorial for Beginners: If you are someone who prefers video content over written one, then this Youtube set of videos is for you. Made in 80 different videos averaging around 10 minutes each, this series takes your hand in explaining various Linux commands beside more advanced topics in writing shell scripts.
ExplainShell.com: Here, let’s say that you encountered a very long command while browsing an online article or a book, and you didn’t know what does it do and how? Just paste it into the website and it will tell you what each part of it does. It’s amazing online website to explain Linux commands.
LearnShell.org: A free online interactive website to learn the shell. You basically write everything and learn everything from inside your browser.