(1)Linux高级命令简介

Linux高级命令简介

在安装好linux环境以后第一件事情就是去学习一些linux的基本指令,我在这里用的是CentOS7作演示。

首先在VirtualBox上装好Linux以后,启动我们的linux,输入账号密码以后学习第一个指令

简介

ip addr

这个指令是查看我们的ip地址

这个ip地址受很多因素影响,当你连接的网段不一样,比如说我在图书馆连校园网运行这个指令就不会显示这个地址,在宿舍连个人的网络就会显示,大家可以把他配置成静态的ip地址,方便我们以后操作

Centos7 配置静态IP地址_centos7配置静态ip地址和网关-CSDN博客

可以参考这位博主的文章,写的很详细。

在获取到IP地址以后,我们要对他建立远程连接,因为我们直接操作这个小窗口十分不方便,复制粘贴,文件上传等十分受限,在这里我使用MobaXterm来建立SSH连接

显示这样就成功连接了。

输入账号密码就成功登陆了

选择一些基本的配置方便我们的的使用

先学习以下如何查看当前目录下的各个文件

使用dir指令,这这指令在windows中也很常用

复制代码
[root@localhost ~]# dir
anaconda-ks.cfg
[root@localhost ~]#

这里只有一个anaconda-ks.cfg文件

下面我们在学习一个命令 创建文件

touch

例如

复制代码
touch hello.txt

创建一个hello.txt文件

复制代码
[root@localhost ~]# touch hello.txt
[root@localhost ~]# dir
anaconda-ks.cfg  hello.txt
[root@localhost ~]#

这样我们发现新增了一个文件,我们创建的hello.txt文件,下面这是一个删除文件的指令,以我的理解来看rm就是remove移除

rf就是remove file移除文件。

复制代码
rm -rf hello.txt

vi

这是一个十分重要的指令,用来打开我们的文件,比如说我们打开hello.txt

复制代码
vi hello.txt

我们就来到了这样一个界面。**按"i"**就可以在里面写入内容意为insert,就是插入

我们在这里插入一个hello world;下面介绍几个有趣的指令

复制粘贴:

首先按esc键回到命令界面,再按两下y键,按一下p键就会复制粘贴到下面:

删除:

按两下d就可以删除一行,这还是不是很方便,比如说我们想要批量删除,就按两下9,再按两下d就可以实现批量删除。

这是删除之前,我们先对它就行标号,,再按两下9,再按两下d就可以实现批量删除。

当我们想要退出的时候有三种方法

在这之前首先确保自己在指令模式,按一下esc键然后,按shift+:就会在最下面输出一个冒号

复制代码
:wq(保存并退出)
:q(不保存退出)
:q!(强制退出,不保存)

linux文件意外退出

在这里介绍一个文件未保存强制退出的情况

比如说我们在对一个文件就行了编辑但是没有保存就强制退出(这里指的不是!q指令是直接关闭了窗口)

当我们再次用vi指令打开它以后就会显示

他也说明了主要原因就是有一个后缀为.swp的文件的存在,虽然我们点击回车键或者空格键都可以退出这个模式,继续编辑,但是这对强迫症来说是很痛苦的,我们可以尝试删除这个.swp文件

复制代码
[root@localhost ~]# ll -a
总用量 44
dr-xr-x---.  2 root root   174 1月  17 15:43 .
dr-xr-xr-x. 17 root root   224 11月 23 20:37 ..
-rw-------.  1 root root  1258 1月  17 11:51 anaconda-ks.cfg
-rw-------.  1 root root  3370 1月  17 15:40 .bash_history
-rw-r--r--.  1 root root    18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root   176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root   176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root   100 12月 29 2013 .cshrc
-rw-r--r--.  1 root root    85 1月  17 15:30 hello.txt
-rw-r--r--.  1 root root 12288 1月  17 15:40 .hello.txt.swp
-rw-r--r--.  1 root root   129 12月 29 2013 .tcshrc
[root@localhost ~]#

使用ll -a指令我们就可以看到所有被隐藏的文件

我们就找到了.hello.txt.swp文件

老办法remove它

在这里我们打开了复制粘贴的设置只要我们用鼠标左键选中了文件名,右键就可以把他粘贴下来。

复制代码
[root@localhost ~]# rm -rf hello.txt.swp
[root@localhost ~]# ll -a
总用量 32
dr-xr-x---.  2 root root  152 1月  17 15:45 .
dr-xr-xr-x. 17 root root  224 11月 23 20:37 ..
-rw-------.  1 root root 1258 1月  17 11:51 anaconda-ks.cfg
-rw-------.  1 root root 3370 1月  17 15:40 .bash_history
-rw-r--r--.  1 root root   18 12月 29 2013 .bash_logout
-rw-r--r--.  1 root root  176 12月 29 2013 .bash_profile
-rw-r--r--.  1 root root  176 12月 29 2013 .bashrc
-rw-r--r--.  1 root root  100 12月 29 2013 .cshrc
-rw-r--r--.  1 root root   85 1月  17 15:30 hello.txt
-rw-r--r--.  1 root root  129 12月 29 2013 .tcshrc
[root@localhost ~]#

像这样我们就成功的删除了.swp文件。再打开就不用有那个界面了。

查找

在我们回到主页面以后,可以通过cat指令来查看刚才写的内容

复制代码
[root@localhost ~]# cat hello.txt
hello world8
hello world7
hello world6
hello world5
hello world4
[root@localhost ~]#

再介绍一线查找和定位的指令

复制代码
[root@localhost ~]# cat hello.txt
1hello world8
2hello world7
3hello world6
4hello world5
5hello world4
longest String

首先这是待查找文件

我们在命令模式下,输入"/"

比如说"/2"就会自动定位到开头为2的那一行,这就是简单的查找。

在我们查看比较长的文件时,没有下拉框是很痛苦的,设计者也想到了这一点。

在命令模式下,切换到大写模式。按"h"就会定位到第一行,按"g"就会定位到最后一行。

wc

wc指令通常是用来统计单词或者字符个数的指令

首先我们输入help指令来查看一下说明文件

  • -c或--bytes或--chars 只显示Bytes数。

  • -l或--lines 显示行数。

  • -w或--words 只显示字数。

  • --help 在线帮助。

  • --version 显示版本信息。

    [root@localhost ~]# wc -c hello.txt
    85 hello.txt
    [root@localhost ~]# wc -l hello.txt
    6 hello.txt
    [root@localhost ~]# wc -w hello.txt
    12 hello.txt
    [root@localhost ~]#

我们通过仔细阅读帮助文件也可以学到很多东西。

sort

首先也是读一下help文件

要是看他不顺眼可以直接输入clear清屏。

  • -b 忽略每行前面开始出的空格字符。
  • -c 检查文件是否已经按照顺序排序。
  • -d 排序时,处理英文字母、数字及空格字符外,忽略其他的字符。
  • -f 排序时,将小写字母视为大写字母。
  • -i 排序时,除了040至176之间的ASCII字符外,忽略其他的字符。
  • -m 将几个排序好的文件进行合并。
  • -M 将前面3个字母依照月份的缩写进行排序。
  • -n 依照数值的大小排序。
  • -u 意味着是唯一的(unique),输出的结果是去完重了的。
  • -o<输出文件> 将排序后的结果存入指定的文件。
  • -r 以相反的顺序来排序。
  • -t<分隔字符> 指定排序时所用的栏位分隔字符。
  • +<起始栏位>-<结束栏位> 以指定的栏位来排序,范围由起始栏位到结束栏位的前一栏位。
  • --help 显示帮助。
  • --version 显示版本信息。
  • -k field1\[,field2] 按指定的列进行排序。

别看他这么多其实我们常用的也就是这些,甚至有些时候直接sort也就够了。

-n 依照数值的大小排序。

-r 以相反的顺序来排序。

复制代码
[root@localhost ~]# cat num.txt
1
2
3
4
6
10
20
30
21
[root@localhost ~]# sort num.txt
1
10
2
20
21
3
30
4
6
[root@localhost ~]#

sort的排序法更类似于基数排序,把第一个字母排序以后才会继续排序。

想要传统的排序方法也很简单

复制代码
[root@localhost ~]# sort -n num.txt
1
2
3
4
6
10
20
21
30
[root@localhost ~]#

也可以逆序排列

复制代码
[root@localhost ~]# sort -n -r num.txt
30
21
20
10
6
4
3
2
1
[root@localhost ~]#

uniq

学习这个指令,首先查看他的帮助文档

其中我们用的比较多的就是-c和-u两个指令

  1. 不加:返回重复的行
  2. -c:返回重复行数的统计
  3. -u:返回未重复的行

下面演示一下uniq的用法

说明一个工作中会遇到的坑:

复制代码
[root@localhost ~]# cat test.txt
hello
hello
hello
world
hello
hello
hello
hello
[root@localhost ~]# uniq test.txt
hello
world
hello

当我们对无序的文本内容进行排序的时候,就会返回这种奇怪的结果,这个时候,我们需要用到

管道命令:

"|"是Linux管道命令操作符,简称管道符。使用此管道符"|"可以将两个命令分隔开,"|"左边命令的输出就会作为"|"右边命令的输入,此命令可连续使用,第一个命令的输出会作为第二个命令的输入,第二个命令的输出又会作为第三个命令的输入,依此类推。

复制代码
[root@localhost ~]# sort test.txt
hello
hello
hello
hello
hello
hello
hello
world
[root@localhost ~]# sort test.txt|uniq
hello
world

默认返回前10条数据,我们也可以指定

演示:

复制代码
[root@localhost ~]# head -3 num.txt
1
2
3

也可以使用管道命令来先排序后输出

复制代码
[root@localhost ~]# cat num.txt|sort -n|head -3
1
2
[root@localhost ~]# cat num.txt|sort -n|head -4
1
2
3

date

日期命令:获取当前日期。它的帮助文帝其实已经说的很清楚了,因为他返回的默认格式,有时候我们并不理解,所以提供了格式化工具

java 复制代码
[root@localhost ~]# date --help
用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -I[TIMESPEC], --iso-8601[=TIMESPEC]  output date/time in ISO 8601 format.
                            TIMESPEC='date' for date only (the default),
                            'hours', 'minutes', 'seconds', or 'ns' for date
                            and time to the indicated precision.
  -r, --reference=文件          显示文件指定文件的最后修改时间
  -R, --rfc-2822                以RFC 2822格式输出日期和时间
                                例如:2006年8月7日,星期一 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC='date', 'seconds', or 'ns' for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time (UTC)
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出

给定的格式FORMAT 控制着输出,解释序列如下:

  %%    一个文字的 %
  %a    当前locale 的星期名缩写(例如: 日,代表星期日)
  %A    当前locale 的星期名全称 (如:星期日)
  %b    当前locale 的月名缩写 (如:一,代表一月)
  %B    当前locale 的月名全称 (如:一月)
  %c    当前locale 的日期和时间 (如:2005年3月3日 星期四 23:05:25)
  %C    世纪;比如 %Y,通常为省略当前年份的后两位数字(例如:20)
  %d    按月计的日期(例如:01)
  %D    按月计的日期;等于%m/%d/%y
  %e    按月计的日期,添加空格,等于%_d
  %F    完整日期格式,等价于 %Y-%m-%d
  %g    ISO-8601 格式年份的最后两位 (参见%G)
  %G    ISO-8601 格式年份 (参见%V),一般只和 %V 结合使用
  %h    等于%b
  %H    小时(00-23)
  %I    小时(00-12)
  %j    按年计的日期(001-366)
  %k   hour, space padded ( 0..23); same as %_H
  %l   hour, space padded ( 1..12); same as %_I
  %m   month (01..12)
  %M   minute (00..59)
  %n    换行
  %N    纳秒(000000000-999999999)
  %p    当前locale 下的"上午"或者"下午",未知时输出为空
  %P    与%p 类似,但是输出小写字母
  %r    当前locale 下的 12 小时时钟时间 (如:11:11:04 下午)
  %R    24 小时时间的时和分,等价于 %H:%M
  %s    自UTC 时间 1970-01-01 00:00:00 以来所经过的秒数
  %S    秒(00-60)
  %t    输出制表符 Tab
  %T    时间,等于%H:%M:%S
  %u    星期,1 代表星期一
  %U    一年中的第几周,以周日为每星期第一天(00-53)
  %V    ISO-8601 格式规范下的一年中第几周,以周一为每星期第一天(01-53)
  %w    一星期中的第几日(0-6),0 代表周一
  %W    一年中的第几周,以周一为每星期第一天(00-53)
  %x    当前locale 下的日期描述 (如:12/31/99)
  %X    当前locale 下的时间描述 (如:23:13:48)
  %y    年份最后两位数位 (00-99)
  %Y    年份
  %z +hhmm              数字时区(例如,-0400)
  %:z +hh:mm            数字时区(例如,-04:00)
  %::z +hh:mm:ss        数字时区(例如,-04:00:00)
  %:::z                 数字时区带有必要的精度 (例如,-04,+05:30)
  %Z                    按字母表排序的时区缩写 (例如,EDT)

%S返回秒,%s返回时间戳

复制代码
[root@localhost ~]# date +%Y-%m-%d_%H:%M:%S
2025-01-29_15:25:52
[root@localhost ~]# date +"%Y-%m-%d %H:%M:%S"
2025-01-29 15:26:06

当我们要求的返回值里面有空格的时候就需要加引号了,没有空格的时候可以直接返回。

有时候我们需要返回当前时间或者两个时间之间的时间差,我们一般先返回时间戳,再计算他们之间的间隔

java 复制代码
[root@localhost ~]# date --date="2026-01-01 00:00:00"
2026年 01月 01日 星期四 00:00:00 CST
//仔细观察这里的%s前丢了一个空格,需要注意哦。
[root@localhost ~]# date --date="2026-01-01 00:00:00"+%s
date: 无效的日期"2026-01-01 00:00:00+%s"
[root@localhost ~]# date --date="2026-01-01 00:00:00" +%s
1767196800

我们也可以返回昨天的时间

java 复制代码
[root@localhost ~]# date --date="1 day ago" +%Y-%m-%d
2025-01-28
[root@localhost ~]# date +%Y-%m-%d
2025-01-29

我们还可以知道一个月具体有多少天

java 复制代码
[root@localhost ~]# date --date="2025-03-01 1day ago" +%d
28

进程相关指令

ps

复制代码
[root@localhost ~]# ps --help simple
Usage:
 ps [options]
Basic options:
 -A, -e               all processes
 -a                   all with tty, except session leaders
  a                   all with tty, including other users
 -d                   all except session leaders
 -N, --deselect       negate selection
  r                   only running processes
  T                   all processes on this terminal
  x                   processes without controlling ttys

我们比较常用的比如说:ps -ef显示所有的进程

我们可以使用 ps -ef|grep (进程名)来过滤进程

复制代码
[root@localhost ~]# ps -ef|grep python
root       675     1  0 14:47 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
root      1027     1  0 14:47 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      1767  1613  0 16:05 pts/0    00:00:00 grep --color=auto python

最下面的是grep本身,上面的两条就是python相关的进程。

netstat

查看端口信息,这个默认是是没有安装的,我们需要yum命令安装下。

我们需要下载一个工具叫做net-tools,我们可能会遇到一个这样的问题:

这时代理出错了,我们需要去访问阿里云镜像站阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区

根据官方的说明运行对应的指令

我这里用的是centOS07的

复制代码
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

它会自动找到合适的镜像,非常的人性。

一路yes就下载好了。

复制代码
netstat -anp

通过这个命令可以查看,指定的端口是否被占用比如说

复制代码
[root@localhost ~]# netstat -anp | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      987/sshd
tcp        0      0 192.168.8.10:22         192.168.8.2:15146       ESTABLISHED 1470/sshd: root@not
tcp        0     48 192.168.8.10:22         192.168.8.2:15113       ESTABLISHED 1466/sshd: root@pts
tcp6       0      0 :::22                   :::*                    LISTEN      987/sshd

可以查到22端口的使用情况

jps

显示用户当前启动的Java进程信息。在装好Java环境以后才能使用

top

主要作用在于动态的显示系统消耗资源最多的进程信息,包括进程ID,内存占用,CPU占用等

和ps命令基本相同,但是top是动态的

按Q键就退出了

kill

可以使用ps命令先找到进程再用kill结束进程

kill PID 杀掉进程,自杀

kill -9 PID 强制杀掉进程

grep查找

查找文件中符合条件的字符串

-i:忽略大小写

-n:显示该行的行号

-v:忽略包含指定字符串的内容

java 复制代码
[root@localhost ~]# cat hello.txt
hello world
hello world
hello world
hello world
hello world
longest String
[root@localhost ~]# grep long hello.txt
longest String
[root@localhost ~]# cat hello.txt |grep long
longest String

有两种写法

  1. grep (目标字符串) (文件名)
  2. cat (文件名)| grep (目标字符串)

他也是支持正则表达式的,例如查找以l开头的行

复制代码
[root@localhost ~]# grep ^l hello.txt
longest String
java 复制代码
[root@localhost ~]# grep -i  LOng hello.txt
longest String

这里的-i表示忽略大小写。

java 复制代码
[root@localhost ~]# grep -i  LOng -n hello.txt
6:longest String

这里的-n表示显示行号

复制代码
[root@localhost ~]# ps -ef |grep python |grep -v grep
root       679     1  0 20:42 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
root       989     1  0 20:42 ?        00:00:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P

grep -v后面过滤含有指定字符串的信息;

相关推荐
aixingkong9218 小时前
Nvidia为例 XPU的Multi-Host、Socket Direct 、GPUDirect、RDMA 技术点分析
服务器·网络·硬件架构·硬件工程
雯宝10 小时前
mnt_init 函数
linux
律宏阔16 小时前
WSL Docker 端口明明空闲,但就是绑不上端口
linux·windows
律宏阔16 小时前
WSL 突然断网,无法 ping 内网或外网
linux·windows
穷人小水滴16 小时前
用容器编译 VirtualBox 虚拟机软件 (ArchLinux, podman)
linux·容器·virtualbox
乱码三千16 小时前
如何优雅地直连无公网 IP 的远程 GPU 服务器
linux·人工智能·程序员
yunwei3716 小时前
使用 eBPF 跟踪 Nginx 请求
linux·后端·性能优化
yunwei3716 小时前
使用 eBPF 跟踪 MySQL 查询
linux·后端·性能优化
yunwei3716 小时前
eBPF 示例教程:使用 XDP 捕获 TCP 信息
linux·后端·性能优化
judezh16 小时前
api 容器一直 unhealthy?我把一个 Agent 运行时的健康检查逐条拆了,查出四个问题
运维·docker