Linux基本指令(一)(2)

简介: Linux常用基本指令前言:linux不止命令,命令这是工具文件1. lsls [选项] [目录或文件]作用:列出该目录下所有的子目录与文件常见选项:-a 列出目录下的所有文件,包括以.开头的隐含文件

17. head

head [选项] [文件]

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

-n<行数> 显示的行数

//原本myfile.txt文件中有一百行,head默认显示前十行
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# head myfile.txt 
hello 0
hello 1
hello 2
hello 3
hello 4
hello 5
hello 6
hello 7
hello 8
hello 9
[root@VM-12-12-centos instruction1]# 
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# head -n3 myfile.txt 
hello 0
hello 1
hello 2
[root@VM-12-12-centos instruction1]# 

18. tail

tail [必要选项] [选择选项] [文件]

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

-f : 循环读取

-n<行数> 显示行数

//myfile.txt文件中有100行hello
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# tail myfile.txt 
hello 91
hello 92
hello 93
hello 94
hello 95
hello 96
hello 97
hello 98
hello 99
hello 100
//默认是文本最后十行

补充:| : 此符号表示命令行管道

root@VM-12-12-centos instruction1]# clear
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt | wc -l
101
[root@VM-12-12-centos instruction1]#

怎么理解管道呢?

77ac5beec44b21f3181787d81b9a30b7.png

//管道用途:显示大文本中的中间内容或任意想提取的内容
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 900 Nov 17 23:25 myfile.txt
[root@VM-12-12-centos instruction1]# wc -l myfile.txt 
101 myfile.txt
[root@VM-12-12-centos instruction1]# cat myfile.txt | head -n50 | tail -n10
hello 40
hello 41
hello 42
hello 43
hello 44
hello 45
hello 46
hello 47
hello 48
hello 49
[root@VM-12-12-centos instruction1]# 

19. date

data [选项] [+format]

作用:显示时间

  • %H : 小时
  • %M : 分钟
  • %S : 秒
  • %X : 相当于 %H:%M:%S
  • %d : 日
  • %m : 月份
  • %Y : 完整年份
  • %F :相当于%Y-%m-%d
[root@VM-12-12-centos instruction1]# date +%F/%X
2022-11-18/12:00:31 AM

补充:时间戳

时间->时间戳:date +%s

时间戳->时间:date -d@时间戳

[root@VM-12-12-centos ~]# date +%s
1668781140
[root@VM-12-12-centos ~]# date -d@1668781140
Fri Nov 18 22:19:00 CST 2022
[root@VM-12-12-centos ~]# 

20. cal

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

作用:用于查看日历等时间信息

-y 显示当前年份的日历

-j 显示当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)

[root@VM-12-12-centos ~]# cal -y 2019
                               2019                               
       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                   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
        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  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
        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  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
       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                   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
[root@VM-12-12-centos ~]# 

21. sort

sort [选项] 文件名

作用:按照每行的第一个字母依次向后对齐比较ASCII码值(类似strcmp函数),默认升序

-r 按照降序排序

[root@VM-12-12-centos instruction1]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 18 22:26 test.txt
[root@VM-12-12-centos instruction1]# nano test.txt
[root@VM-12-12-centos instruction1]# cat test.txt 
1111
111
2222
22222
3333
4444
7777
9999
[root@VM-12-12-centos instruction1]# sort test.txt 
111
1111
2222
22222
3333
4444
7777
9999
[root@VM-12-12-centos instruction1]#

22. uniq

作用:相邻内容去重

[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 40 Nov 18 22:29 test.txt
[root@VM-12-12-centos instruction1]# nano test.txt
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# clear
[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# cat test.txt 
1111
111
2222
22222
3333
4444
7777
9999
9999
0000
0000
1111
[root@VM-12-12-centos instruction1]# uniq test.txt 
1111
111
2222
22222
3333
4444
7777
9999
0000
1111
[root@VM-12-12-centos instruction1]# sort test.txt | uniq
0000
111
1111
2222
22222
3333
4444
7777
9999
[root@VM-12-12-centos instruction1]# 

23. find

find pathname -options filename

作用:用于在文件树中查看文件

-name 按照文件名查找文件

[root@VM-12-12-centos ~]# tree
.
`-- study_2022_11_11
    |-- instruction1
    |   `-- test.txt
    `-- README.txt
2 directories, 2 files
[root@VM-12-12-centos ~]# cd study_2022_11_11/instruction1/
[root@VM-12-12-centos instruction1]# pwd
/root/study_2022_11_11/instruction1
[root@VM-12-12-centos instruction1]# find ~ -name test.txt 
/root/study_2022_11_11/instruction1/test.txt
[root@VM-12-12-centos instruction1]# 

24. which

作用:搜索指令路径

[root@VM-12-12-centos instruction1]# which ls
alias ls='ls --color=auto'
  /usr/bin/ls
[root@VM-12-12-centos instruction1]# which man
/usr/bin/man
[root@VM-12-12-centos instruction1]# which touch
/usr/bin/touch
[root@VM-12-12-centos instruction1]# which echo
/usr/bin/echo
[root@VM-12-12-centos instruction1]# 

25. whereis

作用:查找文件

[root@VM-12-12-centos instruction1]# whereis test
test: /usr/bin/test /usr/share/man/man1/test.1.gz /usr/share/man/man1p/test.1p.gz
[root@VM-12-12-centos instruction1]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

26. alias

作用:给特定命令起别名

[root@VM-12-12-centos instruction1]# alias myls='ls -a -l -i -n'
[root@VM-12-12-centos instruction1]# myls
total 12
656403 drwxr-xr-x 2 0 0 4096 Nov 18 22:45 .
656417 drwxr-xr-x 3 0 0 4096 Nov 16 21:29 ..
656463 -rw-r--r-- 1 0 0   61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# which myls
alias myls='ls -a -l -i -n'
  /usr/bin/ls
[root@VM-12-12-centos instruction1]# 

27. grep

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

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

-n 输出行号

-v 反向选择

-i 忽略大小写

[root@VM-12-12-centos instruction1]# ll
total 4
-rw-r--r-- 1 root root 61 Nov 18 22:32 test.txt
[root@VM-12-12-centos instruction1]# cat test.txt 
1111
111
2222
22222
3333
4444
7777
9999
9999
0000
0000
1111
[root@VM-12-12-centos instruction1]# grep '11' test.txt 
1111
111
1111
[root@VM-12-12-centos instruction1]# grep -v '11' test.txt 
2222
22222
3333
4444
7777
9999
9999
0000
0000
[root@VM-12-12-centos instruction1]# 

28. zip/unzip

zip 压缩文件.zip 目录或文件

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

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

[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 18 23:02 test
[root@VM-12-12-centos instruction1]# zip test.zip .
zip error: Nothing to do! (test.zip) //err operation
[root@VM-12-12-centos instruction1]# zip test.zip test
  adding: test/ (stored 0%)
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:02 test
-rw-r--r-- 1 root root  160 Nov 18 23:03 test.zip
[root@VM-12-12-centos instruction1]# tree
.
|-- test
|   `-- test.txt
`-- test.zip
1 directory, 2 files
[root@VM-12-12-centos instruction1]# 
//打包目录需要递归打包
[root@VM-12-12-centos instruction1]# ll
total 4
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
[root@VM-12-12-centos instruction1]# zip -r test.zip test
  adding: test/ (stored 0%)
  adding: test/test.txt (deflated 23%)
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root  341 Nov 18 23:10 test.zip
[root@VM-12-12-centos instruction1]# tree
.
|-- test
|   `-- test.txt
`-- test.zip
1 directory, 2 files
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root  341 Nov 18 23:10 test.zip
[root@VM-12-12-centos instruction1]# mkdir myfile
[root@VM-12-12-centos instruction1]# ll
total 12
drwxr-xr-x 2 root root 4096 Nov 18 23:11 myfile
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root  341 Nov 18 23:10 test.zip
[root@VM-12-12-centos instruction1]# mv test.zip myfile
[root@VM-12-12-centos instruction1]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:11 myfile
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
[root@VM-12-12-centos instruction1]# tree
.
|-- myfile
|   `-- test.zip
`-- test
    `-- test.txt
2 directories, 2 files
[root@VM-12-12-centos instruction1]# cd myfile/
[root@VM-12-12-centos myfile]# ll
total 4
-rw-r--r-- 1 root root 341 Nov 18 23:10 test.zip
[root@VM-12-12-centos myfile]# unzip test.zip 
Archive:  test.zip
   creating: test/
  inflating: test/test.txt           
[root@VM-12-12-centos myfile]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 18 23:06 test
-rw-r--r-- 1 root root  341 Nov 18 23:10 test.zip
[root@VM-12-12-centos myfile]# tree
.
|-- test
|   `-- test.txt
`-- test.zip
1 directory, 2 files
[root@VM-12-12-centos myfile]# 
//递归处理后就拿到了目录中的文件

补充:

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

29. tar

tar [-cxtzjvf] 文件与目录

-c : 创建一个压缩文件

-x : 解开一个压缩文件

-t : 查看tarfile里面文件

-z : 是否同时具有gzip的属性以及是否同时需要用gzip压缩

-f : 使用档名,在f之后立即接档名(建立f选项放在所有选项后)

-v : 压缩的过程中显示文件

-C : 解压到指定目录(和zip中的-d选项一样作用)

[yinhan@VM-12-12-centos trunk]$ ll
total 4
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
[yinhan@VM-12-12-centos trunk]$ tar -czf tar.tgz tar
[yinhan@VM-12-12-centos trunk]$ ll=
total 8
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
-rw-rw-r-- 1 yinhan yinhan  183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tree
.
|-- tar
|   |-- file.txt
|   |-- test.c
|   `-- tmp
`-- tar.tgz
2 directories, 3 files
[yinhan@VM-12-12-centos trunk]$ rm -rf tar
[yinhan@VM-12-12-centos trunk]$ ll
total 4
-rw-rw-r-- 1 yinhan yinhan 183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tar -xzf tar.tgz 
[yinhan@VM-12-12-centos trunk]$ ll
total 8
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
-rw-rw-r-- 1 yinhan yinhan  183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tre
-bash: tre: command not found
[yinhan@VM-12-12-centos trunk]$ tree
.
|-- tar
|   |-- file.txt
|   |-- test.c
|   `-- tmp
`-- tar.tgz
2 directories, 3 files
[yinhan@VM-12-12-centos trunk]$ 

固定格式就是这样压缩用-czf,解压用-xzf

//查看压缩包内容操作
[yinhan@VM-12-12-centos ~]$ ll
total 8
-rw-rw-r-- 1 yinhan yinhan   50 Nov 23 00:26 README.txt
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:53 trunk
[yinhan@VM-12-12-centos ~]$ tree
.
|-- README.txt
`-- trunk
    |-- tar
    |   |-- file.txt
    |   |-- test.c
    |   `-- tmp
    `-- tar.tgz
3 directories, 4 files
[yinhan@VM-12-12-centos ~]$ cd trunk/
[yinhan@VM-12-12-centos trunk]$ ll
total 8
drwxrwxr-x 3 yinhan yinhan 4096 Nov 23 10:51 tar
-rw-rw-r-- 1 yinhan yinhan  183 Nov 23 10:52 tar.tgz
[yinhan@VM-12-12-centos trunk]$ tar -tf tar.tgz
tar/
tar/test.c
tar/file.txt
tar/tmp/
[yinhan@VM-12-12-centos trunk]$ tar -tzvf tar.tgz 
drwxrwxr-x yinhan/yinhan     0 2022-11-23 10:51 tar/
-rw-rw-r-- yinhan/yinhan     0 2022-11-23 10:51 tar/test.c
-rw-rw-r-- yinhan/yinhan     0 2022-11-23 10:51 tar/file.txt
drwxrwxr-x yinhan/yinhan     0 2022-11-23 10:51 tar/tmp/

30. bc

作用:进行运算

[yinhan@VM-12-12-centos trunk]$ echo "1+3+8" | bc
12

31. uname -r

uname [选项]

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

-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类

型,硬件平台类型,操作系统名称

[yinhan@VM-12-12-centos trunk]$ uname -a
Linux VM-12-12-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
[yinhan@VM-12-12-centos trunk]$ 

32. 热键

ctrl + c : 强制终止非正常输入

tab : 补全文件或指令

ctrl + d : 退出的意思

向上或向下键:用来查阅上次或下次的指令

ctrl + insert : 复制

shift + insert : 粘贴

ctrl + r : 在历史指令中搜索

history : 列出所有历史指令

33. 创建私人用户

adduser + 用户 : 添加用户到home目录

passwd + 用户 : 设置密码 (这里设置密码是没有回显的)

userdel -r + 用户 : 删除用户
































目录
打赏
0
0
0
0
0
分享
相关文章
Linux:进程间通信(共享内存详细讲解以及小项目使用和相关指令、消息队列、信号量)
通过上述讲解和代码示例,您可以理解和实现Linux系统中的进程间通信机制,包括共享内存、消息队列和信号量。这些机制在实际开发中非常重要,能够提高系统的并发处理能力和数据通信效率。希望本文能为您的学习和开发提供实用的指导和帮助。
168 20
【Linux】基础指令
本文介绍了Linux操作系统的基本概念及特点,强调了其开源性、多用户多任务处理能力、稳定性和安全性。文章重点讲解了多个Linux基础命令,如ls、cd、touch、mkdir、rm、man、cp、mv、cat、less、find、grep、tar等,旨在帮助初学者快速掌握Linux命令行操作,为后续深入学习奠定基础。
97 0
|
4月前
|
linux-du指令
`du`命令是Linux系统中查看磁盘使用情况的基本工具之一。通过灵活使用 `du`命令的各种选项,可以准确、高效地获取文件和目录的大小信息,有助于系统管理员进行磁盘空间管理。无论是查找大文件、分析目录结构还是排除特定类型的文件,`du`命令都提供了丰富的功能和灵活的配置。理解和掌握 `du`命令的使用,对于维护和优化Linux系统的磁盘空间至关重要。
73 4
|
5月前
|
【Linux第一弹】- 基本指令
【Linux第一弹】- 基本指令
56 1
【Linux】-基本指令(下)
【Linux】-基本指令(下)
【Linux】-基本指令(下)
【Linux】-基本指令(上)
【Linux】-基本指令(上)
Ubuntu学习笔记(三):Linux下操作指令大全
Ubuntu学习笔记,介绍了Linux操作系统中常用的命令和操作,如文件管理、系统信息查看、软件安装等。
70 3
Linux入门1——初识Linux指令
Linux入门1——初识Linux指令
56 0
Linux入门1——初识Linux指令
装杯 之 Linux指令
本文介绍了Linux操作系统的基本命令。首先欢迎各位进入Linux的世界,并展示了`ls`命令,用于列出目录下的文件和子目录。接着介绍了`pwd`命令,用于显示当前目录;`cd`命令,用于切换目录;`mkdir`命令,用于创建目录;`touch`命令,用于创建普通文件或修改文件时间戳;`rmdir`命令,用于删除空目录;以及`rm`命令,用于删除文件或目录。通过这些基本命令,你可以开始探索和管理Linux系统。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等