Linux学习-day9

文本处理常用命令

cat命令

cat适合读取小文件

cat 适合读取小文件,不能读取大文件,一次性将文件内容全部读取到内存中,且输出到屏幕上

[root@localhost ~]# cat /etc/nginx/nginx.conf  #查看nginx软件的配置文件(linux默认安装的软件,配置文件会自动写到/etc目录下)
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
.....
#            location = /50x.html {
#        }
#    }
}

查看系统用户信息

[root@localhost ~]# cat /etc/passwd  #查看系统的用户信息有哪些
root:x:0:0:root:/root:/bin/bash
....
nginx:x:988:982:Nginx web server:/var/lib/nginx:/sbin/nologin

[root@localhost ~]# cat -n  /etc/passwd  #-n 显示行号
     1	root:x:0:0:root:/root:/bin/bash
....
    44	nginx:x:988:982:Nginx web server:/var/lib/nginx:/sbin/nologin

连续读取多个文件:cat -n doupo.txt hehe.txt

[root@localhost opt]# cat -n  doupo.txt hehe.txt  hello_python.txt  #还可以连续读取多个文件,并且显示三文件一共有多少行

结合重定向符号:cat >> chunxiao2.txt << EOF

* 结合重定向符号使用
>   重定向覆盖输出符,数据从左边,覆盖写入到右边 
<   重定向覆盖输入符,数据从右边,覆盖写入到左边
>>  重定向追加输出符,数据从左边,追加写入到右边 
<<  重定向覆盖输入符,数据从右边,追加写入到左边

[root@localhost ~]# echo chunxiao.txt
chunxiao.txt
[root@localhost ~]# echo 春晓 > chunxiao.txt
[root@localhost ~]# echo 春眠不觉晓 >> chunxiao.txt
[root@localhost ~]# echo 花落知多少 >> chunxiao.txt
[root@localhost ~]# echo 夜来风雨声 >> chunxiao.txt
[root@localhost ~]# echo 处处蚊子咬 >> chunxiao.txt
[root@localhost ~]# cat chunxiao.txt
春晓
春眠不觉晓
花落知多少
夜来风雨声
处处蚊子咬

[root@localhost ~]# cat >> chunxiao2.txt << EOF  #EOF是一个关键字,end of file  ,文件的结束
> 春晓
> 春眠不觉晓
> 处处闻啼鸟
> 夜来风雨声
> 花落知多少
> EOF
[root@localhost ~]# cat chunxiao2.txt
春晓
春眠不觉晓
处处闻啼鸟
夜来风雨声
花落知多少

添加编号:cat -b chunxiao.txt

cat证明文件存在空行的办法
-b  只会对有内容的行,显示其行号,空行不显示
-E  在linux文件中,每一行的结束,默认会添加一个你看不到的,特殊符号 '$'  ,表示是该行的结尾

可以利用-b参数,完成对有内容的行,进行编号的作用
[root@localhost opt]# cat -b chunxiao.txt 

tac命令

将文件按行逆序查看

[root@localhost opt]# cat -n chunxiao.txt 
春晓
春眠不觉晓
处处闻啼鸟
[root@localhost opt]# 
[root@localhost opt]# 
[root@localhost opt]# tac hehe.txt 
处处闻啼鸟
春眠不觉晓
春晓

ls和sl

ls 查看文件夹下内容;sl是个小火车

安装这个sl命令
yum install sl -y

more和less命令

分屏显示

more和cat都是一次性读取所有内容到内存,不适合读取大文件,占资源;

less命令是显示多少文本,消耗多少内存,省资源。

空格,翻篇

回车 下一行

head和tail命令

显示文件前/后10行信息

-n 显示文件前/后n行信息

tail -f 文件名称  #显示文件新增信息,例如日志监控
tail -F 文件名称  #监控文件,哪怕文件不存在也可以,文件有变化即显示	

wc命令

统计文件的行

  • vim(set nu)
  • cat -n
  • wc -l
1.wc命令,统计文件内有多少行,有一个回车,是一个空行
[root@localhost opt]# wc -l doupo.txt 
32 doupo.txt

2. wc -w 统计文件内的单词数,根据空格判断
[root@localhost opt]# wc -w hehe.txt 
9 hehe.txt

du命令

统计文件大小的命令

  • ls -lh
du命令
作用:查看文件或目录(会递归显示子目录)占用磁盘空间大小

-s :summaries,只显示汇总的大小,统计文件夹的大小,默认du与du -s是一样的
[root@localhost opt]# du -s .
105072KB	.

-h:表示以高可读性的形式进行显示,如果不写-h,默认以KB的形式显示文件大小
linux的文件系统,对文件最小管理单位是4kb算起。
[root@localhost opt]# du -h *
4.0K	doupo.txt
4.0K	hehe.txt
4.0K	hello_python.txt
4.0K	index.txt
4.0K	three_files.txt
26M	xixi
26M	xixi1
26M	xixi2
26M	xixi3
4.0K	古诗2.txt
4.0K	呵呵.txt
4.0K	春晓.txt
4.0K	蔡旭困.txt


2.显示文件夹的大小
[root@localhost /]# du -h /opt
103M	/opt


3.发现机器磁盘空间不够了,你领导让你看看日志目录是不是太大了
du -sh  /var/log

4.查看nginx软件的日志目录,容量是多少
[root@localhost log]# du -sh /var/log/nginx
32K	/var/log/nginx



find找文件

windows下的强大搜索工具

everything工具

linux的工具是谁?---find命令。

find是递归查找

[root@localhost ~]# find  /  -name 'doupo.txt'  #最大范围,全系统搜索
/opt/doupo.txt

[root@localhost ~]# find  /opt  -name 'doupo.txt' #缩小搜索范围,从/opt开始找
/opt/doupo.txt

如果文件名是写死的,找不到,那就是这个/opt目录下没有该文件
[root@localhost ~]# find  /opt  -name 'doupo.txt1'

# 找到机器上所有的 doupo.txt
[root@localhost ~]# find  /  -name 'doupo.txt'
/etc/doupo.txt
/tmp/doupo.txt
/usr/local/doupo.txt
/home/doupo.txt
/opt/doupo.txt
[root@localhost ~]# 

# 模糊查找,找出/var下所有的log文件  nginx.log  mysql.log   yuchao.log    *.log 
[root@localhost ~]# find /var  -name '*.log'

# -type  f   找到文本类型的数据 
# -type  d    找到文件夹类型的数据

# 找出机器上所有的 doupo.txt 【文件,文本文件,可以被cat的文件】
# 严格注意命令的语法细节!!

find / -type f -name 'doupo.txt' 

我想查看linux上的图片,如何看

图片路径是,/usr/share/nginx/html/nginx-logo.png

有哪些思路
1.windows (linux把该图片,发到win上,lrzsz工具,xftp)
sz /usr/share/nginx/html/nginx-logo.png
2.linux上装图像编辑器(你的linux也得装图形化)

补充知识

nginx的了解

淘宝网,京东网,绝大多数网站,都是以nginx软件为官网入口,给用户提供 html文件。

1.nginx小试牛刀
yum install nginx -y
系统默认是没有这个nginx安装包的。

2.简单了解,给你的机器,配置一个软件仓库(360软件大师,点点点,下载各种工具,它的软件,还携带了一堆广告)

配置阿里云的软件仓库
https://developer.aliyun.com/mirror/
这里配置下载2个软件仓库 (centos和redhat系统的关系)

# wget -O 指定文件存放到哪里 资源的下载链接
wget https://mirrors.aliyun.com/repo/Centos-7.repo  # 直接下载Centos-7.repo这个文件,到当前目录

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo #不但下载资源,且指定到该路径下
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3.既然机器有了软件仓库,即可下载各种应用了
[root@localhost opt]# yum install nginx -y

4. 就可以启动该应用
[root@localhost opt]# systemctl start nginx

5.程序运行了,如何判断他运行?
windows的任务管理器
查看进程,就是一个个运行中的软件,在消耗系统的资源

linux的任务管理器
查看nginx这个程序的进程
执行ps命令,仅仅查看这个机器上的 nginx进程信息,是否存在
ps -ef | grep nginx

6. 一个网站的运行,默认端口是 http的80端口
用你的客户端,浏览器去访问,
10.96.0.134:80即可

7.注意关闭服务器的防火墙
[root@localhost opt]# iptables -F   #关闭防火墙
[root@localhost opt]# setenforce 0  #关闭系统默认防火墙

8.停止这个网站服务,思路是?
[root@localhost opt]# systemctl stop nginx  #停用80端口,也就是,停止这个软件的运行
systemctl restart nginx  #再次重新运行

8.可以修改该软件的配置了,比如网站首页的内容
wget https://dnf.qq.com/main.shtml  #下载该网址首页html文件
[root@localhost opt]# mv /opt/main.shtml /usr/share/nginx/html/index.html  
mv: overwrite ‘/usr/share/nginx/html/index.html’? y       ##移动,拷贝该文件,到你的nginx网站目录下, 它会自动识别

9.当你修改了网站的前端页面,无须重启,网站自动更新
http://10.96.0.134/

10.问题排错 server / client
I、出现403错误页面,权限的问题,防火墙的问题
解决思路
- 加大文件夹的权限 chmod
- 关闭防火墙
II、nginx程序也运行了,但是就无法访问(浏览器,压根看不到页面内容,直接决绝连接)
解决思路
了解任务管理器,如何访问该程序(web网站程序)
- 找服务端的问题 ,确保没什么问题
- 客户端也可能出错
- 用的是微软的很老的浏览器,兼容性太差,访问不了,建议,以后都用谷歌浏览器,火狐浏览器

posted on 2024-04-14 01:06  day_day_u_p  阅读(3)  评论(0编辑  收藏  举报