Linux常用指令和选项

阅读: 评论:0

Linux常用指令和选项

Linux常用指令和选项

目录

  • 🌈前言
    • 🚁ls指令
    • 🚂pwd指令
    • 🚃cd指令
    • 🚄touch指令
    • 🚅mkdir指令(important)
    • 🚆rmdir指令 && rm指令(important)
    • 🚇man指令(important)
    • 🚈cp指令(important)
    • 🚉mv指令(剪切/重命名)(important)
    • 🚐cat指令
    • 🚑more指令
    • 🚒less指令(important)
    • 🚠输入/输出/追加重定向(</>/>>)
    • 🚠管道
    • 🚓head指令
    • 🚕tail指令
    • 🚖时间相关指令
      • 🚗date指令
    • 🚘cal指令
    • 🚙find指令:(very important) -name
    • 🚡grep指令
    • 🚢zip/unzip指令
    • 🚣tar指令(重要):打包/解包,不打开它,直接看内容
    • 🚤bc指令(浮点数运算)
    • 🚥uname –r指令
    • 🚦重要的几个热键[Tab],[ctrl]-c, [ctrl]-d

🌈前言

本篇文章进行Linux指令的学习!!!


🚁ls指令

语法:ls [选项][目录或文件]
功能:对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息

常用选项:

  • -a :列出目录下的所有文件,包括以 . (点)开头的隐藏文件
  • -d:将目录象文件一样显示,而不是显示其下的文件。 如: ls –d 指定目录
  • -l:列出文件的详细信息,包含文件权限和属性,所占byte,文件拥有者和所属组的人是谁,日期时间,还有文件名
  • -i:输出文件的 i 节点的索引信息。 如 ls –ai 指定文件
  • -k:以 k 字节的形式表示文件的大小。 ls –alk 指定文件
  • -n:文件拥有者和所属组用数字的 UID,GID 代替他们的名称
  • -r:对目录进行反向排序
  • -t:对目录进行时间排序
  • -R:列出所有子目录下的文件
  • -1:一行只输出一个文件
  • -F:在每个文件名后附上一个字符以说明该文件的类型, “ * ”表示可执行的普通文件; “ / ”表示目录; “ @ ”表示符号链接; “|”表示FIFOs; “ = ”表示套接字(sockets)。(目录类型识别)

演示几个常用的:

[@localhost 2022_9_7]$ ls -a
.  ..  makefile  test  test.cpp
[@localhost 2022_9_7]$ ls -l
总用量 28
-rw-rw-r--. 1 lyh_sky lyh_sky    73 9月   9 11:07 makefile
-rwxrwxr-x. 1 lyh_sky lyh_sky 19672 9月   9 11:07 test
-rw-rw-r--. 1 lyh_sky lyh_sky   185 9月   9 11:07 test.cpp
[@localhost 2022_9_7]$ ls -al
总用量 28
drwxrwxr-x. 2 lyh_sky lyh_sky    50 9月   9 11:07 .
drwxrwxr-x. 6 lyh_sky lyh_sky    79 9月   7 23:27 ..
-rw-rw-r--. 1 lyh_sky lyh_sky    73 9月   9 11:07 makefile
-rwxrwxr-x. 1 lyh_sky lyh_sky 19672 9月   9 11:07 test
-rw-rw-r--. 1 lyh_sky lyh_sky   185 9月   9 11:07 test.cpp

注意:" 点 "和 " 点点 " 隐藏文件是保存当前路径和上一个路径的文件


如果我们创建一个文件并且不写入数据,在磁盘中占据空间吗???

答案是:是的!磁盘需要保存文件属性的数据,文件 = 属性 + 数据
注意:属性是文件创建时的日期时间,文件名等等…


🚂pwd指令

语法:pwd
功能:显示当前用户所在的路径
常用选项:无

[@localhost ~]$ pwd // 显示当前工作目录下的路径
/home/lyh_sky

🚃cd指令

  • 在Linux系统中,磁盘上的文件和目录被组成一棵目录树(多叉树),每个节点都是目录或文件,树中的每个节点j既可以是一个目录,也可以是一个文件

  • 目录树(多叉树)的子节点一定是一个普通文件或空目录

    注意:/是主目录,相当于我们windows里面的图形化界面


语法:cd 目录名
功能:改变工作目录,将当前工作目录改变到指定的目录下
常用选项:无

cd .. : 返回上级目录
cd /home/litao/linux/ : 绝对路径
cd ../day02/ : 相对路径
cd ~:进入用户家目
cd -:返回最近(上一次)访问的目录
  • 相对路径:相对于我们当前所处的路径,点点/开始就是相对路径
  • 绝对路径:./开始就是绝对路径,意思是唯一/绝对的路径

🚄touch指令

语法:touch [选项] [文件名]

功能: 它可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件

常用选项

  • -d: 使用指定的日期时间,而非现在的时间 – 格式:touch -d 小时:分钟
  • -m:只更改变动时间,更改当前系统的时间(北京时间) – touch -m file
  • -t:使用指定的日期时间,而非现在的时间 – touch -t ‘202209091200’ file
  • -r:把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同 – touch -r file1 file2
[@localhost test]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 2月   3 2022 test
[@localhost test]$ touch -d 5:20 test // 指定修改时间
[@localhost test]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月   9 05:20 test[@localhost test]$ touch -m test // 默认设置日期时间为系统时间
[@localhost test]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月   9 23:13 test[@localhost test]$ touch -t '202210011200' test // 更改日期时间为2022年10月1日12:00
[@localhost test]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 10月  1 2022 test[@localhost test]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 10月  1 2022 test
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月   9 23:15 test2
[@localhost test]$ touch -r test2 test //将test的日期时间更改为test2的日期时间
[@localhost test]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月   9 23:15 test
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月   9 23:15 test2

注意:一般直接使用创建文件,或者直接vim创建文件


🚅mkdir指令(important)

语法: mkdir [选项] [目录名]

功能:在当前工作目录下创建一个空目录

常用选项:

  • -p, --parents:可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录
[@localhost test]$ mkdir dir // 创建空目录
[@localhost test]$ ll
drwxrwxr-x. 2 lyh_sky lyh_sky 6 9月   9 23:26 dir[lyh_sky@localhost test]$ cd dir // 进入dir目录
[lyh_sky@localhost dir]$ pwd 		// 显示当前路径
/home/lyh_sky/test/dir
[lyh_sky@localhost dir]$ mkdir -p dir1 dir2 dir3 // 在当前目录递归创建三个目录
[lyh_sky@localhost dir]$ tree . // 以树形结构显示当前目录下的结构
. // 点是dir目录的路径
├── dir1
├── dir2
└── dir33 directories, 0 files //三个目录。0个文件

🚆rmdir指令 && rm指令(important)

rmdir是一个与mkdir相对应的命令。 mkdir是建立目录,而rmdir是删除命令

语法:rmdir [-p] [目录名]
适用对象:具有当前目录操作权限的所有使用者
功能:删除空目录

  • -p: 当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除
[@localhost test]$ tree dir
dir
├── dir1
├── dir2
└── dir3
[@localhost test]$ rmdir -p dir
rmdir: 删除 "dir" 失败: 目录非空[@localhost test]$ ll
drwxrwxr-x. 2 lyh_sky lyh_sky 6 9月   9 23:46 dir1
[@localhost test]$ tree dir1
dir1 // dir1目录为空目录0 directories, 0 files
[@localhost test]$ rmdir -p dir1
[@localhost test]$ ll

注意:rmdir只能删除空目录,不能删除文件


rm指令可以同时删除目录和文件

语法: rm [选项][目录/文件名]
适用对象:适合所有操作者
功能:可以删除目录和文件

常用选项

  • -f:即使文件属性为只读(即写保护),亦直接删除
  • -r:删除目录及其下所有文件
  • -i:删除前逐一询问确认
[@localhost ~]$ ls -al test/
总用量 4
drwxrwxr-x.  4 lyh_sky lyh_sky   30 9月   9 23:54 .
drwx------. 22 lyh_sky lyh_sky 4096 9月   9 22:57 ..
drwxrwxr-x.  2 lyh_sky lyh_sky   18 9月   9 23:54 dir1
drwxrwxr-x.  2 lyh_sky lyh_sky   18 9月   9 23:55 dir2
[@localhost ~]$ tree test/
test/
├── dir1
│   └── test
└── dir2└── test2 directories, 2 files // 二个目录,二个文件[@localhost ~]$ rm -rf test		// 一般这两个选项一起用
[lyh_sky@localhost ~]$ ls -al test
ls: 无法访问test/: 没有那个文件或目录

注意:在Linux系统下不要随便删文件,因为他不像windows一样有回收站


🚇man指令(important)

Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助

我们可以使用man来进行查询:man [选项] 命令
常用选项

  • -k 根据关键字搜索联机帮助
  • num 只在第num章节找 – 例如:man 3 mkdir
  • -a 将所有章节的都显示出来,比如 man printf 它缺省从第一章开始搜索,知道就停止,用a选项,当按下q退出,他会继续往后面搜索,直到所有章节都搜索完毕

解释一下,面手册分为8章:

  1. 第1章是普通的命令

  2. 第2章是是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件,还有就是怎么使用它)

  3. 第三章是库函数,如printf,fread4是特殊文件,也就是/dev下的各种设备文件

  4. 第五章是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义

  5. 第六章是给游戏留的,由各个游戏自己定义

  6. 第七章是附件还有一些变量,比如向environ这种全局变量在这里就有说明

  7. 第八章是系统管理用的命令,这些命令只能由root使用,如ifconfig


🚈cp指令(important)

语法: cp [选项] [源文件或目录] [目标文件或目录]
功能: 复制文件或目录

说明:

  • cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中

  • 若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息

  • 如果cp拷贝文件到当前路径下,二个文件的名字不能一样

  • cp默认不能拷贝目录,可以强制-f或-r进行目录递归拷贝

常用功能选项

  • -f 或 --force:强行复制文件或目录, 不论目的文件或目录是否已经存在
  • -i 或 --interactive:覆盖文件之前先询问用户
  • -R 或 --recursive:递归处理,将指定目录下的文件及子目录一并处理
  • -r:递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
// 拷贝文件
[@localhost test1]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月  10 11:19 file
drwxrwxr-x. 2 lyh_sky lyh_sky 6 9月  10 11:18 test
[@localhost test1]$ cp ./file ./test/
[@localhost test1]$ ll ./test/
-rw-rw-r--. 1 lyh_sky lyh_sky 0 9月  10 11:19 file// 使用-r选项递归拷贝目录
[lyh_sky@localhost test1]$ ll
drwxrwxr-x. 2 lyh_sky lyh_sky 57 9月  10 11:22 test
drwxrwxr-x. 2 lyh_sky lyh_sky  6 9月  10 11:22 test2
[lyh_sky@localhost test1]$ tree test
test
├── file
├── file1
├── file2
└── file30 directories, 4 files
[lyh_sky@localhost test1]$ cp -r ./test ./test2
[lyh_sky@localhost test1]$ tree test2
test2
└── test├── file├── file1├── file2└── file31 directory, 4 files

🚉mv指令(剪切/重命名)(important)

说明:mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录

语法:mv [选项] [源文件或目录] [目标文件或目录]

功能

  • 视mv命令第二个参数类型(目标文件或目录)的不同,mv将文件进程重命名或移动到新的目录中

  • 当第二和第三个参数同时是文件或目录时,mv命令将完成重命名

  • 当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个, mv命令将各参数指定的源文件均移至目标目录中

常用选项:

  • -f : force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖

  • -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖

[@localhost test1]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky  0 9月  10 11:40 file
drwxrwxr-x. 3 lyh_sky lyh_sky 18 9月  10 11:23 test[@localhost test1]$ mv file file2 // 将file文件重命名为file2
[@localhost test1]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky  0 9月  10 11:40 file2
drwxrwxr-x. 3 lyh_sky lyh_sky 18 9月  10 11:23 test[@localhost test1]$ mv file2 test/
[@localhost test1]$ ll
drwxrwxr-x. 3 lyh_sky lyh_sky 31 9月  10 11:43 test
[@localhost test1]$ tree test/
test/
├── file2
└── test├── file├── file1├── file2└── file31 directory, 5 files[@localhost test1]$ ll
drwxrwxr-x. 3 lyh_sky lyh_sky 18 9月  10 11:43 test[@localhost test1]$ mv test test2 // 将test目录重命名为test2
drwxrwxr-x. 3 lyh_sky lyh_sky 31 9月  10 11:43 test2

🚐cat指令

语法: cat [选项][文件]
功能: 查看目标文件的内容
常用选项

  • -b 对非空输出行编号

  • -n 对输出的所有行编号

  • -s 不输出多行空行

[@localhost Test_Make]$ ll
总用量 5
-rw-rw-r--. 1 lyh_sky lyh_sky   79 9月   5 10:54 main.cpp[@localhost Test_Make]$ cat main.cpp // 显示文件内容
#include "test.h"int main()
{Show();cout << Num << endl;return 0;
}[@localhost Test_Make]$ cat -n main.cpp // 对输出行编号1	#include "test.h"2	3	int main()4	{5	  Show();6	  cout << Num << endl;7	  return 0;8	}[@localhost Test_Make]$ cat -s main.cpp // 不输出多行空格
#include "test.h"int main()
{Show();cout << Num << endl;return 0;
}[lyh_sky@localhost Test_Make]$ cat -b main.cpp // 不输出空行且对行进行编号1	#include "test.h"2	int main()3	{4	  Show();5	  cout << Num << endl;6	  return 0;7	}

🚑more指令

语法:more [选项] [文件名]

功能:该命令类似于cat,用于阅读量比较大的文本文件,支持往前翻阅读文件

常用选项

  • -n:对输出的所有行进行编号
  • -q:退出more指令
// 这是一个while语句,将1w个hello world输入到中,并且编号
[@localhost Linux_Study]$ cnt=1; while [ $cnt -le 10000 ]; do echo "hello world $cnt"; let cnt++; done > [lyh_sky@localhost Linux_Study]$ ll
-rw-rw-r--. 1 lyh_sky lyh_sky 168894 9月  10 20:
[lyh_sky@localhost Linux_Study]$  

注意:进入more指令后,按下键可以对文件内容进行前翻


🚒less指令(important)

  • less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大

  • less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup][pagedown] 等按键的功能来往前往后翻看文件,更容易用
    来查看一个文件的内容!

  • 除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜索,也可以向上搜索

语法:less [选项] [文件名]

功能:less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前不会加载整个文件

常用选项

  • -i:忽略搜索时的大小写
  • -N:显示每行的行号
  • /字符串:向下搜索“字符串”的功能
  • ?字符串:向上搜索“字符串”的功能
  • n:重复前一个搜索(与 / 或 ? 有关)
  • N:反向重复前一个搜索(与 / 或 ? 有关)
  • q:quit
这个自己去试试,不好截图和粘贴代码
[@localhost Linux_Study]$ cnt=1; while [ $cnt -le 10 ]; do echo "hello world $cnt"; let cnt++; done > 
[@localhost Linux_Study]$ less - // 显示每行的行号
1 hello world 1
2 hello world 2
3 hello world 3
4 hello world 4
5 hello world 5
6 hello world 6
7 hello world 7
8 hello world 8
9 hello world 9
10 hello world 10[@localhost Linux_Study]$ less - // 忽略搜索时的大小写
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9
hello world 10

🚠输入/输出/追加重定向(</>/>>)

输入重定向(<):将文件中的数据流入到外设(显示器)当中

[@localhost lesson8]$  
hello world
[@localhost lesson8]$ cat <  
hello world

输出重定向(>):将输入(键盘)的数据流入到文件当中,如果文件中有数据,将会被清空

[@localhost lesson8]$ echo "aaaaaaaaa,bbbbbbbbbbbb" >  
[@localhost lesson8]$  
aaaaaaaaa,bbbbbbbbbbbb

追加重定向(>>):与输出重定向一样,但是不会清空文件中的数据,而是在下一行增加

[@localhost lesson8]$  
aaaaaaaaa,bbbbbbbbbbbb
[@localhost lesson8]$ echo "hello world" >>  
[@localhost lesson8]$  
aaaaaaaaa,bbbbbbbbbbbb
hello world

🚠管道

管道(|):顾名思义就是我们生活中传输天然气、石油、水源等等的东西

管道的基本应用,后面再研究原理:

[lyh_sky@localhost lesson8]$  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[lyh_sky@localhost lesson8]$ head -  | tail -5 | tail -2
9
10


🚓head指令

前言:head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中

语法:head [选项] [文件]
功能:head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行

常用选项:

  • -n(n是行数):显示文本头部前n行
[@localhost Linux_Study]$ 
[@localhost Linux_Study]$  
123
abc
456
def
789
hlj
101112
klm
[@localhost Linux_Study]$ head - // 显示前三行
123
abc
456
[lyh_sky@localhost Linux_Study]$  // 默认显示前10行
123
abc
456
def
789
hlj
101112
klm

🚕tail指令

前言:tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容

语法:tail [选项] [文件名]
功能:用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件

常用选项

  • -f:循环读取
  • -n(n是行数):显示文本尾部后n行
[lyh_sky@localhost Linux_Study]$  
123
abc
456
def
789
hlj
101112
klm
1111
1111
2222
[lyh_sky@localhost Linux_Study]$  // 不加任何选项,默认显示后十行
abc
456
def
789
hlj
101112
klm
1111
1111
2222
[lyh_sky@localhost Linux_Study]$ tail - // 显示后三行
1111
1111
2222

【-f选项只能截图进行演示…】


🚖时间相关指令

🚗date指令

date 指定格式显示时间: date +%Y:%m:%d
date 用法: date [选项] [格式]

  1. 在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中常用的标记列表如下:
  • %H : 小时(00~23)
  • %M : 分钟(00~59)
  • %S : 秒(00~60)
  • %X : 相当于 %H:%M:%S
  • %d : 日 (01~31)
  • %m : 月份 (01~12)
  • %Y : 完整年份 (0000~9999)
  • %F : 相当于 %Y-%m-%d

  1. 在时间设定方面
  • date -s // 设置当前时间,只有root权限才能设置,其他只能查看。
  • date -s 20080523 // 设置成20080523,这样会把具体时间设置成空00:00:00
  • date -s 01:01:01 // 设置具体时间,不会对日期做更改
  • date -s “01:01:01 2008-05-23″ // 这样可以设置全部时间
  • date -s “01:01:01 20080523″ // 这样可以设置全部时间
  • date -s “2008-05-23 01:01:01″ // 这样可以设置全部时间
  • date -s “20080523 01:01:01″ // 这样可以设置全部时间

  1. 时间戳
  • 时间->时间戳: date +%s
  • 时间戳->时间: date -d@1508749502
  • Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒
[lyh_sky@localhost Linux_Study]$ date +%s
1662818381 // 时间戳

🚘cal指令

前言:cal命令可以用来显示公历(阳历)日历。公历是现在国际通用的历法,又称格列历,通称阳历。 “阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”。

语法:cal [选项] [月份] [年份]

功能:用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份

常用选项

  • -3 显示系统前一个月,当前月,下一个月的月历
  • -j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
  • -y 显示当前年份的日历
[lyh_sky@localhost Linux_Study]$ cal // 无选项,显示当前月月历九月 2022     
日 一 二 三 四 五 六1  2  34  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30[lyh_sky@localhost Linux_Study]$ cal -3 // 显示上一个月,当前月,下个月的月历八月 2022             九月 2022             十月 2022     
日 一 二 三 四 五 六  日 一 二 三 四 五 六  日 一 二 三 四 五 六1  2  3  4  5  6               1  2  3                     17  8  9 10 11 12 13   4  5  6  7  8  9 10   2  3  4  5  6  7  8
14 15 16 17 18 19 20  11 12 13 14 15 16 17   9 10 11 12 13 14 15
21 22 23 24 25 26 27  18 19 20 21 22 23 24  16 17 18 19 20 21 22
28 29 30 31           25 26 27 28 29 30     23 24 25 26 27 28 2930 31               
[lyh_sky@localhost Linux_Study]$ cal -j // 显示当年中的第几天九月 2022         日  一  二  三  四  五  六244 245 246
247 248 249 250 251 252 253
254 255 256 257 258 259 260
261 262 263 264 265 266 267
268 269 270 271 272 273[lyh_sky@localhost Linux_Study]$ cal -y // 显示当年的全部月历2022                               一月                   二月                   三月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六1          1  2  3  4  5          1  2  3  4  52  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 129 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31四月                   五月                   六月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六1  2    1  2  3  4  5  6  7             1  2  3  43  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30七月                   八月                   九月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六1  2       1  2  3  4  5  6                1  2  33  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31十月                  十一月                 十二月       
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六1          1  2  3  4  5                1  2  32  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 109 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31

🚙find指令:(very important) -name

  • Linux下find命令在目录结构中搜索文件,并执行指定的操作

  • Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下

  • 即使系统中含有网络文件系统( NFS), find命令在该文件系统中同样有效,只你具有相应的权限

  • 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)

语法: find pathname -options
功能: 用于在文件树种查找文件,并作出相应的处理(可能访问磁盘)
常用选项

  • -name 按照文件名查找文件
[@localhost lesson8]$ ll
总用量 4
-rw-rw-r--. 1 lyh_sky lyh_sky 52 10月 25 16:
[@localhost lesson8]$ 

[@localhost lesson8]$ 
find: ‘’: 没有那个文件或目录

🚡grep指令

【grep指令参考文档】

语法: grep [选项] 搜寻字符串 文件

功能: 在文件中搜索字符串,将找到的行打印出来

常用指令:

  • -i:忽略大小写的不同,所有大小写视为相同

  • -n:输出行号

  • -v:反向选择,即显示出没有“搜寻字符串”内容的那一行

[@localhost lesson8]$  
C/C++
c/c++
Java
java
Python
python
Shell
shell
Hello world
hello world// 默认输出想匹配的字符的整行数据
[@localhost lesson8]$ grep 'c'  
c/c++// 忽略大小写的不同,输出匹配大小写的字符的整行数据
[lyh_sky@localhost lesson8]$ grep -i 'c'  
C/C++
c/c++// 附带行号
[@localhost lesson8]$ grep -in 'h'  
5:Python
6:python
7:Shell
8:shell
9:Hello world
10:hello world// 反向输出不匹配的字符的整行内容
[lyh_sky@localhost lesson8]$ grep -v 'm'  
C/C++
c/c++
Java
java
Python
python
Shell
shell
Hello world
hello world

🚢zip/unzip指令

语法: zip 压缩文件.zip 目录或文件

功能: 将目录或文件压缩成zip格式

常用选项:

  • -r 递 归处理,将指定目录下的所有文件和子目录一并处理
  • -d:unzip解压时,指定解压到指定路径下

压缩解压普通文件:

[@localhost lesson8]$ zip file. adding:  (deflated 28%)
[@localhost lesson8]$ ll
总用量 8
-rw-rw-r--. 1 lyh_sky lyh_sky  72 10月 25 22:
-rw-rw-r--. 1 lyh_sky lyh_sky 218 10月 25 22:56 file.zip// 解压到指定位置
[@localhost lesson8]$ mkdir test
[@localhost lesson8]$ ll
总用量 8
-rw-rw-r--. 1 lyh_sky lyh_sky  72 10月 25 22:
-rw-rw-r--. 1 lyh_sky lyh_sky 218 10月 25 22:56 file.zip
drwxrwxr-x. 2 lyh_sky lyh_sky   6 10月 25 22:57 test
[@localhost lesson8]$ unzip file.zip -d test/
Archive:  file.zipinflating:            
[@localhost lesson8]$ tree test/
test/
└── 0 directories, 1 file

压缩解压目录:

[@localhost lesson8]$ ls
file1
[@localhost lesson8]$ tree file1/
file1/
└── file2└── file3[@localhost lesson8]$ zip -r file.zip file1/adding: file1/ (stored 0%)adding: file1/file2/ (stored 0%)adding: file1/file2/file3/ (stored 0%)
[@localhost lesson8]$ ls
file1  file.zip// 解压到指定位置
[@localhost lesson8]$ mkdir test
[@localhost lesson8]$ ls
file1  file.zip  test
[@localhost lesson8]$ unzip file.zip -d test/
Archive:  file.zipcreating: test/file1/creating: test/file1/file2/creating: test/file1/file2/file3/
[@localhost lesson8]$ tree test/
test/
└── file1└── file2└── file3

🚣tar指令(重要):打包/解包,不打开它,直接看内容

tar [-cxtzjvf] 文件与目录 …参数:

  • -c :建立一个压缩文件的参数指令(create 的意思);
  • -x :解开一个压缩文件的参数指令!
  • -t :查看 tarfile 里面的文件!
  • -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
  • -j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩?
  • -v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!
  • -f :使用档名,请留意,在 f 之后要立即接档名喔!不要再加参数!
  • -C : 解压到指定目录

基本的打包压缩和解压过程 -zcf/xcf

[lyh_sky@localhost lesson8]$ ls

[lyh_sky@localhost lesson8]$ 
hello world
// 建立一个压缩文件
[lyh_sky@localhost lesson8]$ tar   
[lyh_sky@localhost lesson8]$ ls
  [lyh_sky@localhost lesson8]$ 
[lyh_sky@localhost lesson8]$ ls

// 解压,默认解压到当前路径
[lyh_sky@localhost lesson8]$ tar 
[lyh_sky@localhost lesson8]$ ls
  [lyh_sky@localhost lesson8]$ mkdir test
[lyh_sky@localhost lesson8]$ ls
    test
// 解压到指定目录中
[lyh_sky@localhost lesson8]$ tar  -C test/
[lyh_sky@localhost lesson8]$ tree test/
test/
└── 0 directories, 1 file

压缩和解压的过程中显示文件 -zvcf/xvcf

[lyh_sky@localhost lesson8]$ ls
test
[lyh_sky@localhost lesson8]$ tree test/
test/
└── file1└── file2└── file33 directories, 0 files
// 打包压缩过程中显示文件
[lyh_sky@localhost lesson8]$ tar  test
test/
test/file1/
test/file1/file2/
test/file1/file2/file3/
[lyh_sky@localhost lesson8]$ ls
test  [lyh_sky@localhost lesson8]$ rm -rf test
[lyh_sky@localhost lesson8]$ ls

//解包过程中显示文件
[lyh_sky@localhost lesson8]$ tar 
test/
test/file1/
test/file1/file2/
test/file1/file2/file3/
[lyh_sky@localhost lesson8]$ ls
test  
[lyh_sky@localhost lesson8]$ tree test
test
└── file1└── file2└── file33 directories, 0 files

🚤bc指令(浮点数运算)

bc命令可以很方便的进行浮点运算

[@localhost ~]$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
13.14 * 13.14
172.65
12.5 * 25.4
317.5

注意:如果想要推出bc指令,可以直接ctrl+c结束该进程


🚥uname –r指令

语法: uname [选项]

功能: uname用来获取电脑和操作系统的相关信息

补充说明: uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息

常用选项:

  • -a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类
    型,硬件平台类型,操作系统名称
[@localhost ~]$ uname
Linux
[@localhost ~]$ uname -r
3.10.0-1160.76.1.el7.x86_64
[@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

🚦重要的几个热键[Tab],[ctrl]-c, [ctrl]-d

  • [Tab]按键:具有『命令补全』和『档案补齐』的功能

  • [Ctrl]+c按键:让当前的程序『停掉』-- 结束该进程

  • [Ctrl]+d按键—通常代表着:『键盘输入结束(End Of File, EOF 戒 End OfInput)』的意思;另外,他也可以用来取代exit

  • [Ctrl+r]:查找历史使用的指令

// 按下Ctrl+r -- 然后搜索(输入对应的字符串)
(reverse-i-search)`while': cnt=1; while [ $cnt -le 10000 ]; do echo "hello world"; cnt++; done > // Ctrl+d是退出当前用户,如果用户是管理员,则退出终端

本文发布于:2024-02-02 01:21:21,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170681263140490.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

下一篇:ASP.NET Core
标签:指令   选项   常用   Linux
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23