Linux基本指令(二)

目录

1 man指令(重要)

常用选项

2 cp指令

2.1 语法 

2.2 功能

2.3 说明

2.4 常用选项

3 mv指令(重要)

3.1 语法

3.2 功能

3.3 常用选项:

4 echo

4.1 输出重定向

4.2 追加重定向

4.3 输入重定向

5 wc

5.1 语法

5.2 用法

6 cat

6.1 语法

6.2 功能

6.3 常用选项

7 more指令

7.1 语法

7.2 功能

7.3 常用选项

8 less指令

8.1 语法

8.2 功能

8.3 选项

9 head指令

9.1 语法

9.2 功能:

9.3 选项:

10 tail指令

10.1 语法

10.2 功能

10.3 选项:

10.4 举例 取文件中间n行

11 find指令: -name

11.1 语法

11.2 功能

11.3 常用选项

12 which

12.1 语法

12.2 功能

13 alias

13.1 语法                                                       

13.2 功能

13.3 举例

14 whereis

14.1 语法

14.2 功能

15 grep指令

15.1 语法

15.2 功能

15.3 常用选项:

16 top 

16.1 语法

16.2 功能

17 zip/unzip指令

17.1 语法 

17.2 功能

17.3 常用选项

17.4 举例

18 date显示

18.1 在显示方面

18.2 在设定时间方面

18.3 时间戳

19 cal

19.1 cal指令

19.2 命令格式:

19.3 功能

19.4 常用选项:

20 命令行管道

21 sort

21.1 排序

21.2 去重排序

22 tar指令

22.1 参数

22.2 举例

23 热键

23.1 ctrl+c

23.2 [Ctrl]-d

23.3 上下按键

23.4 ctrl +r

23.5 拓展 history命令   

24 bc

25 uname –r指令:

25.1 语法

25.2 功能

25.3 补充说明

25.4 常用选项

26 shutdown关机

27 reboot 重启

28 扩展


1 man指令(重要)

Linux 的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问 Linux 手册页的命令是 man 语法: man [选项] 命令

常用选项

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

面手册分为8章

1 是普通的命令
2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)
3 是库函数,如printf,fread4是特殊文件,也就是/dev下的各种设备文件
5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义
6 是给游戏留的,由各个游戏自己定义
7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明
8 是系统管理用的命令,这些命令只能由root使用,如ifconfifig

2 cp指令

2.1 语法 

cp [选项] 源文件或目录 目标文件或目录

2.2 功能

复制文件或目录

2.3 说明

cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息

2.4 常用选项

-f 或 --force 强行复制文件或目录, 不论目的文件或目录是否已经存在
-i 或 --interactive 覆盖文件之前先询问用户
-r 递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
-R 或 --recursive递归处理,将指定目录下的文件及子目录一并处理
cp -rf 

3 mv指令(重要)

        mv命令是 move 的缩写,可以用来 移动 文件 (相当于剪切) 或者将文件 改名 move (rename) fifiles ),是 Linux 系统下常用的命令,经常用来备份文件或者目录。

3.1 语法

mv [ 选项 源文件或目录 目标文件或目录

3.2 功能

1. mv 命令中第二个参数类型的不同(是目标文件还是目标目录), mv 命令将文件重命名或将其移至一个新的目录中。
2. 当第二个参数类型是文件时, mv 命令完成 文件重命名 ,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录 重命名为给定的目标文件名

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

3.3 常用选项

-f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
-i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!

4 echo

IO

4.1 输出重定向

[root@VM-12-17-centos lesson3]# cat my.txt
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">my.txt
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">my.txt
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">my.txt
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc

         echo "内容">目标文件 ,写入到显示器文件,写入之前会清空内容,所以多次写入并不增加内容;

        显示到显示器上,向显示器打印,可以理解为向显示器写入,显示器也可以看成一种“文件”,c程序获取数据scanf\cin,都是从键盘获取,也可以看成一种“文件”,Linux以下一切皆文件。

4.2 追加重定向

[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">>my.txt
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">>my.txt
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">>my.txt
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">>my.txt
[root@VM-12-17-centos lesson3]# echo "aaa bbb ccc">>my.txt
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc

        echo " 内容”>>目标文件 不断向目标文件新增内容 

4.3 输入重定向

 cat wc 读取文本并回显

[root@VM-12-17-centos lesson3]# cat <my.txt
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
[root@VM-12-17-centos lesson3]# wc -l < my.txt
6

        如果cat后不接文本名称,则键入什么回显什么,即默认从键盘读取数据

[root@VM-12-17-centos lesson3]# cat
a
a
a^H
a
^C
[root@VM-12-17-centos lesson3]# cat
xbajhxbauycb
xbajhxbauycb
bjhbi
bjhbi
^C

5 wc

5.1 语法

wc+文件名

5.2 用法

类似cat

wc -l输出文件行数

[root@VM-12-17-centos lesson3]# wc my.txt
 6 18 72 my.txt
[root@VM-12-17-centos lesson3]# wc -l my.txt
6 my.txt
[root@VM-12-17-centos lesson3]# wc -l < my.txt
6

6 cat

6.1 语法

cat [ 选项 ][ 文件 ]

6.2 功能

 查看目标文件的内容
[root@VM-12-17-centos lesson3]# cat <my.txt
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc

6.3 常用选项

-b 对非空输出行编号
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
 
 
 
[root@VM-12-17-centos lesson3]# cat -b my.txt
     1	aaa bbb ccc
     2	aaa bbb ccc
     3	aaa bbb ccc
     4	aaa bbb ccc
     5	aaa bbb ccc
     6	aaa bbb ccc





-n 对输出的所有行编号
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
 
 
 
 
[root@VM-12-17-centos lesson3]# cat -n my.txt
     1	aaa bbb ccc
     2	aaa bbb ccc
     3	aaa bbb ccc
     4	aaa bbb ccc
     5	aaa bbb ccc
     6	aaa bbb ccc
     7	 
     8	 
     9	 
    10	 
-s 不输出多行空行
[root@VM-12-17-centos lesson3]# cat my.txt
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
aaa bbb ccc
 




[root@VM-12-17-centos lesson3]# cat -sn my.txt
     1	aaa bbb ccc
     2	aaa bbb ccc
     3	aaa bbb ccc
     4	aaa bbb ccc
     5	aaa bbb ccc
     6	aaa bbb ccc
     7	

        如果cat后不接文本名称,则键入什么回显什么,即默认从键盘读取数据

[root@VM-12-17-centos lesson3]# cat
a
a
a^H
a
^C
[root@VM-12-17-centos lesson3]# cat
xbajhxbauycb
xbajhxbauycb
bjhbi
bjhbi
^C

7 more指令

7.1 语法

more [选项][文件]

7.2 功能

more命令,功能类似 cat

7.3 常用选项

-n 对输出的所有行编号
q 退出more
按enter显示下一行
按空格键显示下一页

8 less指令

        less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大。
        less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就 可以使用 [pageup][pagedown] 等按键 的功能来往前往后翻看文件,更容易用来查看一个文件的内容!
        除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜。

8.1 语法

 less [参数] 文件

8.2 功能

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

8.3 选项

-i  忽略搜索时的大小写
-N  显示每行的行号
/字符串:向下搜索“字符串”的功能
?字符串:向上搜索“字符串”的功能
n:重复前一个搜索(与 / 或 ? 有关)
N:反向重复前一个搜索(与 / 或 ? 有关)
q:quit

9 head指令

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

9.1 语法

 head [参数]... [文件]... 

9.2 功能

head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。 

9.3 选项

-n<行数> 显示的行数
[root@VM-12-17-centos lesson3]# head -5 mylog.txt
hello 0
hello 1
hello 2
hello 3
hello 4

10 tail指令

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

10.1 语法

tail[必要参数][选择参数][文件] 

默认后十行

[root@VM-12-17-centos lesson3]# tail mylog.txt
hello 991
hello 992
hello 993
hello 994
hello 995
hello 996
hello 997
hello 998
hello 999
hello 1000
[root@VM-12-17-centos lesson3]# tail -5 mylog.txt
hello 996
hello 997
hello 998
hello 999
hello 1000

10.2 功能

用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文件。

10.3 选项:

-f 循环读取

-n<行数> 显示行数

10.4 举例 取文件中间n行

例如:从500开始,取20行
[root@VM-12-17-centos lesson3]# head -520 mylog.txt|tail -20
hello 500
hello 501
hello 502
hello 503
hello 504
hello 505
hello 506
hello 507
hello 508
hello 509
hello 510
hello 511
hello 512
hello 513
hello 514
hello 515
hello 516
hello 517
hello 518
hello 519
[root@VM-12-17-centos lesson3]# head -520 mylog.txt|tail -20|wc -l
20

        先head前520行,再tail后20行

11 find指令: -name

        Linux下find命令在目录结构中搜索文件,并执行指定的操作。
        Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。
        即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。
        在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系 统可能会花费很长的时间(这里是指30G字节以上的文件系统)。

11.1 语法

find pathname -options

11.2 功能

用于在文件树种查找文件,并作出相应的处理(可能访问磁盘)

11.3 常用选项

-name   按照文件名查找文件

[root@VM-12-17-centos lesson3]# find /root/lesson3 -name  test.c
/root/lesson3/test.c

12 which

12.1 语法

which 指令

12.2 功能

查看指令路径

[root@VM-12-17-centos lesson3]# which pwd
/usr/bin/pwd
[root@VM-12-17-centos lesson3]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@VM-12-17-centos lesson3]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
	/usr/bin/alias
	/usr/bin/which
[root@VM-12-17-centos lesson3]# which clear
/usr/bin/clear

13 alias

13.1 语法                                                       

 alias 别名=‘原名’

13.2 功能

         起别名

13.3 举例

[root@VM-12-17-centos lesson3]# alias myls='ls -a -l -i -n'
[root@VM-12-17-centos lesson3]# ll
total 32
-rwxr-xr-x 1 root root 8408 Jan  6 10:18 a.out
-rw-r--r-- 1 root root 9901 Jan  6 10:33 mylog.txt
-rw-r--r-- 1 root root   69 Jan  6 10:24 my.txt
-rw-r--r-- 1 root root   93 Jan  6 10:18 test.c
[root@VM-12-17-centos lesson3]# myls
total 40
657003 drwxr-xr-x  2 0 0 4096 Jan  6 10:23 .
393219 dr-xr-x---. 7 0 0 4096 Jan  5 21:15 ..
657768 -rwxr-xr-x  1 0 0 8408 Jan  6 10:18 a.out
657311 -rw-r--r--  1 0 0 9901 Jan  6 10:33 mylog.txt
657790 -rw-r--r--  1 0 0   69 Jan  6 10:24 my.txt
657764 -rw-r--r--  1 0 0   93 Jan  6 10:18 test.c
[root@VM-12-17-centos lesson3]# which myls
alias myls='ls -a -l -i -n'
	/usr/bin/ls
[root@VM-12-17-centos lesson3]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

14 whereis

14.1 语法

        whereis 关键字

14.2 功能

[root@VM-12-17-centos lesson3]# whereis my
my: /etc/my.cnf
[root@VM-12-17-centos lesson3]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

        搜索包含关键字的文档

15 grep指令

15.1 语法

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

15.2 功能

在文件中搜索字符串,将找到的行打印出来
[root@VM-12-17-centos lesson3]# grep '88' mylog.txt
hello 88
hello 188
hello 288
hello 388
hello 488
hello 588
hello 688
hello 788
hello 880
hello 881
hello 882
hello 883
hello 884
hello 885
hello 886
hello 887
hello 888
hello 889
hello 988

15.3 常用选项:

-i :忽略大小写的不同,所以大小写视为相同
[root@VM-12-17-centos lesson3]# cat my.txt
aaaaa
bbbb
cccc
ddddd
eeeee
AAAA
BBBB
CCC
DDD
EEEE
[root@VM-12-17-centos lesson3]# grep -i 'aaa' my.txt
aaaaa
AAAA
-n :顺便输出行号
[root@VM-12-17-centos lesson3]# grep -n '88' mylog.txt
89:hello 88
189:hello 188
289:hello 288
389:hello 388
489:hello 488
589:hello 588
689:hello 688
789:hello 788
881:hello 880
882:hello 881
883:hello 882
884:hello 883
885:hello 884
886:hello 885
887:hello 886
888:hello 887
889:hello 888
890:hello 889
989:hello 988

-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
[root@VM-12-17-centos lesson3]# cat my.txt
aaaaa
bbbb
cccc
ddddd
eeeee
AAAA
BBBB
CCC
DDD
EEEE
[root@VM-12-17-centos lesson3]# grep -v 'aaa' my.txt
bbbb
cccc
ddddd
eeeee
AAAA
BBBB
CCC
DDD
EEEE

-ivn 显示不含‘aaa'(包括大小写)的内容及行号

[root@VM-12-17-centos lesson3]# grep -ivn 'aaa' my.txt
2:bbbb
3:cccc
4:ddddd
5:eeeee
7:BBBB
8:CCC
9:DDD
10:EEEE
注意:
双引号单引号都可“最好带上引号,防止例如’aaa aaa含空格的内容不能正常识别
[root@VM-12-17-centos lesson3]# cat my.txt |grep aaa aaa
grep: aaa: No such file or directory
[root@VM-12-17-centos lesson3]# cat my.txt |grep "aaa aaa"

16 top 

16.1 语法

top

16.2 功能

任务管理器

17 zip/unzip指令

17.1 语法 

 zip 压缩文件.zip 目录或文件        zip test2.zip test2/*

如果打包的是目录及其包含的所有文件 zip -r temp.zip temp

unzip test2.zip test2 (默认解压到当前路径)

解压到tmp目录: unzip test2.zip -d /tmp

17.2 功能

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

zip文件​解压到某目录

17.3 常用选项

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

17.4 举例

        将test2目录压缩:zip test2.zip test2/*           zip -r temp.zip temp

[root@VM-12-17-centos ~]# tree lesson3
lesson3
|-- a.out
|-- mylog.txt
|-- my.txt
`-- test.c

0 directories, 4 files
[root@VM-12-17-centos ~]# ls
dir  lesson3  newname.txt  yum
[root@VM-12-17-centos ~]# mv -p dir lesson3
mv: invalid option -- 'p'
Try 'mv --help' for more information.
[root@VM-12-17-centos ~]# mv dir lesson3
[root@VM-12-17-centos ~]# tree lesson3
lesson3
|-- a.out
|-- dir
|   `-- d1
|       `-- d2
|           `-- d3
|-- mylog.txt
|-- my.txt
`-- test.c

4 directories, 4 files
[root@VM-12-17-centos ~]# cd /home
[root@VM-12-17-centos home]# ls
juzi  lesson3  lesson3.zip  lighthouse
[root@VM-12-17-centos home]# rm -rf lesson3
[root@VM-12-17-centos home]# ls
juzi  lesson3.zip  lighthouse
[root@VM-12-17-centos home]# rm lesson3* -rf
[root@VM-12-17-centos home]# ll
total 8
drwx------ 2 juzi       juzi       4096 Jan  4 00:55 juzi
drwx------ 5 lighthouse lighthouse 4096 Jan  3 15:56 lighthouse
[root@VM-12-17-centos home]# ls
juzi  lighthouse
[root@VM-12-17-centos home]# cd -
/root
[root@VM-12-17-centos ~]# ls
lesson3  newname.txt  yum
[root@VM-12-17-centos ~]# zip -r lesson3.zip lesson3
  adding: lesson3/ (stored 0%)
  adding: lesson3/test.c (deflated 3%)
  adding: lesson3/mylog.txt (deflated 80%)
  adding: lesson3/dir/ (stored 0%)
  adding: lesson3/dir/d1/ (stored 0%)
  adding: lesson3/dir/d1/d2/ (stored 0%)
  adding: lesson3/dir/d1/d2/d3/ (stored 0%)
  adding: lesson3/my.txt (deflated 31%)
  adding: lesson3/a.out (deflated 72%)
[root@VM-12-17-centos ~]# ll
total 12
drwxr-xr-x 3 root root 4096 Jan  6 15:59 lesson3
-rw-r--r-- 1 root root 5878 Jan  6 16:11 lesson3.zip
-rw-r--r-- 1 root root    0 Jan  5 21:11 newname.txt
-rw-r--r-- 1 root root    0 Jan  3 21:44 yum
[root@VM-12-17-centos ~]# mv lesson3.zip ~
mv: ‘lesson3.zip’ and ‘/root/lesson3.zip’ are the same file
[root@VM-12-17-centos ~]# mv lesson3.zip /home
[root@VM-12-17-centos ~]# cd home
-bash: cd: home: No such file or directory
[root@VM-12-17-centos ~]# pwd
/root
[root@VM-12-17-centos ~]# ls
lesson3  newname.txt  yum
[root@VM-12-17-centos ~]# cd /home
[root@VM-12-17-centos home]# ls
juzi  lesson3.zip  lighthouse
[root@VM-12-17-centos home]# unzip lesson3.zip
Archive:  lesson3.zip
   creating: lesson3/
  inflating: lesson3/test.c          
  inflating: lesson3/mylog.txt       
   creating: lesson3/dir/
   creating: lesson3/dir/d1/
   creating: lesson3/dir/d1/d2/
   creating: lesson3/dir/d1/d2/d3/
  inflating: lesson3/my.txt          
  inflating: lesson3/a.out           
[root@VM-12-17-centos home]# ll
total 20
drwx------ 2 juzi       juzi       4096 Jan  4 00:55 juzi
drwxr-xr-x 3 root       root       4096 Jan  6 15:59 lesson3
-rw-r--r-- 1 root       root       5878 Jan  6 16:11 lesson3.zip
drwx------ 5 lighthouse lighthouse 4096 Jan  3 15:56 lighthouse
[root@VM-12-17-centos home]# tree lesson3
lesson3
|-- a.out
|-- dir
|   `-- d1
|       `-- d2
|           `-- d3
|-- mylog.txt
|-- my.txt
`-- test.c

        unzip              unzip test2.zip test2 (默认解压到当前路径)  

                               解压到tmp目录:unzip test2.zip -d /tmp

[root@VM-12-17-centos home]# unzip lesson3.zip
Archive:  lesson3.zip
   creating: lesson3/
  inflating: lesson3/test.c          
  inflating: lesson3/mylog.txt       
   creating: lesson3/dir/
   creating: lesson3/dir/d1/
   creating: lesson3/dir/d1/d2/
   creating: lesson3/dir/d1/d2/d3/
  inflating: lesson3/my.txt          
  inflating: lesson3/a.out           
[root@VM-12-17-centos home]# ll
total 20
drwx------ 2 juzi       juzi       4096 Jan  4 00:55 juzi
drwxr-xr-x 3 root       root       4096 Jan  6 15:59 lesson3
-rw-r--r-- 1 root       root       5878 Jan  6 16:11 lesson3.zip
drwx------ 5 lighthouse lighthouse 4096 Jan  3 15:56 lighthouse

18 date显示

date 指定格式显示时间: date +%Y:%m:%d
date 用法: date [OPTION]... [+FORMAT]

18.1 在显示方面

使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中常用的标记列表如下
%H : 小时(00..23)
%M : 分钟(00..59)
%S : 秒(00..61)
%X : 相当于 %H:%M:%S
%d : 日 (01..31)
%m : 月份 (01..12)
%Y : 完整年份 (0000..9999)
%F : 相当于 %Y-%m-%d
[root@VM-12-17-centos lesson3]# date +%Y
2023
[root@VM-12-17-centos lesson3]# date +%Y-%m-%d/%H:%M:%S
2023-01-06/19:46:22

18.2 在设定时间方面

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″ //这样可以设置全部时间

18.3 时间戳

时间 -> 时间戳: date +%s
时间戳 -> 时间: date -d@1508749502
Unix 时间戳(英文为 Unix epoch, Unix time, POSIX time Unix timestamp )是从 1970 1 1 日( UTC/GMT 的午夜)开始所经过的秒数,不考虑闰秒。由于时差的问题,北京时间是上午8:00;
[root@VM-12-17-centos lesson3]# cat test.c
#include <stdio.h>
int main()
{

	printf("time:%u\n",(unsigned int)time(NULL));
	return 0;
}
[root@VM-12-17-centos lesson3]# gcc test.c
[root@VM-12-17-centos lesson3]# ls
a.out  dir  mylog.txt  my.txt  test.c
[root@VM-12-17-centos lesson3]# ./a.out
time:1673011484
[root@VM-12-17-centos lesson3]# ./a.out
time:1673011487
[root@VM-12-17-centos lesson3]# ./a.out
time:1673011488
[root@VM-12-17-centos lesson3]# ./a.out
time:1673011488
[root@VM-12-17-centos lesson3]# ./a.out
time:1673011489
[root@VM-12-17-centos lesson3]# ./a.out
time:1673011491

c程序种随机数,即srand的参数是时间戳。

19 cal

19.1 cal指令

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

19.2 命令格式

cal [ 参数 ][ 月份 ][ 年份 ]

19.3 功能

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

19.4 常用选项

-3 显示系统前一个月,当前月,下一个月的月历
[root@VM-12-17-centos lesson3]# cal -3
    December 2022         January 2023          February 2023   
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
             1  2  3   1  2  3  4  5  6  7            1  2  3  4
 4  5  6  7  8  9 10   8  9 10 11 12 13 14   5  6  7  8  9 10 11
11 12 13 14 15 16 17  15 16 17 18 19 20 21  12 13 14 15 16 17 18
18 19 20 21 22 23 24  22 23 24 25 26 27 28  19 20 21 22 23 24 25
25 26 27 28 29 30 31  29 30 31              26 27 28            

-j  显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
-y  显示当前年份的日历
[root@VM-12-17-centos lesson3]# cal 2023
                               2023                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4             1  2  3  4
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    5  6  7  8  9 10 11
15 16 17 18 19 20 21   12 13 14 15 16 17 18   12 13 14 15 16 17 18
22 23 24 25 26 27 28   19 20 21 22 23 24 25   19 20 21 22 23 24 25
29 30 31               26 27 28               26 27 28 29 30 31

        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1       1  2  3  4  5  6                1  2  3
 2  3  4  5  6  7  8    7  8  9 10 11 12 13    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   14 15 16 17 18 19 20   11 12 13 14 15 16 17
16 17 18 19 20 21 22   21 22 23 24 25 26 27   18 19 20 21 22 23 24
23 24 25 26 27 28 29   28 29 30 31            25 26 27 28 29 30
30
        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
                   1          1  2  3  4  5                   1  2
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    3  4  5  6  7  8  9
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   10 11 12 13 14 15 16
16 17 18 19 20 21 22   20 21 22 23 24 25 26   17 18 19 20 21 22 23
23 24 25 26 27 28 29   27 28 29 30 31         24 25 26 27 28 29 30
30 31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7             1  2  3  4                   1  2
 8  9 10 11 12 13 14    5  6  7  8  9 10 11    3  4  5  6  7  8  9
15 16 17 18 19 20 21   12 13 14 15 16 17 18   10 11 12 13 14 15 16
22 23 24 25 26 27 28   19 20 21 22 23 24 25   17 18 19 20 21 22 23
29 30 31               26 27 28 29 30         24 25 26 27 28 29 30
                                              31

20 命令行管道

cat mylog.txt | wc -l

21 sort

21.1 排序

        按ASCII码值排序,默认升序排序, -r 降序排序

[root@VM-12-17-centos lesson3]# cat my.txt
1111111
222222
33333333333
666666
777777777
66666

8888888

999999
9999999
66666666
4444444
3333333
[root@VM-12-17-centos lesson3]# sort my.txt


1111111
222222
3333333
33333333333
4444444
66666
666666
66666666
777777777
8888888
999999
9999999

21.2 去重排序

        uniq去重

[root@VM-12-17-centos lesson3]# sort my.txt


1111111
1111111
222222
222222
3333333
33333333333
4444444
66666
666666
66666666
777777777
8888888
999999
9999999
[root@VM-12-17-centos lesson3]# sort my.txt | uniq

1111111
222222
3333333
33333333333
4444444
66666
666666
66666666
777777777
8888888
999999
9999999

22 tar指令

打包/解包,不打开它,直接看内容

tar [-cxtzjvf]  文件与目录  ....

22.1 参数

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

22.2 举例

        tar -czf dst src         
 src打包放入新建的压缩文件-c,并压缩-z,生成dst 压缩文件
(例如 生成new.tgz)
[root@VM-12-17-centos lesson4]# ll
total 4
drwxr-xr-x 3 root root 4096 Jan  7 08:42 temp
[root@VM-12-17-centos lesson4]# tar -czf new.tgz temp
[root@VM-12-17-centos lesson4]# ll
total 8
-rw-r--r-- 1 root root  252 Jan  7 09:08 new.tgz
drwxr-xr-x 3 root root 4096 Jan  7 08:42 temp
[root@VM-12-17-centos lesson4]# rm -rf temp
[root@VM-12-17-centos lesson4]# ll
total 4
-rw-r--r-- 1 root root 252 Jan  7 09:08 new.tgz

  解压      tar -xzf new.tgz 

[root@VM-12-17-centos lesson4]# tar -xzf new.tgz 
[root@VM-12-17-centos lesson4]# ll
total 8
-rw-r--r-- 1 root root  252 Jan  7 09:08 new.tgz
drwxr-xr-x 3 root root 4096 Jan  7 08:42 temp

解包到指定路径下 tar xzf new.tgz -C指定路径


[root@VM-12-17-centos lesson4]# ll
total 4
-rw-r--r-- 1 root root 252 Jan  7 09:08 new.tgz
[root@VM-12-17-centos lesson4]# tar xzf new.tgz -C..
[root@VM-12-17-centos lesson4]# ls ..
lesson3  lesson4  newname.txt  temp  yum

不解压,直接查看内容 tar -ztvf new.tgz

[root@VM-12-17-centos lesson4]# ll
total 4
-rw-r--r-- 1 root root 252 Jan  7 09:08 new.tgz
[root@VM-12-17-centos lesson4]# tar -ztvf new.tgz
drwxr-xr-x root/root         0 2023-01-07 08:42 temp/
-rw-r--r-- root/root         0 2023-01-07 08:40 temp/test.c
-rw-r--r-- root/root        15 2023-01-07 08:45 temp/test.txt
drwxr-xr-x root/root         0 2023-01-07 08:42 temp/dir/
-rw-r--r-- root/root         0 2023-01-07 08:39 temp/test.py
-rw-r--r-- root/root         0 2023-01-07 08:40 temp/test.sh
-rw-r--r-- root/root         0 2023-01-07 08:40 temp/test.cpp
[root@VM-12-17-centos lesson4]# ll
total 4
-rw-r--r-- 1 root root 252 Jan  7 09:08 new.tgz

23 热键

23.1 ctrl+c

 强制终止前台影响我输入指令的程序

23.2 [Ctrl]-d

        按键---通常代表着:『键盘输入结束(End Of File, EOF 戒 End OfInput)』的意思;另外,也可以用来取代exit,自动退出

23.3 上下按键

        翻阅历史指令

23.4 ctrl +r

        在历史命令中进行搜索

23.5 拓展 history命令   

        保存了历史指令

[root@VM-12-17-centos ~]# history > history.txt
[root@VM-12-17-centos ~]# less history.txt

 

24 bc

方便计算

[root@VM-12-17-centos lesson4]# 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
23.4+23.7
47.1
quit
[root@VM-12-17-centos lesson4]# echo "1+2+3+4" | bc
10

25 uname –r指令:

25.1 语法

uname [ 选项

25.2 功能

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

25.3 补充说明

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

25.4 常用选项

        -a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
[root@VM-12-17-centos lesson4]# uname -a
Linux VM-12-17-centos 3.10.0-1160.71.1.el7.x86_64 #1 SMP Tue Jun 28 15:37:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
        -r显示主要的信息
[root@VM-12-17-centos lesson4]# uname -r
3.10.0-1160.71.1.el7.x86_64

        cat /etc/redhat-release

[root@VM-12-17-centos lesson4]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

26 shutdown关机

语法:shutdown [选项] ** 常见选项:**
-h : 将系统的服务停掉后,立即关机。
-r : 在将系统的服务停掉之后就重新启动
-t sec : -t 后面加秒数,亦即『过几秒后关机』的意思
(云服务器没必要关机)

27 reboot 重启

28 扩展

以下命令作为扩展 :
◆ 安装和登录命令 login shutdown halt reboot install mount umount chsh exit last
◆ 文件处理命令: file mkdir grep dd find mv ls diff cat ln
◆ 系统管理相关命令 df top free quota at lp adduser groupadd kill crontab
◆ 网络操作命令 ifconfig ip ping netstat telnet ftp route rlogin rcp finger mail nslookup
◆ 系统安全相关命令: passwd su umask chgrp chmod chown chattr sudo ps who
◆ 其它命令: tar unzip gunzip unarj mtools man unendcode uudecode

评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值