Linux命令行基礎(chǔ)

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.shsource命令的簡(jiǎn)寫(xiě)形式為半角句號(hào):.

即命令source demo.sh等價(jià)于. demo.sh粪摘。

資料推薦

  1. 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).

  2. 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.

  3. 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.

  4. LearnShell.org: A free online interactive website to learn the shell. You basically write everything and learn everything from inside your browser.

推薦閱讀

The Linux Command Line 中文版
Linux中為什么執(zhí)行自己的程序要在前面加./

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瀑晒,一起剝皮案震驚了整個(gè)濱河市绍坝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌苔悦,老刑警劉巖轩褐,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異玖详,居然都是意外死亡把介,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén)蟋座,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)拗踢,“玉大人,你說(shuō)我怎么就攤上這事向臀〕彩” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵券膀,是天一觀的道長(zhǎng)君纫。 經(jīng)常有香客問(wèn)我,道長(zhǎng)芹彬,這世上最難降的妖魔是什么蓄髓? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮舒帮,結(jié)果婚禮上双吆,老公的妹妹穿的比我還像新娘。我一直安慰自己会前,他們只是感情好好乐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著瓦宜,像睡著了一般蔚万。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上临庇,一...
    開(kāi)封第一講書(shū)人閱讀 51,301評(píng)論 1 301
  • 那天反璃,我揣著相機(jī)與錄音,去河邊找鬼假夺。 笑死淮蜈,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的已卷。 我是一名探鬼主播梧田,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了裁眯?” 一聲冷哼從身側(cè)響起鹉梨,我...
    開(kāi)封第一講書(shū)人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎穿稳,沒(méi)想到半個(gè)月后存皂,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡逢艘,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年旦袋,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片它改。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡猜憎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出搔课,到底是詐尸還是另有隱情胰柑,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布爬泥,位于F島的核電站柬讨,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏袍啡。R本人自食惡果不足惜踩官,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望境输。 院中可真熱鬧蔗牡,春花似錦、人聲如沸嗅剖。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)信粮。三九已至黔攒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間强缘,已是汗流浹背督惰。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旅掂,地道東北人赏胚。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像商虐,于是被迫代替她去往敵國(guó)和親觉阅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子崖疤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,381評(píng)論 0 5
  • 操作系統(tǒng)小知識(shí) 現(xiàn)代操作系統(tǒng)通常都有一個(gè)使用繪圖設(shè)備的圖形用戶(hù)界面(GUI),并附加如鼠標(biāo)或觸控版等有別于減半的輸...
    Jingle_hunger閱讀 647評(píng)論 1 0
  • 初學(xué)到此留拾,寫(xiě)文記錄 一些概念的個(gè)人理解 linux:一種操作系統(tǒng) 圖形界面:不用代碼操作,圖形化的操作界面 命令行...
    張路1806閱讀 224評(píng)論 0 0
  • linux命令行基礎(chǔ) 作為一個(gè)前端工程師會(huì)基本的命令行是必備的要求 名詞 [圖形界面] [命令行] [終端] [s...
  • 自古以來(lái),在人類(lèi)的歷史上戰(zhàn)爭(zhēng)從未停止過(guò)疫向。喬治-布什認(rèn)為戰(zhàn)爭(zhēng)的源頭在于伊拉克咳蔚、朝鮮,而我認(rèn)為真正的戰(zhàn)爭(zhēng)在于餐桌搔驼,在人...
    蓮花釋界閱讀 266評(píng)論 0 0