linux基础指令(下)

目录

1、echo指令

 2、more指令

3、less指令

 4、head指令

5、tail指令

6、 date指令

7、 cal指令

8、find指令

9、which指令

10、whereis指令

 11、alias指令

 12、grep指令

13、zip/unzip指令

14、tar指令

15、uname指令


1、echo指令

功能:将后面默认的内容打印到显示器

示例

[root@ecs-67186 lesson1]# echo "hello world"
hello world
也可以在其后面加一个>写入文件当中

[root@ecs-67186 lesson1]# ll
total 0
[root@ecs-67186 lesson1]# echo "hello world" > file.txt
[root@ecs-67186 lesson1]# ll
total 4
-rw-r--r-- 1 root root 12 Aug 17 09:27 file.txt
其中>叫做输出重定向,输出重定向的意思就是本该打印到显示器上的内容写入到文件中。

如果后面跟的文件是不存在的,它会帮你直接创建这个文件,如果文件是存在的,它会直接访问。

访问的话有两种形式:1、输出重定向(从文件的开始,覆盖式写入)

                                        [root@ecs-67186 lesson1]# echo "aaaaaaa" > file.txt
                                        [root@ecs-67186 lesson1]# cat file.txt
                                        aaaaaaa
                                        [root@ecs-67186 lesson1]# echo "bbb" > file.txt 
                                        [root@ecs-67186 lesson1]# cat file.txt
                                        bbb
                                        不管你文件之前有什么内容,全部都覆盖掉。

                                    2、追加重定向(从文件的末尾,追加式写入)

                                     [root@ecs-67186 lesson1]# echo "x" > file.txt
                                     [root@ecs-67186 lesson1]# echo "y" >> file.txt
                                     [root@ecs-67186 lesson1]# echo "z" >> file.txt
                                     [root@ecs-67186 lesson1]# cat file.txt
                                      x
                                      y
                                      z
                                    注意是两个>>,中间没有空格

既然有输出重定向那肯定就有输入重定向<,输入重定向把本来从键盘中读取内容,变成从文件中读取。

[root@ecs-67186 lesson1]# cat 
hds
hds
dsdf
dsdf
^C

[root@ecs-67186 lesson1]# cat < file.txt
x
y
z
单单一个cat命令是从键盘中读取,可以按Ctrl+c终止掉。 

[root@ecs-67186 lesson1]# cat file.txt > test.txt
[root@ecs-67186 lesson1]# ll
total 8
-rw-r--r-- 1 root root 6 Aug 17 09:46 file.txt
-rw-r--r-- 1 root root 6 Aug 17 10:08 test.txt
[root@ecs-67186 lesson1]# cat test.txt
x
y
z
输出重定向和输入重定向写法多样,也可以搭配使用,就不一一演示了。                     

 2、more指令

功能:阅读长文本

 常用选项

-n 输出相应行数

示例

[root@ecs-67186 lesson1]# cout=0; while [ $cout -le 1000 ]; do echo "hello linux ${cout}"; let cout++; done > file.txt #BR/#
[root@ecs-67186 lesson1]# more file.txt
hello linux 0
hello linux 1
hello linux 2
hello linux 3
hello linux 4
hello linux 5
hello linux 6
hello linux 7
hello linux 8
hello linux 9
hello linux 10
--More--(3%)
more显示的内容会将一个屏幕打满,按上下键是没有用的,按回车才能往后翻,按q退出

[root@ecs-67186 lesson1]# more -5 file.txt
hello linux 0
hello linux 1
hello linux 2
hello linux 3
hello linux 4
--More--(0%)

3、less指令

功能:和more类似,但是less可以随便浏览文件

常用选项

i :忽略搜索时的大小写
/+ 字符串:向下搜索“字符串”的功能
?+字符串:向上搜索“字符串”的功能
N:返回前一个搜索

示例

[root@ecs-67186 lesson1]# less file.txt

hello linux 0
hello linux 1
hello linux 2
hello linux 3
hello linux 4
hello linux 5
hello linux 6
hello linux 7
hello linux 8
hello linux 9

……

hello linux 33
hello linux 34
hello linux 35
:
是在这:后面输出指令
同样是将一个屏幕打满,但是可以按上下键翻,也可以按回车,按q退出。

 4、head指令

功能:获取文件头部的若干行

head+n(行数):显示的行数

示例

[root@ecs-67186 lesson1]# head -5 file.txt
hello linux 0
hello linux 1
hello linux 2
hello linux 3
hello linux 4
 

5、tail指令

 功能:获取文件尾部的若干行,和head类似

head和tail常常搭配使用

示例: 

[root@ecs-67186 lesson1]# tail file.txt
hello linux 991
hello linux 992
hello linux 993
hello linux 994
hello linux 995
hello linux 996
hello linux 997
hello linux 998
hello linux 999
hello linux 1000
[root@ecs-67186 lesson1]# head -666 file.txt | tail -5
hello linux 661
hello linux 662
hello linux 663
hello linux 664
hello linux 665

其中|叫做管道,顾名思义它就是用来传输数据的,这里就是head处理完的数据通过管道又交给tail处理。

6、 date指令

功能:查看时间

可以在后面加标记显示你要的时间

%H : 小时
%M : 分钟
%S : 秒
%X : 相当于 %H:%M:%S
%d : 日 
%m : 月份 
%Y : 完整年份 

示例

[root@ecs-67186 lesson1]# date +%Y
2022
[root@ecs-67186 lesson1]# date +%Y:%M
2022:10
[root@ecs-67186 lesson1]# date +%Y:%m
2022:08
[root@ecs-67186 lesson1]# date +%Y:%m:%d
2022:08:17
[root@ecs-67186 lesson1]# date +%Y:%m:%d-%H
2022:08:17-16
[root@ecs-67186 lesson1]# date +%Y:%m:%d-%H:%M
2022:08:17-16:12
[root@ecs-67186 lesson1]# date +%Y:%m:%d-%H:%M:%S
2022:08:17-16:12:40
[root@ecs-67186 lesson1]# date +%X
04:15:10 PM
 

[root@ecs-67186 lesson1]# date +%s
1660724212
[root@ecs-67186 lesson1]# date +%s
1660724232
这一长串数字就是时间戳,它是从1970年1月1日0时0分开始一直到现在的秒数。不过它是外国人定的时间,和我们的北京时间相差8个小时

该数据是单向递增的,具有唯一性。

时间戳也可以转化为时间

[root@ecs-67186 lesson1]# date -d@1660724232
Wed Aug 17 16:17:12 CST 2022
[root@ecs-67186 lesson1]# date  +%Y:%m:%d-%H:%M:%S -d@1660724232
2022:08:17-16:17:12

[root@ecs-67186 lesson1]# date  +%Y:%m:%d-%H:%M:%S -d@0
1970:01:01-08:00:00
 

7、 cal指令

功能:用来显示公历(阳历)日历

常用选项

-3 : 显示系统前一个月,当前月,下一个月的日历

-1 : 显示当前月(不加也行,直接cal是同样的效果)

示例

[root@ecs-67186 lesson1]# cal
     August 2022    
Su Mo Tu We Th Fr Sa
    1  2  3  4  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 31

[root@ecs-67186 lesson1]# cal -1
     August 2022    
Su Mo Tu We Th Fr Sa
    1  2  3  4  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 31

[root@ecs-67186 lesson1]# cal -3
      July 2022            August 2022         September 2022   
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
                1  2      1  2  3  4  5  6               1  2  3
 3  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                                                              
 

8、find指令

功能:查找文件(后面跟你要找的文件名)

 示例
 [root@ecs-67186 lesson1]# find ~ -name file.txt
/root/file.txt
/root/lesson1/file.txt
[root@ecs-67186 lesson1]# find file.txt
file.txt

更多选项可以通过man指令去了解

9、which指令

功能: 在系统路径中,去查找特定的指令所在的路径

其实我们所用的指令本质上是可执行程序,和我们Windows桌面上的APP图标一样

 示例

[root@ecs-67186 lesson1]# which ls
alias ls='ls --color=auto'
    /usr/bin/ls
[root@ecs-67186 lesson1]# which passwd
/usr/bin/passwd

10、whereis指令

功能:在系统路径下查找文档

示例

 [root@ecs-67186 lesson1]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz

只要带相关字眼的都会帮你找出来

 11、alias指令

功能:给一条指令取别名

示例

[root@ecs-67186 lesson1]# alias hhhh='ls -al'

[root@ecs-67186 lesson1]# hhhh
total 28
drwxr-xr-x  2 root root  4096 Aug 17 10:08 .
dr-xr-x---. 9 root root  4096 Aug 17 09:19 ..
-rw-r--r--  1 root root 15907 Aug 17 10:39 file.txt
-rw-r--r--  1 root root     6 Aug 17 10:08 test.txt

如果你退出再重新登录的话,这个就不起作用了。

 12、grep指令

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

常用选项

grep -i : 忽略大小写的不同,视为一样的

-n : 输出行号

-v : 反向选择,显示出与你想搜索的相反的内容

我们也可以将管道和输出重定向与grep结合使用,将你想搜索的内容写入新的文件中

示例

[root@ecs-67186 lesson1]# grep '66' file.txt | head -10 > tmp.txt
[root@ecs-67186 lesson1]# cat tmp.txt
hello linux 66
hello linux 166
hello linux 266
hello linux 366
hello linux 466
hello linux 566
hello linux 660
hello linux 661
hello linux 662
hello linux 663
[root@ecs-67186 lesson1]# grep '88' file.txt
hello linux 88
hello linux 188
hello linux 288
hello linux 388
……
 

13、zip/unzip指令

功能:zip是压缩文件,unzip是解压文件

常用选项

-r : 递归处理,将指定目录下的所有文件和子目录一并处理

示例

注意一定要加.zip后缀 

上面演示的是解压到当前路径,要解压到指定路径还要加-d和路径名

zip/unzip name.zip -d 指定的路径名 

14、tar指令

功能:打包和解压

常用选项

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

示例

压缩:

注意后缀名是.tgz

由于我解压出来的文件名重名了,所以我换了个路径解压

解压:

解压到指定路径

[root@ecs-67186 test]# tar -xzf project.tgz -C /root/project 

15、uname指令

功能:在linux下查看linux的体系结构和内核版本

 常用选项:

-a : 输出详细信息

-r : 查看内核版本

示例

[root@ecs-67186 test]# uname -a
Linux ecs-67186 3.10.0-1160.53.1.el7.x86_64 #1 SMP Fri Jan 14 13:59:45 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@ecs-67186 test]# uname -r
3.10.0-1160.53.1.el7.x86_64

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值