Linux常用命令(一)

2019-03-10  本文已影响0人  z1挂东南

以下所有命令演示的环境是CentOS Linux 7

GNOME:版本 3.28.2
OS type:64-bit

一、简单命令

1. who

列出所有正在使用系统的用户、所用终端名和登录到系统的时间。

[test@localhost ~]$ who
test     :0           2019-03-09 14:45 (:0)
test     pts/0        2019-03-09 14:50 (:0)
[test@localhost ~]$ who am i
test     pts/0        2019-03-09 14:50 (:0)
[test@localhost ~]$ whoami
test

2. echo

将命令行中的参数显示到屏幕上。

[test@localhost ~]$ echo who am i
who am i
[test@localhost ~]$ echo who     am      i
who am i
[test@localhost ~]$ echo 'who    am      i'
who    am      i

3. date

在屏幕上显示或设置系统的日期和时间。如果没有参数,只显示系统当前的日期和时间。

部分参数:
-d:显示字符串所指的日期与时间。
-s:根据字符串来设置日期与时间。

显示当前时间:

[test@localhost ~]$ date
2019年 03月 09日 星期六 15:10:11 CST

显示当前时间,但是用户自定义格式:

[test@localhost ~]$ date "+%Y-%m-%d %H:%M:%S"
2019-03-09 21:15:11

显示前一天现在的时间:

[test@localhost ~]$ date -d "- 1 day"
2019年 03月 08日 星期五 21:17:03 CST

4. cal

可以显示公元1~9999年中任意一年或者任意一个月的日历。

不带参数,显示当前月份:

[test@localhost ~]$ cal
      三月 2019     
日 一 二 三 四 五 六
                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

带一个参数,参数被解释为年份:

[test@localhost ~]$ cal 2019
                               2019                               

        一月                   二月                   三月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
       1  2  3  4  5                   1  2                   1  2
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    3  4  5  6  7  8  9
13 14 15 16 17 18 19   10 11 12 13 14 15 16   10 11 12 13 14 15 16
20 21 22 23 24 25 26   17 18 19 20 21 22 23   17 18 19 20 21 22 23
27 28 29 30 31         24 25 26 27 28         24 25 26 27 28 29 30
                                              31
        四月                   五月                   六月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
    1  2  3  4  5  6             1  2  3  4                      1
 7  8  9 10 11 12 13    5  6  7  8  9 10 11    2  3  4  5  6  7  8
14 15 16 17 18 19 20   12 13 14 15 16 17 18    9 10 11 12 13 14 15
21 22 23 24 25 26 27   19 20 21 22 23 24 25   16 17 18 19 20 21 22
28 29 30               26 27 28 29 30 31      23 24 25 26 27 28 29
                                              30
        七月                   八月                   九月        
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
    1  2  3  4  5  6                1  2  3    1  2  3  4  5  6  7
 7  8  9 10 11 12 13    4  5  6  7  8  9 10    8  9 10 11 12 13 14
14 15 16 17 18 19 20   11 12 13 14 15 16 17   15 16 17 18 19 20 21
21 22 23 24 25 26 27   18 19 20 21 22 23 24   22 23 24 25 26 27 28
28 29 30 31            25 26 27 28 29 30 31   29 30

        十月                  十一月                 十二月       
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
       1  2  3  4  5                   1  2    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    8  9 10 11 12 13 14
13 14 15 16 17 18 19   10 11 12 13 14 15 16   15 16 17 18 19 20 21
20 21 22 23 24 25 26   17 18 19 20 21 22 23   22 23 24 25 26 27 28
27 28 29 30 31         24 25 26 27 28 29 30   29 30 31

带两个参数,第一个参数被解释为月份,第二个参数被解释为年份,年份不能简写,必须是4位数字:

[test@localhost ~]$ cal 7 2018
      七月 2018     
日 一 二 三 四 五 六
 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

5. clear

用于清除屏幕上的信息,类似DOS里的CLS命令。

6. passwd

决定用户是否可以修改分派给它的密码,用户必须多长时间更改更改一下自己的密码,以及用户的密码中可以使用什么样的字符串。

[test@localhost ~]$ passwd
更改用户 test 的密码 。
为 test 更改 STRESS 密码。
(当前)UNIX 密码:
新的 密码:

二、文件操作命令

1. cat

将文件里的内容输出到屏幕上,类似DOS里的TYPE命令。

常用参数:
-n,--number 由 1 开始对所有输出的行进行编号
-b,--number-nonblank 由 1 开始对所有输出的非空行进行编号
-s,--squeeze-blank 将多个相邻的空行合并成一个相邻的空行

直接打开文件:

[root@localhost 文档]# cat file.txt
zhe shi yi ge ce shi wen dang
xia mian shi yi ge kong hang

yi gong you 5 hang
zhe shi zui hou yi hang

-n打开文件:

[root@localhost 文档]# cat -n file.txt
     1  zhe shi yi ge ce shi wen dang
     2  xia mian shi yi ge kong hang
     3  
     4  yi gong you 5 hang
     5  zhe shi zui hou yi hang

-b打开文件:

[root@localhost 文档]# cat -b file.txt
     1  zhe shi yi ge ce shi wen dang
     2  xia mian shi yi ge kong hang

     3  yi gong you 5 hang
     4  zhe shi zui hou yi hang

将两个文件合并为一个文件:

[root@localhost 文档]# cat file.txt
zhe shi yi ge ce shi wen dang
xia mian shi yi ge kong hang

yi gong you 5 hang
zhe shi zui hou yi hang
[root@localhost 文档]# cat file2.txt
zhe ye shi yi ge ce shi wen dang
zhe ge wen dang mei you kong hang
suo yi yi gong si hang
zhe shi zui hou yi hang
[root@localhost 文档]# cat file.txt file2.txt>file3.txt
[root@localhost 文档]# cat file3.txt
zhe shi yi ge ce shi wen dang
xia mian shi yi ge kong hang

yi gong you 5 hang
zhe shi zui hou yi hang
zhe ye shi yi ge ce shi wen dang
zhe ge wen dang mei you kong hang
suo yi yi gong si hang
zhe shi zui hou yi hang

2. more

如果文件里的内容过大,一个屏幕放不下,再来瓶雪花我们勇闯天涯...不好意思串了。more命令每次只显示一屏幕的内容,剩下的内容通过用户手动一点一点的显示出来。

命令格式:
more [选项] 文件名

常用参数:
-num,指定一个整数表示一屏显示多少行。
-s,将文件中连续的空行压缩成一个空行显示。
+num,从行号num开始显示。

Space键,显示文本的下一屏内容。
Enter键,显示文本的下一行内容。
B键,显示上一屏内容。
Q键,退出more指令。

没用more打开一个四十一行的文档:

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

[root@localhost 文档]# 

使用more命令后打开同样的文档:

00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
--More--

打开文件,一次显示17行,从第20行开始显示(文件第一行是00):

[root@localhost 文档]# more -17 +20 file4.txt
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
--More--(87%)

3. less

与more命令相反,或者说是改良的more命令。more命令只能想下翻页,而less命令既能向上翻页也能向下翻页。显示文件时用PageUp键向上翻页,用PageDown键向下翻页。使用方法与more基本一致,只是最后要用Q键退出,这里就不写例子了。

4. head

在屏幕上显示指定文件的开头若干行,默认值是10。

命令格式:
head [选项] 文件名

常用参数:
-c, --bytes=SIZE 显示每个文件的前SIZE个字节内容。
-n,--lines=NUMBER 显示每个文件的前NUMBER行内容。
-q,--quiet,--silent 不显示文件的标题
-v,--verbose 总是显示文件的标题

显示文件前6个字节:

[root@localhost 文档]# head -c 6 file4.txt
00
01

显示文件前5行内容,并显示标题:

[root@localhost 文档]# head -n 5 -v file4.txt
==> file4.txt <==
00
01
02
03
04

5. tail

与head命令相反,在屏幕上显示指定文件的末尾若干行,默认是文件的末尾10行。如果没有指定文件名则读取标准输入,按Ctrl+C结束。

常用参数:
-f 当文件增长时输出附加的数据。
-c 输出文件最后c个字节。
-n 输出文件最后n行。
-q 不输出文件名。
-v 输出文件名。

给定的文件名不止一个,则在显示的每个文件前加一个文件名:

[root@localhost 文档]# tail file.txt file2.txt
==> file.txt <==
zhe shi yi ge ce shi wen dang
xia mian shi yi ge kong hang

yi gong you 5 hang
zhe shi zui hou yi hang

==> file2.txt <==
zhe ye shi yi ge ce shi wen dang
zhe ge wen dang mei you kong hang
suo yi yi gong si hang
zhe shi zui hou yi hang

6. touch

修改指定文件的时间标签或者创建一个空文件。touch命令会修改指定文件的时间标签,把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来。如果该文件尚未存在,则创建一个新的文件夹。

命令格式:
touch [选项] 文件名

常用参数:
-a,仅改变指定文件的存取时间。
-c,不创建任何文件。
-m,仅改变文件的修改时间。
-t,使用STAMP指定的时间标签,而不是系统当前的时间。

改变文件时间标签:

[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月   9 22:44 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 3月   9 22:43 file.txt
[root@localhost 文档]# touch  file.txt
[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月   9 22:44 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 3月  10 13:22 file.txt

使用-a和-m参数(-a并没有变化):

[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月   9 22:44 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 3月  10 13:22 file.txt
[root@localhost 文档]# touch -a file2.txt
[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月   9 22:44 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 3月  10 13:22 file.txt
[root@localhost 文档]# touch -m file2.txt
[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月  10 13:25 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 3月  10 13:22 file.txt

使用-t参数修改为用户指定时间:

[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月  10 13:25 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 3月  10 13:22 file.txt
[root@localhost 文档]# touch -t 201807170631 file.txt
[root@localhost 文档]# ls -l
总用量 16
-rw-r--r--. 1 root root 114 3月  10 13:25 file2.txt
-rw-r--r--. 1 root root 217 3月   9 22:54 file3.txt
-rw-r--r--. 1 root root 124 3月   9 23:04 file4.txt
-rw-r--r--. 1 root root 103 7月  17 2018 file.txt

快到期末考试的你,一定会感谢平时做笔记的自己。

上一篇 下一篇

猜你喜欢

热点阅读