Linux Shell 常用命令 – 01篇

系列文章:
Linux Shell 常用命令 – 02篇

0. 在线使用 Linux Shell

参考 https://www.sohu.com/a/343421845_298038

JS/UIX – Terminal

地址:https://www.masswerk.at/jsuix/index.html

进入后,点击open terminal即可。它提供简单的终端环境,没有自带gcc等编译套件,不过练习基本命令和shell脚本还是可以的。

1. 常用命令

1.1 ls 命令 – 显示指定工作目录下的内容

详见 https://www.runoob.com/linux/linux-comm-ls.html

显示指定工作目录的内容(列出当前工作目录中包含的文件和子目录)

[En]

Display the contents of the specified working directory (list the files and subdirectories contained in the current working directory)

语法:

ls [-alrtAFR] [name...]

参数:
-a: 显示所有文件及目录 (.开头的隐藏文件也会列出)
-l: 除文件名称外,亦将文件型态、权限、拥有者、文件大小等资讯详细列出

ls -l  可写作 ll

常用写法:
ll
ls -al

Linux Shell 常用命令 - 01篇

结合 grep 使用:(关于 grep 的详细介绍见后文)

查看文件 log.txt 是否存在于当前目录下:

>ls | grep log.txt
log.txt
>ls -al | grep log.txt
-rw-rw-r--.  1 mozhiyan mozhiyan    0 4月  15 17:26 log.txt

1.2 查看文件内容:cat

Linux Shell 常用命令 - 01篇

1.3 创建文件: touch

Linux Shell 常用命令 - 01篇

此时,若想往text2中写入内容,可用 echo "hello word">>text2

Linux Shell 常用命令 - 01篇

>>代表在末尾追加,若使用 >则会把之前的内容覆盖:

Linux Shell 常用命令 - 01篇

当然,也可直接使用 vim text2对文件进行编辑

注:无论是 echo "hello">file 还是 echo "hello">>file ,如果file不存在都会先创建file文件

类似的用法有: cat ./result.lst>>aa.txt(将result.lst文件中的内容追加到aa.txt的末尾)

1.4 mv – 为文件或目录改名、或移入其它位置

详见 https://www.runoob.com/linux/linux-comm-mv.html

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇
👆注:若test目录不存在,则该命令将text4重命名为test

Linux Shell 常用命令 - 01篇

1.5 rm – 删除一个文件或者目录

详见 https://www.runoob.com/linux/linux-comm-rm.html

删除文件: rm 文件名

删除文件夹: rm -r 文件夹名-r 将目录及以下之档案亦逐一删除

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

-f 直接删除,无需逐一确认,见如下示例:

Linux Shell 常用命令 - 01篇

删除当前目录中的所有文件和目录:

[En]

Delete all files and directories in the current directory:

rm -r *

文件一旦通过 rm命令删除,则无法恢复,所以须格外小心地使用该命令。

1.6 cp – 复制文件或文件夹

语法:

cp [options] source dest

参数:
-a:此选项通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容。其作用等于dpR参数组合。
-d:复制时保留链接。这里所说的链接相当于 Windows 系统中的快捷方式。
-f:覆盖已经存在的目标文件而不给出提示。
-i:与 -f 选项相反,在覆盖目标文件之前给出提示,要求用户确认是否覆盖,回答 y 时目标文件将被覆盖。
-p:除复制文件的内容外,还把修改时间和访问权限也复制到新文件中。
-r:若给出的源文件是一个目录文件,此时将复制该目录下所有的子目录和文件。
-l:不复制文件,只是生成链接文件。

例如,将当前路径下的a.txt复制到另外一个文件夹下:

>cp a.txt /data/test/

将文件夹xgb_pipeline复制到另一个路径下:

>cp -r xgb_pipeline /data/test/

将当前路径下的多个文件复制到另一个路径:

[En]

Copy multiple files under the current path to another path:

>cp a.txt b.json /data/test/

2. 压缩打包命令 – tar、zip、unzip

来自 https://blog.csdn.net/weixin_44901564/article/details/99682926

打包与压缩的区别:

打包是指将多个文件或目录放在一起形成一个总的包,便于保存和传输,但大小不变。压缩是指通过一种压缩算法对一个或多个大文件或目录进行压缩,以减小文件大小,从而达到压缩的目的,从而可以节省存储空间。

[En]

Packaging refers to putting multiple files or directories together to form a total package, which is easy to save and transfer, but the size does not change. Compression refers to the compression of one or more large files or directories through a compression algorithm to reduce the size of files to achieve the purpose of compression, which can save storage space.

tar命令可以进行打包或压缩、解压,zip与unzip主要进行压缩与解压。(zip命令主要用于windows 系统下,例如在Linux服务器上可用zip进行压缩,下载到本地windows系统,进行解压更加方便)

2.1 tar命令

tar命令常用参数

-z 是否同时具有gz属性 -j 是否同时具有bz2属性 -J 是否同时具有xz属性 -x 解压缩、提取打包的内容 -t 查看压缩包内容 -c 建立一个压缩,打包文档 -C 切换到指定目录,表示指定解压缩包的内容和打包的内容存放的目录 -v 显示压缩或者打包的内容 -f 使用文件名,在f后面要接压缩后的文件的名字,只要用到tar命令,-f选项是必须要用的,-f参数在使用的时候一定排在其他参数的后面,在最右边 -p 保留备份数据的原本权限与属性,常用于备份(-c)重要的配置文件 -P(大写P) 保留绝对路径

一、打包

Linux Shell 常用命令 - 01篇
二、压缩

Linux主要有三种压缩方式:

  1. gzip:是公认的压缩速度最快,压缩大文件的时候与其他的压缩方式相比更加明显,历史最久,应用最广泛的压缩方式
  2. bzip:压缩形成的文件小,但是可用性不如gzip
  3. xz:是最新的压缩方式,可以自动提供最佳的压缩率

压缩时建议注明后缀:

[En]

It is recommended to indicate the suffix when compressing:

参数 作用 命名方式 -z 用于gzip压缩方式 文件名.tar.gz -j 用于bzip2压缩方式 文件名.tar.bz2 -J 用于xz压缩方式 文件名.tar.xz

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇
三、解压

Linux Shell 常用命令 - 01篇

tar命令在解压时不需要指明压缩的方式,它会自己选择跟压缩方式对应的方式去解压

Linux Shell 常用命令 - 01篇

👆原文中未给出命令,猜测为 tar -xf Golden.apk.tar.gz

Linux Shell 常用命令 - 01篇

补充

a.在打包和压缩的过程中,我们有时会看到这样的语句: tar: 从成员名中删除开头的”/”,这个并不是报错,是因为没有加上-P选项,没有保留原来的绝对路径去打包或者压缩,提取打包的内容跟解压一样,下面举一个例子:

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

解压的时候同理,如果在压缩文件的时候使用了-P选项,那么在解压时也要加上-P选项,不然也会出现 tar: 从成员名中删除开头的”/”

b.在使用tar压缩或者打包的时候,可以通过增加 --exclude 来达到排除指定的文件的目的

Linux Shell 常用命令 - 01篇
压缩文件同理。

c. 使用 -r 选项增加.tar归档文件的内容

通过 tar --help 命令查看帮助

-r 选项的作用:把要存档的文件追加到档案文件的未尾。例如用户已经作好备份文件,又发现还有一个目录或是一些文件忘记备份了,这时可以使用该选项,将忘记的目录或文件追加到备份文件中

Linux Shell 常用命令 - 01篇
使用实例:
Linux Shell 常用命令 - 01篇

(先将1进行归档,再追加2)

1.归档操作

Linux Shell 常用命令 - 01篇
生成.tar文件,目录归档完毕

2.将calculating_time.sh追加进入.tar文件中

Linux Shell 常用命令 - 01篇

查看.tar文件的归档信息:

Linux Shell 常用命令 - 01篇

可以看到calculating_time.sh文件已经被追加到档案的末尾了。

2.2 zip命令和unzip命令

在使用 zip 跟 unzip 命令之前先查看系统有没有安装这两个命令的包,没有的话要自己安装。
查看有没有安装zip跟unzip命令的命令:

rpm -q zip unzip

如果已安装,则会显示命令的版本号

[En]

If installed, the version number of the command is displayed

Linux Shell 常用命令 - 01篇
一、zip命令

基本用法:

zip [参数] [压缩包名] [压缩的目录或者文件的路径]

示例:

>zip -r xgb_pipeline.zip xgb_pipeline

zip命令常用参数

-m 将文件压缩后,删除原文件 -o 将压缩文件内的所有文件的最新变动时间设为压缩的时间 -q 安静模式,在压缩的时候不显示指令执行的过程 -r 递归压缩,将自定目录下的所有子文件以及文件一起处理 -x “文件列表”,压缩时排除文件列表中的文件

实例

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇
二、unzip命令

直接使用:

unzip xxx.zip

便可解压到当前目录下。

基本用法:

unzip [参数] [压缩文件]  (-d [目录])  //如果不使用括号内的内容,则解压文件到当前工作目录

unzip命令常用参数

-c 将解压缩的结果显示到屏幕上(显示每一个目录下的每一个文件的内容),同时对字符做适当的转换,但是并没有解压压缩包 -l 显示压缩文件内所包含的文件 -t 检查压缩文件是否正确 -v 执行时显示压缩文件的详细信息 -q 安静模式,执行时不显示任何信息 -d 指定文件解压后存储的目录 -x 指定不要处理压缩文件中的那些文件

实例

a.将harry.zip压缩包解压缩的结果显示出来:

Linux Shell 常用命令 - 01篇

将显示每个文件的详细信息和每个文件的内容。

[En]

Details will be displayed to each file and the contents of each file.

b.显示jihe.zip压缩包里面所包含的文件

Linux Shell 常用命令 - 01篇

c.检查dajihe.zip压缩文件是否正确

Linux Shell 常用命令 - 01篇

全部OK表示全部文件都是正确的

d.不解压压缩文件dajihe.zip,查看压缩包里面的内容(查看显示的文件列表还包含压缩比率)

Linux Shell 常用命令 - 01篇

使用 -v显示的信息比使用 -l显示的信息更加详细

e.将dajihe.zip压缩包解压到/opt/目录下:

Linux Shell 常用命令 - 01篇

注:

unzip -l dajihe.zip
unzip -v dajihe.zip
unzip -c dajihe.zip
都不会解压,只是查看
unzip dajihe.zip会解压

3.Linux属主权限,chgrp,chown,chmod

来自 https://www.cnblogs.com/CYHISTW/p/11031429.html

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

【示例】可使用 chmod +x 文件名 使文件变为可执行文件,例如:

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

👆 输入 i进行编辑模式,完成后,按 esc再输入 :wq 保存并退出

Linux Shell 常用命令 - 01篇

👆 可以看到此时run.sh处于不可执行状态

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

成功执行

4. shell 命令行参数

参考
http://c.biancheng.net/cpp/view/2739.html
http://blog.csdn.net/jake_tian/article/details/97274630

运行脚本时传递给脚本的参数称为命令行参数。

[En]

The parameters passed to the script when you run the script are called command line parameters.

变量 含义 $0 当前脚本的文件名 $n | 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,获取第一个参数是$1,获取第二个参数是$2,以此类推 $# 传递给脚本或函数的参数个数 $ 传递给脚本或函数的所有参数。 $@ | 传递给脚本或函数的所有参数。被双引号(” “)包含时,与 $ 稍有不同 $? 上个命令的退出状态,或函数的返回值。

例:脚本如下:

#!/bin/bash
echo "File Name: $0"
echo "First Parameter : $1"
echo "First Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

运行:

$./test.sh Zara Ali
File Name : ./test.sh
First Parameter : Zara
Second Parameter : Ali
Quoted Values: Zara Ali
Quoted Values: Zara Ali
Total Number of Parameters : 2

关于 $?:上条命令的返回值。0表示没有错误,其他任何数值表示有错误。

参考
https://blog.csdn.net/aLLLiyyy/article/details/103184367
https://blog.csdn.net/weixin_39928768/article/details/111582889

例如:如果上条命令执行失败,则重复尝试3次,shell脚本如下:

cnt=0
python test.py

while(($?==1 && cnt

5. shell 变量

参考
runoob.com/linux/linux-shell-variable.html
http://c.biancheng.net/view/743.html

定义变量时,变量名不加 $,如:

a=10

注意:变量名和等号之间不能有空格

[En]

Note: there can be no space between the variable name and the equal sign

使用一个定义过的变量时,在变量名前加 $,如:

a=10
echo $a
echo ${a}

变量名外的花括号是可选的,添加花括号是为了帮助解释器识别变量的边界。

[En]

Curly braces outside the variable name are optional, and curly braces are added to help the interpreter identify the boundaries of the variable.

可以重新定义的已定义变量:

[En]

Defined variables that can be redefined:

a=10
echo $a
a=20
echo $a

注:第二次赋值的时候不能写成 $a=20,使用变量时才加 $

将命令的结果赋给变量:

[En]

Assign the result of the command to the variable:

两种方式:

variable=command
variable=$(command)

第一种方法是用反引号将命令括起来,这与单引号非常相似,容易混淆,因此不推荐使用这种方法;第二种方法是命令用$()括起来,区别更明显。建议您使用这种方式。

[En]

The first way is to surround the command in backquotation marks, which is very similar to the single quotation mark, so it is easy to be confused, so this method is not recommended; in the second way, the command is surrounded by $(), and the distinction is more obvious. This way is recommended.

例:

Linux Shell 常用命令 - 01篇

6. shell 字符串

6.1 单引号

str='this is a string'

单引号字符串的限制:

  • 单引号中的任何字符都将按原样输出,单引号字符串中的变量无效
    [En]

    any character in the single quote will be output as is, and the variable in the single quote string is invalid*

  • 单引号不能出现在单引号字符串中(即使在转义后也不能),但它可以成对出现,并可以用作字符串连接。
    [En]

    A single quote cannot appear in a single quote string (not even after escape), but it can appear in pairs and can be used as a string concatenation.*

例:

your_name='runoob'
str='Hello, I know you are $your_name'
echo $str

运行结果:
Hello, I know you are $yourname

6.2 双引号

your_name="runoob"
str="Hello, I know you are \"$your_name\"!"
echo $str

运行结果:
Hello, I know you are "runoob"!

双引号的优点:

  • 双引号里可以有变量
  • 转义字符可以用双引号引起来
    [En]

    escape characters can appear in double quotation marks*

7. shell 基本运算符

参考 runoob.com/linux/linux-shell-basic-operators.html

原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如expr。expr是一款表达式计算工具,使用它能完成表达式的求值操作。

例如,两个数相加:

#!/bin/bash

val=expr 2 + 2  # 或写为 val=$(expr 2 + 2)
echo "两数之和为: $val"

结果
两数之和为: 4

👆 注:表达式和运算符之间要有空格,例如 2+2 是不对的,必须写成 2 + 2

7.1 算数运算符

Linux Shell 常用命令 - 01篇

注:条件表达式要放在方括号之间,并且要有空格,例如: [$a==$b] 是错误的,必须写成 [ $a == $b ]。(方括号之间要有空格,运算符和变量之间也要有空格)

Linux Shell 常用命令 - 01篇

7.2 关系运算符

除非字符串的值是数字,否则关系运算符仅支持数字,而不支持字符串。

[En]

Relational operators only support numbers, not strings, unless the value of the string is a number.

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

7.3 布尔运算符

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

7.4 逻辑运算符

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

7.5 字符串运算符

Linux Shell 常用命令 - 01篇

Linux Shell 常用命令 - 01篇

Original: https://www.cnblogs.com/qypx/p/16155506.html
Author: qypx
Title: Linux Shell 常用命令 – 01篇

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523780/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球