怎么做企业网站优化需要多少钱,大学网站建设情况汇报,专门做app网站,供电公司企业文化建设Linux高级命令简介
在安装好linux环境以后第一件事情就是去学习一些linux的基本指令#xff0c;我在这里用的是CentOS7作演示。
首先在VirtualBox上装好Linux以后#xff0c;启动我们的linux#xff0c;输入账号密码以后学习第一个指令 简介 Linux高级命令简介ip addrtou…Linux高级命令简介
在安装好linux环境以后第一件事情就是去学习一些linux的基本指令我在这里用的是CentOS7作演示。
首先在VirtualBox上装好Linux以后启动我们的linux输入账号密码以后学习第一个指令 简介 Linux高级命令简介ip addrtouchvilinux文件意外退出查找 wcsortuniqheaddate进程相关指令psnetstatjpstopkillgrep查找 ip addr
这个指令是查看我们的ip地址 这个ip地址受很多因素影响当你连接的网段不一样比如说我在图书馆连校园网运行这个指令就不会显示这个地址在宿舍连个人的网络就会显示大家可以把他配置成静态的ip地址方便我们以后操作
Centos7 配置静态IP地址_centos7配置静态ip地址和网关-CSDN博客
可以参考这位博主的文章写的很详细。
在获取到IP地址以后我们要对他建立远程连接因为我们直接操作这个小窗口十分不方便复制粘贴文件上传等十分受限在这里我使用MobaXterm来建立SSH连接 显示这样就成功连接了。
输入账号密码就成功登陆了 选择一些基本的配置方便我们的的使用
先学习以下如何查看当前目录下的各个文件
使用dir指令这这指令在windows中也很常用
[rootlocalhost ~]# dir
anaconda-ks.cfg
[rootlocalhost ~]#这里只有一个anaconda-ks.cfg文件
下面我们在学习一个命令 创建文件
touch
例如
touch hello.txt创建一个hello.txt文件
[rootlocalhost ~]# touch hello.txt
[rootlocalhost ~]# dir
anaconda-ks.cfg hello.txt
[rootlocalhost ~]#这样我们发现新增了一个文件我们创建的hello.txt文件下面这是一个删除文件的指令以我的理解来看rm就是remove移除
rf就是remove file移除文件。
rm -rf hello.txtvi
这是一个十分重要的指令用来打开我们的文件比如说我们打开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文件
[rootlocalhost ~]# 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
[rootlocalhost ~]#使用ll -a指令我们就可以看到所有被隐藏的文件
我们就找到了.hello.txt.swp文件
老办法remove它
在这里我们打开了复制粘贴的设置只要我们用鼠标左键选中了文件名右键就可以把他粘贴下来。
[rootlocalhost ~]# rm -rf hello.txt.swp
[rootlocalhost ~]# 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
[rootlocalhost ~]#像这样我们就成功的删除了.swp文件。再打开就不用有那个界面了。
查找
在我们回到主页面以后可以通过cat指令来查看刚才写的内容
[rootlocalhost ~]# cat hello.txt
hello world8
hello world7
hello world6
hello world5
hello world4
[rootlocalhost ~]#再介绍一线查找和定位的指令
[rootlocalhost ~]# 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 显示版本信息。
[rootlocalhost ~]# wc -c hello.txt
85 hello.txt
[rootlocalhost ~]# wc -l hello.txt
6 hello.txt
[rootlocalhost ~]# wc -w hello.txt
12 hello.txt
[rootlocalhost ~]#我们通过仔细阅读帮助文件也可以学到很多东西。
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 以相反的顺序来排序。
[rootlocalhost ~]# cat num.txt
1
2
3
4
6
10
20
30
21
[rootlocalhost ~]# sort num.txt
1
10
2
20
21
3
30
4
6
[rootlocalhost ~]#
sort的排序法更类似于基数排序把第一个字母排序以后才会继续排序。
想要传统的排序方法也很简单
[rootlocalhost ~]# sort -n num.txt
1
2
3
4
6
10
20
21
30
[rootlocalhost ~]#也可以逆序排列
[rootlocalhost ~]# sort -n -r num.txt
30
21
20
10
6
4
3
2
1
[rootlocalhost ~]#uniq
学习这个指令首先查看他的帮助文档 其中我们用的比较多的就是-c和-u两个指令
不加返回重复的行-c:返回重复行数的统计-u:返回未重复的行
下面演示一下uniq的用法 说明一个工作中会遇到的坑
[rootlocalhost ~]# cat test.txt
hello
hello
hello
world
hello
hello
hello
hello
[rootlocalhost ~]# uniq test.txt
hello
world
hello
当我们对无序的文本内容进行排序的时候就会返回这种奇怪的结果这个时候我们需要用到
管道命令
“|”是Linux管道命令操作符简称管道符。使用此管道符“|”可以将两个命令分隔开“|”左边命令的输出就会作为“|”右边命令的输入此命令可连续使用第一个命令的输出会作为第二个命令的输入第二个命令的输出又会作为第三个命令的输入依此类推。
[rootlocalhost ~]# sort test.txt
hello
hello
hello
hello
hello
hello
hello
world
[rootlocalhost ~]# sort test.txt|uniq
hello
worldhead
默认返回前10条数据我们也可以指定 演示
[rootlocalhost ~]# head -3 num.txt
1
2
3也可以使用管道命令来先排序后输出
[rootlocalhost ~]# cat num.txt|sort -n|head -3
1
2
[rootlocalhost ~]# cat num.txt|sort -n|head -4
1
2
3date
日期命令获取当前日期。它的帮助文帝其实已经说的很清楚了因为他返回的默认格式有时候我们并不理解所以提供了格式化工具
[rootlocalhost ~]# 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, --dateSTRING display time described by STRING, not now-f, --fileDATEFILE like --date once for each line of DATEFILE-I[TIMESPEC], --iso-8601[TIMESPEC] output date/time in ISO 8601 format.TIMESPECdate for date only (the default),hours, minutes, seconds, or ns for dateand time to the indicated precision.-r, --reference文件 显示文件指定文件的最后修改时间-R, --rfc-2822 以RFC 2822格式输出日期和时间例如2006年8月7日星期一 12:34:56 -0600--rfc-3339TIMESPEC output date and time in RFC 3339 format.TIMESPECdate, seconds, or ns fordate and time to the indicated precision.Date and time components are separated bya single space: 2006-08-07 12:34:56-06:00-s, --setSTRING 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 数字时区带有必要的精度 (例如-0405:30)%Z 按字母表排序的时区缩写 (例如EDT)%S返回秒%s返回时间戳
[rootlocalhost ~]# date %Y-%m-%d_%H:%M:%S
2025-01-29_15:25:52
[rootlocalhost ~]# date %Y-%m-%d %H:%M:%S
2025-01-29 15:26:06当我们要求的返回值里面有空格的时候就需要加引号了没有空格的时候可以直接返回。
有时候我们需要返回当前时间或者两个时间之间的时间差我们一般先返回时间戳再计算他们之间的间隔
[rootlocalhost ~]# date --date2026-01-01 00:00:00
2026年 01月 01日 星期四 00:00:00 CST
//仔细观察这里的%s前丢了一个空格需要注意哦。
[rootlocalhost ~]# date --date2026-01-01 00:00:00%s
date: 无效的日期2026-01-01 00:00:00%s
[rootlocalhost ~]# date --date2026-01-01 00:00:00 %s
1767196800我们也可以返回昨天的时间
[rootlocalhost ~]# date --date1 day ago %Y-%m-%d
2025-01-28
[rootlocalhost ~]# date %Y-%m-%d
2025-01-29我们还可以知道一个月具体有多少天
[rootlocalhost ~]# date --date2025-03-01 1day ago %d
28进程相关指令
ps
[rootlocalhost ~]# ps --help simple
Usage:ps [options]
Basic options:-A, -e all processes-a all with tty, except session leadersa all with tty, including other users-d all except session leaders-N, --deselect negate selectionr only running processesT all processes on this terminalx processes without controlling ttys我们比较常用的比如说ps -ef显示所有的进程 我们可以使用 ps -ef|grep 进程名来过滤进程
[rootlocalhost ~]# 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 --colorauto 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通过这个命令可以查看指定的端口是否被占用比如说
[rootlocalhost ~]# 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: rootnot
tcp 0 48 192.168.8.10:22 192.168.8.2:15113 ESTABLISHED 1466/sshd: rootpts
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:忽略包含指定字符串的内容
[rootlocalhost ~]# cat hello.txt
hello world
hello world
hello world
hello world
hello world
longest String
[rootlocalhost ~]# grep long hello.txt
longest String
[rootlocalhost ~]# cat hello.txt |grep long
longest String有两种写法
grep (目标字符串) (文件名)cat 文件名| grep (目标字符串)
他也是支持正则表达式的例如查找以l开头的行
[rootlocalhost ~]# grep ^l hello.txt
longest String[rootlocalhost ~]# grep -i LOng hello.txt
longest String这里的-i表示忽略大小写。
[rootlocalhost ~]# grep -i LOng -n hello.txt
6:longest String这里的-n表示显示行号
[rootlocalhost ~]# 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 -Pgrep -v后面过滤含有指定字符串的信息