Linux命令行基础

Linux命令行基础

本文内容根据Linux Command Line Basics & Excaples进行改编

Linux命令行简要介绍

AT&T公司于20世纪70年代发布了UNIX系统。经过多年的发展,Unix不再是某一个具体操作系统的名称,而是对遵循Unix规范、设计和哲学的一类操作系统的统称。还有一些操作系统,它们遵循Unix设计、有着与Unix类似的规范和标准,这些操作系统被称为类Unix系统(Unix-like),Linux就是其中的一员。

在设计上Unix包含一个Unix Shell。它是一种命令行解释器(CLI)或者Shell,可以让用户通过输入命令与系统交互。Unix Shell既可以直接执行用户输入的命令,也可以从文件中读取命令执行(shell scripting)。最常用的Unix Shell是Bash,几乎所有的Linux发行版中都内置有Bash。通常所说的Linux命令行就是Bash命令或Bash脚本。

Linux命令行以强大灵活著称,使用少数命令就可以执行许多任务,还可以将许多任务自动化。

Linux命令行基础

Linux启动后,就会创建一个shell会话(shell session)。shell session是一个基础环境,它允许系统与应用、及应用间进行通讯。可以一次打开多个会话,会话间可以存在父子关系,如果当前会话的父会话被关闭,当前会话也会被终止。

上图是VSCode远程开发模式下,连接到Windows10 WSL(Ubuntu18.04.2)的截图。光标前面的内容格式如下:

username@hostname:locate,对应到上图,即当前会话的用户名为wjchi,hostname是DESKTOP-J2OGSVG,当前目录为~,即当前用户的家目录:/home/wjchi。

下面是一些Linux常用符号的含义:

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,输出如下:

Linux中常用导航命令如下:

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.shsource命令的简写形式为半角句号:.

即命令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中为什么执行自己的程序要在前面加./

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,513评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,312评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,124评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,529评论 0 217
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,937评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,913评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,084评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,816评论 0 205
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,593评论 1 249
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,788评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,267评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,601评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,265评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,158评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,953评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,066评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,852评论 2 277

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,314评论 0 5
  • 操作系统小知识 现代操作系统通常都有一个使用绘图设备的图形用户界面(GUI),并附加如鼠标或触控版等有别于减半的输...
    Jingle_hunger阅读 631评论 1 0
  • 初学到此,写文记录 一些概念的个人理解 linux:一种操作系统 图形界面:不用代码操作,图形化的操作界面 命令行...
    张路1806阅读 212评论 0 0
  • linux命令行基础 作为一个前端工程师会基本的命令行是必备的要求 名词 [图形界面] [命令行] [终端] [s...
    一条没有梦想的老咸鱼阅读 159评论 0 3
  • 自古以来,在人类的历史上战争从未停止过。乔治-布什认为战争的源头在于伊拉克、朝鲜,而我认为真正的战争在于餐桌,在人...
    莲花释界阅读 235评论 0 0