linux基本指令(下)

时间:2022-11-24 08:01:50

(文章目录)

1.sort 指令

使用vim编辑器,在888.txt文件中输入内容

[yzq@VM-8-8-centos 6.6]$  vim 888.txt
[yzq@VM-8-8-centos 6.6]$ cat 888.txt
1111
2222
3333
4444
444444
333333
8888

  • 这里vim编辑器的用法先不用了解,记住 使用 a 后输入你想要打印的内容
  • 最后使用 ESC :wq 退出vim编辑器

sort ——升序

对应文本内容按行为单位进行排序

[yzq@VM-8-8-centos 6.6]$ sort 888.txt

1111
2222
3333
333333
4444
444444
8888

  • sort排序规则:从左向右,每行的第一个字母开始按ascii值进行比较,谁的ascii值小就放在前面

sort -r ——降序

[yzq@VM-8-8-centos 6.6]$ sort -r 888.txt
8888
444444
4444
333333
3333
2222
1111


sort +文件 | uniq ——去重

再次使用vim ,将888.txt文件内容进行修改,使其拥有重复的1111 、2222、3333


[yzq@VM-8-8-centos 6.6]$ vim 888.txt
[yzq@VM-8-8-centos 6.6]$ cat 888.txt
1111
1111
2222
2222
3333
3333

  • 这里vim编辑器的用法先不用了解,记住 使用 a 后输入你想要打印的内容
  • 最后使用 ESC :wq 退出vim编辑器
[yzq@VM-8-8-centos 6.6]$ sort 888.txt | uniq

1111
2222
3333

把相邻的一样的文本,压缩只剩下一个 去重后,只剩下 1111 2222 3333

2. find - name 指令

根据指定选项完成文件搜索

[yzq@VM-8-8-centos 6.6]$ find ~ -name  test.c
/home/mydir/game/the-first-stu-wirting/通讯录 文件/通讯录 文件/test.c
/home/mydir/game/the-first-stu-wirting/文件/文件/test.c
/home/mydir/lesson5/test.c

  • ~ 代表当前目录的家目录
  • find ~ -name test.c 代表 在家目录中test.c文件全部被查找到

3. which 指令

搜索对应指令,在对应的路径

[yzq@VM-8-8-centos 6.6]$ which pwd
/usr/bin/pwd

说明pwd指令在 /usr/bin 路径中

alias ——起别名

[yzq@VM-8-8-centos 6.6]$ which ls
alias ls='ls --color=auto'
	/usr/bin/ls

这里出现了一个alias,是用来给特定的命令起别名 对此我们可以验证一下

[yzq@VM-8-8-centos 6.6]$ ls -la -i -n
total 12
786436 drwxrwxr-x  2 1002 1002 4096 Nov 23 10:41 .
657483 drwx------ 15 1002 1002 4096 Nov 23 10:41 ..
786446 -rw-rw-r--  1 1002 1002   31 Nov 23 10:41 888.txt
[yzq@VM-8-8-centos 6.6]$ alias myls='ls -la -i -n'
[yzq@VM-8-8-centos 6.6]$ myls
total 12
786436 drwxrwxr-x  2 1002 1002 4096 Nov 23 10:41 .
657483 drwx------ 15 1002 1002 4096 Nov 23 10:41 ..
786446 -rw-rw-r--  1 1002 1002   31 Nov 23 10:41 888.txt

  • ls -la -i -n 输出的结果 与起别名后 myls 的结果相同
[yzq@VM-8-8-centos 6.6]$ which myls
alias myls='ls -la -i -n'
	/usr/bin/ls

再次使用 which myls ,就显示了 将 ls -la -i -n 起别名为 myls

4. whereis 指令

系统默认路径下指定名称的文件、程序、或者归档文件(压缩包)

[yzq@VM-8-8-centos 6.6]$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

  • 既找到 ls程序,又把程序配套的man手册对应的内容也显示出来了

5. grep指令

1.作用

文本行过滤工具 将指定文本内容按照特定关键字来进行按行筛选

这里就要用到上一节提到的1——1000的带有编号的hello wold 的创建linux基本指令(中)

[yzq@VM-8-8-centos 6.6]$ grep '88' test.txt
hello world [88]
hello world [188]
hello world [288]
hello world [388]
hello world [488]
hello world [588]
hello world [688]
hello world [788]
hello world [880]
hello world [881]
hello world [882]
hello world [883]
hello world [884]
hello world [885]
hello world [886]
hello world [887]
hello world [888]
hello world [889]
hello world [988]

在test.txt文件中 搜索 带有88的内容

2. grep -n

[yzq@VM-8-8-centos 6.6]$ grep -n '88' test.txt
89:hello world [88]
189:hello world [188]
289:hello world [288]
389:hello world [388]
489:hello world [488]
589:hello world [588]
689:hello world [688]
789:hello world [788]
881:hello world [880]
882:hello world [881]
883:hello world [882]
884:hello world [883]
885:hello world [884]
886:hello world [885]
887:hello world [886]
888:hello world [887]
889:hello world [888]
890:hello world [889]
989:hello world [988]

对应行的行号会被带上

3.grep - v

反向匹配 首先使用vim,将888.txt的内容改变

[yzq@VM-8-8-centos 6.6]$ vim 888.txt
[yzq@VM-8-8-centos 6.6]$ cat 888.txt
aaaa
bbbb
cccc
AAAA
BBBB
CCCC

这里vim的问题就不说了,有需要就去从头看看vim的用法

[yzq@VM-8-8-centos 6.6]$ grep 'aaaa' -v 888.txt
bbbb
cccc
AAAA
BBBB
CCCC
  • 除了包含 aaaa文本内容的显示全部出来

4. grep - i

忽略大小写

[yzq@VM-8-8-centos 6.6]$ grep -i 'aaaa' 888.txt
aaaa
AAAA

6 .zip指令

1. 安装

使用前需要安装下

[yzq@VM-8-8-centos 6.6]$ yum install -y zip unzip

2. 打包

zip 文件名.zip 文件名

[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test
[yzq@VM-8-8-centos ~]$ zip test.zip test
  adding: test/ (stored 0%)
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.zip

这里使用zip 对test目录进行打包后,会出现 test.zip的压缩包

3. 解包

unzip 文件名.zip

[yzq@VM-8-8-centos ~]$ mv test.zip my
[yzq@VM-8-8-centos ~]$ cd my
[yzq@VM-8-8-centos my]$ ls
test.zip
[yzq@VM-8-8-centos my]$ unzip test.zip
Archive:  test.zip
   creating: test/
[yzq@VM-8-8-centos my]$ tree test
test

0 directories, 0 files

将test.zip压缩包剪切到my目录下,解包后发现没有数据存在 是因为 打包的时候没有把里面的东西 打包并压缩

zip -r

将里面的内容也打包并压缩 zip -r 文件名.zip 文件名

6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test
[yzq@VM-8-8-centos ~]$ zip -r test.zip test
  adding: test/ (stored 0%)
  adding: test/makefile (deflated 16%)
  adding: test/mytest (deflated 71%)
  adding: test/mytest.c (deflated 38%)
  adding: test/mytest_d (deflated 70%)
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.zip
[yzq@VM-8-8-centos ~]$ mv test.zip my
[yzq@VM-8-8-centos ~]$ cd my
[yzq@VM-8-8-centos my]$ ls
test  test.zip
[yzq@VM-8-8-centos my]$ unzip test.zip
Archive:  test.zip
  inflating: test/makefile           
  inflating: test/mytest             
  inflating: test/mytest.c           
  inflating: test/mytest_d           
[yzq@VM-8-8-centos my]$ tree test
test
|-- makefile
|-- mytest
|-- mytest.c
`-- mytest_d

0 directories, 4 files

此时使用 zip -r 将test 压缩成 test .zip的压缩包后,再剪切到my目录中打开 发现有test内部的内容啦

4. unzip -d

解包到指定目录下 需要注意的是 虽然解包了 但原有位置的.zip依旧存在

6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test
[yzq@VM-8-8-centos ~]$ zip -r test.zip test
  adding: test/ (stored 0%)
  adding: test/makefile (deflated 16%)
  adding: test/mytest (deflated 71%)
  adding: test/mytest.c (deflated 38%)
  adding: test/mytest_d (deflated 70%)
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.zip
[yzq@VM-8-8-centos ~]$ unzip test.zip -d 6.6
Archive:  test.zip
   creating: 6.6/test/
  inflating: 6.6/test/makefile       
  inflating: 6.6/test/mytest         
  inflating: 6.6/test/mytest.c       
  inflating: 6.6/test/mytest_d       
[yzq@VM-8-8-centos ~]$ cd 6.6
[yzq@VM-8-8-centos 6.6]$ ls
888.txt  test  test.txt

linux基本指令(下)

在test的所处目录中,使用unzip -d 将test.zip 压缩包传入到6.6中, 就不用使用mv剪切了

7. tar 指令

1.tar -czf ——打包

  • tar - c :创建一个新的归档文件即压缩包

  • tar - z : 使用打包的同时可以进行压缩

  • tar - f : 给归档文件一个名字 建议把 f 放在最后

  • tar -czf + 文件名.tgz +文件名

[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test
[yzq@VM-8-8-centos ~]$ tar -czf test.tgz test
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.tgz

linux基本指令(下)

压缩包 以 .tgz 为结尾

2.tar -xzf —— 解包

  • tar -x 解开文件
  • tar -xzf 文件名.tgz
[yzq@VM-8-8-centos ~]$ tar -xzf test.tgz
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.tgz
[yzq@VM-8-8-centos ~]$ tree test
test
|-- makefile
|-- mytest
|-- mytest.c
`-- mytest_d

0 directories, 4 files

3.tar -ztvf ——直接查看压缩包内的内容

  • tar -ztvf +文件名.tgz
  • tar -t 不解压 ,直接看压缩包中有什么
  • tar -v :将文件显示更详细
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.tgz
[yzq@VM-8-8-centos ~]$ tar -ztvf test.tgz
drwxrwxr-x yzq/yzq           0 2022-11-23 06:37 test/
-rw-rw-r-- yzq/yzq          75 2022-10-15 18:29 test/makefile
-rwxrwxr-x yzq/yzq        8441 2022-11-23 06:37 test/mytest
-rw-rw-r-- yzq/yzq         278 2022-10-16 14:03 test/mytest.c
-rwxrwxr-x yzq/yzq        9648 2022-10-15 18:30 test/mytest_d

4. tar -xzf 文件名.tgz -C

解包到指定目录下

  • tar -xzf 文件名.tgz -C
[yzq@VM-8-8-centos ~]$ tar czf test.tgz test
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.tgz
[yzq@VM-8-8-centos ~]$ tar -xzf test.tgz -C 6.6
[yzq@VM-8-8-centos ~]$ ls
6.6  9.9  dir  game  lesson5  mk  mkdir  my  mybin  test  test.tgz
[yzq@VM-8-8-centos ~]$ cd 6.6
[yzq@VM-8-8-centos 6.6]$ ls
888.txt  test  test.txt
[yzq@VM-8-8-centos 6.6]$ tree test
test
|-- makefile
|-- mytest
|-- mytest.c
`-- mytest_d

0 directories, 4 files

linux基本指令(下)

将 test的压缩包 test.tgz ,解包到6.6目录中

8. bc 指令

linux上计算器,看可以进行加减乘除,也可以进行精度计算

[yzq@VM-8-8-centos ~]$  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'. 
1+1
2
5*2
10
2.0*5.0
10.0
5.0/4.0
1