Linux命令行基礎

本文內容根據Linux Command Line Basics & Excaples進行改編php

Linux命令行簡要介紹

AT&T公司於20世紀70年代發佈了UNIX系統。通過多年的發展,Unix再也不是某一個具體操做系統的名稱,而是對遵循Unix規範、設計和哲學的一類操做系統的統稱。還有一些操做系統,它們遵循Unix設計、有着與Unix相似的規範和標準,這些操做系統被稱爲類Unix系統(Unix-like),Linux就是其中的一員。linux

在設計上Unix包含一個Unix Shell。它是一種命令行解釋器(CLI)或者Shell,可讓用戶經過輸入命令與系統交互。Unix Shell既能夠直接執行用戶輸入的命令,也能夠從文件中讀取命令執行(shell scripting)。最經常使用的Unix Shell是Bash,幾乎全部的Linux發行版中都內置有Bash。一般所說的Linux命令行就是Bash命令或Bash腳本。nginx

Linux命令行以強大靈活著稱,使用少數命令就能夠執行許多任務,還能夠將許多任務自動化。web

Linux命令行基礎

Linux啓動後,就會建立一個shell會話(shell session)。shell session是一個基礎環境,它容許系統與應用、及應用間進行通信。能夠一次打開多個會話,會話間能夠存在父子關係,若是當前會話的父會話被關閉,當前會話也會被終止。thinkphp

 

 

上圖是VSCode遠程開發模式下,鏈接到Windows10 WSL(Ubuntu18.04.2)的截圖。光標前面的內容格式以下:shell

username@hostname:locate,對應到上圖,即當前會話的用戶名爲wjchi,hostname是DESKTOP-J2OGSVG,當前目錄爲~,即當前用戶的家目錄:/home/wjchi。vim

下面是一些Linux經常使用符號的含義:bash

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來查看各個命令的說明,man是manual的簡寫。在命令行輸入:man man,輸出以下:session

 

 

Linux中經常使用導航命令以下:less

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設計哲學中包含一條準則:一切皆文件,everything is a file。這意味着,鍵盤、鼠標、會話、進程、I/O操做等,不管是軟件仍是硬件都被描述爲一個文件存放於文件系統中,你能夠像操做普通文件同樣操做它們。Bash包含了許多配置文件,你能夠經過修改這些配置文件對Bash作些定製,Bash配置文件包含如下內容:

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

一般咱們執行命令:ls -Ali來列出當前目錄中的文件信息,但每次執行都須要輸入選項-Ali,有些繁瑣。咱們能夠修改bashrc來達到簡化的目的。

使用vim打開文件:vim ~./bashrc,在文件中輸入alias la='ls -Ali',而後執行source ~/.bashrc讓修改當即生效便可:

而後在命令行中輸入:la ~ ~/code能夠看到列出了家目錄及家目錄下code文件中的文件信息:

 

 

⚠️ 若是直接執行alias la='ls -Ali',那麼在終端關閉後,la命令也不復存在

Bash Scripting基礎

Bash腳本一般帶有後綴sh(不帶也行),寫一段腳本以下:

vim demo.sh

 

# 這裏是註釋
la # 列出當前目錄中的文件信息

而後使用source命令讀取腳本中的命令並執行:

source demo.sh

source命令的簡寫形式爲半角句號:.

即命令source demo.sh等價於. 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中爲何執行本身的程序要在前面加./

相關文章
相關標籤/搜索