Linux 教程

本文深入讲解Linux系统中核心命令的使用,包括grep、sed、awk等文本处理工具,以及ps、tar、scp等系统管理和文件传输命令。涵盖命令详解、实例演示与常见应用场景,适合初学者至进阶用户学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

linux 三剑客之grep  sed awk    https://blog.youkuaiyun.com/zisefeizhu/article/details/82526749

grep:   https://baijiahao.baidu.com/s?id=1591674543801492781&wfr=spider&for=pc

sed:  https://www.cnblogs.com/chensiqiqi/p/6382080.html

awk:

 

 

根据字段查询

查看centos的版本

 

 cat /etc/redhat-release

查看linux的cpu信息

cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep "physical id"

 

netstat -tulp

sed -n '/2018-06-21 14:30:20/,/2018-06-21 16:12:00/p'' catalina.out | grep ‘keyword’

 

cat -n 1000 catalina.out |grep "keyword"

 

sed -n ‘/2010-11-17 09:[0-9][0-9]:[0-9][0-9]/,/2010-11-17 16:[0-9][0-9]:[0-9][0-9]/p’  logfile|grep "keyword"

 

 

cat /etc/issue 查看版本

 

Linux的连接方式:    

ssh [-l login_name] [-p port] [user@]hostname

如果:该服务器的ssh端口不是默认的22端口,需要指定登录端口,如:1234

  ssh -l root -p 1234 192.168.0.1

    exit退出

linux常用的命令:

su : 切换用户

cat/tac命令:

格式:cat [选项] [文件]

功能:查看目标文件的内容。

常用选项:

-b 对非空输出行编号。

-n 对输出的所有行编号。

-s 不输出多行空行。

tail命令

格式:tail [选项] [文件]

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

说明:tail命令从指定点开始将文件写到标注输出。使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不断刷新,使你看到最新的文件内容。

常用选项:

-f 循环读取。

-n <行数> 显示行数。

 

tar命令

格式:tar [-cxtzjvf] 文件与目录… 参数

功能:打包/解包,不打开它,直接看内容。

-c 建立一个压缩文件的参数指令(create的意思)。

-x 解开一个压缩文件的参数指令。

-t 查看tarfile里面的文件。

-z 是否同时具有gzip的属性?亦即是否需要用gzip压缩?

-j 是否同时具有bzip的属性?亦即是否需要用bzip压缩?

-v 压缩的过程中显示文件。这个常用,不建议用在背景执行过程。

-f 使用档名,请留意,在f之后要立即接档名,不要再加参数。

-C 解压到指定目录。

 

ps命令:

 

-ax 显示所有没有控制终端的进程

-ax | less 管道

 

注:-f可以优化格式

 

注: 压缩文件属于文件夹

 

scp命令

1.命令格式:

scp [参数] [原路径] [目标路径]

2.命令功能:

scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。linux的scp命令可以在linux服务器之间复制文件和目录。

3.命令参数:

-1  强制scp命令使用协议ssh1

-2  强制scp命令使用协议ssh2

-4  强制scp命令只使用IPv4寻址

-6  强制scp命令只使用IPv6寻址

-B  使用批处理模式(传输过程中不询问传输口令或短语)

-C  允许压缩。(将-C标志传递给ssh,从而打开压缩功能)

-p 保留原文件的修改时间,访问时间和访问权限。

-q  不显示传输进度条。

-r  递归复制整个目录。

-v 详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。  

-c cipher  以cipher将数据传输进行加密,这个选项将直接传递给ssh。  

-F ssh_config  指定一个替代的ssh配置文件,此参数直接传递给ssh。

-i identity_file  从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。   

-l limit  限定用户所能使用的带宽,以Kbit/s为单位。    

-o ssh_option  如果习惯于使用ssh_config(5)中的参数传递方式,  

-P port  注意是大写的P, port是指定数据传输用到的端口号  

-S program  指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。

4.使用实例:

scp命令的实际应用概述:

从本地服务器复制到远程服务器:

(1) 复制文件:

命令格式:

scp local_file remote_username@remote_ip:remote_folder

或者

scp local_file remote_username@remote_ip:remote_file

或者

scp local_file remote_ip:remote_folder

或者

scp local_file remote_ip:remote_file

第1,2个指定了用户名,命令执行后需要输入用户密码,第1个仅指定了远程的目录,文件名字不变,第2个指定了文件名

第3,4个没有指定用户名,命令执行后需要输入用户名和密码,第3个仅指定了远程的目录,文件名字不变,第4个指定了文件名  

(2) 复制目录:

命令格式:

scp -r local_folder remote_username@remote_ip:remote_folder

或者

scp -r local_folder remote_ip:remote_folder

第1个指定了用户名,命令执行后需要输入用户密码;

第2个没有指定用户名,命令执行后需要输入用户名和密码;

从远程服务器复制到本地服务器:

从远程复制到本地的scp命令与上面的命令雷同,只要将从本地复制到远程的命令后面2个参数互换顺序就行了。

实例1:从远处复制文件到本地目录

命令:

scp root@192.168.120.204:/opt/soft/nginx-0.5.38.tar.gz /opt/soft/

 

crontab 命令

 

 

基本操作:

  service crond start    //启动服务

  service crond stop     //关闭服务

  service crond restart  //重启服务

  service crond reload   //重新载入配置

  service crond status   //查看服务状态

 

crontab命令还有一些其他的选项

  -u #指定哪个用户的cron服务,一般是root用户执行这个命令的时候需要

  -l #列出用户的定时任务列表,默认当前用户

  -r #删除用户的定时任务列表,默认当前用户

  -e #编辑用户的定时任务列表,默认当前用户

 

 

1、先手动执行定时任务以此来判断脚本是否有问题。

 

2、确认服务器是否开启定时任务计划服务

 

命令:service crond status

 

service crond start

 

 

3、 查看crontab执行记录

 

如果出现了crontab定时任务不执行的情况,首先需要定位问题,那么就需要通过日志来确定问题所在。

 

crontab的日志位置一般位于/var/log/cron,利用下面的语句即可查看日志。

 

tail -f /var/log/cron

 

上面的/var/log/cron只会记录是否执行了某些计划的脚本,但是具体执行是否正确以及脚本执行过程中的一些信息linux

 

会通过邮件形式发送到给该用户。 对于root用户该邮件记录位于/var/spool/mail/root,通过以下命令可以查看最近的crontab执行情况。

 

tail -f /var/spool/mail/root

mail邮件一般只会记录脚本执行成功与否,如果执行失败,无法给出进一步的错误信息,这时需要我们将语句执行的错误信息

 

重定向至文件中,这样可以很方便的查看错误信息

 

Catalina.out日志查看

 

netstat命令各个参数说明如下:

  

-t : 指明显示TCP端口

  -u : 指明显示UDP端口

  -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的程序)

  -p : 显示进程标识符和程序名称,每一个套接字/端口都属于一个程序。

  -n : 不进行DNS轮询,显示IP(可以加速操作)

即可显示当前服务器上所有端口及进程服务,于grep结合可查看某个具体端口及服务情况··

netstat -ntlp   //查看当前所有tcp端口·

netstat -ntulp |grep 80   //查看所有80端口使用情况·

netstat -an | grep 3306   //查看所有3306端口使用情况·

查看一台服务器上面哪些服务及端口

netstat  -lanp

查看一个服务有几个端口。比如要查看mysqld

ps -ef |grep mysqld

查看某一端口的连接数量,比如3306端口

netstat -pnt |grep :3306 |wc

查看某一端口的连接客户端IP 比如3306端口

netstat -anp |grep 3306

netstat -an 查看网络端口

 

lsof -i :port,使用lsof -i :port就能看见所指定端口运行的程序,同时还有当前连接。

 

nmap 端口扫描

netstat -nupl  (UDP类型的端口)

netstat -ntpl  (TCP类型的端口)

netstat -anp 显示系统端口使用情况

 

ps -ef | grep java   查看全部的java进程

 

(1)find / -name httpd.conf  #在根目录下查找文件httpd.conf,表示在整个硬盘查找

    (2)find /etc -name httpd.conf  #在/etc目录下文件httpd.conf

    (3)find /etc -name '*srm*'  #使用通配符*(0或者任意多个)。表示在/etc目录下查找文件名中含有字符串‘srm’的文件

    (4)find . -name 'srm*'   #表示当前目录下查找文件名开头是字符串‘srm’的文件

查看linux的版本  cat  /etc/redhat-release

 

 

 

curl -o /dev/null -s -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} http://player.qinjiaoyun.cn:20033/data1/transform/2018/12/6/11/40/69DD5BB305F9664FE053661510ACB290/49578b7abbc6792c083bc5e3dde8362a.mp4/mp4/transform.mp4

 

vim下查看

 

 

通过curl得到http各阶段的响应时间

curl的部分时间等变量注释:

url_effective The URL that was fetched last. This is most meaningful if you've told curl to follow location: headers.

 

filename_effective The ultimate filename that curl writes out to. This is only meaningful if curl is told to write to a file with the --remote-name or --output option. It's most useful in combination with the --remote-header-name option. (Added in 7.25.1)

 

http_code http状态码,如200成功,301转向,404未找到,500服务器错误等。(The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.)

 

http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)

 

time_total 总时间,按秒计。精确到小数点后三位。 (The total time, in seconds, that the full operation lasted. The time will be displayed with millisecond resolution.)

 

time_namelookup DNS解析时间,从请求开始到DNS解析完毕所用时间。(The time, in seconds, it took from the start until the name resolving was completed.)

 

time_connect 连接时间,从开始到建立TCP连接完成所用时间,包括前边DNS解析时间,如果需要单纯的得到连接时间,用这个time_connect时间减去前边time_namelookup时间。以下同理,不再赘述。(The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.)

 

time_appconnect 连接建立完成时间,如SSL/SSH等建立连接或者完成三次握手时间。(The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0))

 

time_pretransfer 从开始到准备传输的时间。(The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and negotiations that are specific to the particular protocol(s) involved.)

 

time_redirect 重定向时间,包括到最后一次传输前的几次重定向的DNS解析,连接,预传输,传输时间。(The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execution time for multiple redirections. (Added in 7.12.3))

 

time_starttransfer 开始传输时间。在client发出请求之后,Web 服务器返回数据的第一个字节所用的时间(The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.)

 

size_download 下载大小。(The total amount of bytes that were downloaded.)

 

size_upload 上传大小。(The total amount of bytes that were uploaded.)

 

size_header 下载的header的大小(The total amount of bytes of the downloaded headers.)

 

size_request 请求的大小。(The total amount of bytes that were sent in the HTTP request.)

 

speed_download 下载速度,单位-字节每秒。(The average download speed that curl measured for the complete download. Bytes per second.)

 

speed_upload 上传速度,单位-字节每秒。(The average upload speed that curl measured for the complete upload. Bytes per second.)

 

content_type 就是content-Type,不用多说了,这是一个访问我博客首页返回的结果示例(text/html; charset=UTF-8);(The Content-Type of the requested document, if there was any.)

 

num_connects Number of new connects made in the recent transfer. (Added in 7.12.3)

 

num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)

 

redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)

 

ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)

 

ssl_verify_result ssl认证结果,返回0表示认证成功。( The result of the SSL peer certificate verification that was requested. 0 means the verification was successful. (Added in 7.19.0))

 

1、可以直接访问使用:

#curl -o /dev/null -s -w %{http_code}:%{http_connect}:%{content_type}:%{time_namelookup}:%{time_redirect}:%{time_pretransfer}:%{time_connect}:%{time_starttransfer}:%{time_total}:%{speed_download} www.baidu.com

输出变量需要按照%{variable_name}的格式,如果需要输出%,double一下即可,即%%,同时,\n是换行,\r是回车,\t是TAB。 

-w 指定格式化文件

-o 请求重定向到,不带此参数则控制台输出返回结果

-s 静默,不显示进度

2、也可以定义时间格式化文件访问

#vim curl-time.txt

\n

     http: %{http_code}\n

dns: %{time_namelookup}s\n

redirect: %{time_redirect}s\n

time_connect: %{time_connect}s\n

time_appconnect: %{time_appconnect}s\n

time_pretransfer: %{time_pretransfer}s\n

time_starttransfer: %{time_starttransfer}s\n

size_download: %{size_download}bytes\n

speed_download: %{speed_download}B/s\n

----------\n

time_total: %{time_total}s\n

\n

#curl -w "@curl_time.txt" -s -H "Content-Type: application/json" --insecure --header 'Host: passport.500.com' --data '{"platform":"android","userimei":"F5D815EA2BD8DBARD","app_channel":"10000","mbimei":"9DB358AF","version":"3.1.4","username":"hqzx","userpass":"976af4"}' --compressed https://119.147.113.177/user/login

3、curl以post请求方法

1)以json格式数据

#curl -H "Content-Type: application/json" -X POST --data '{"data":"1"}' http://127.0.0.1/

2)以&连接参数数据

#curl -d "data=7778a8143f111272&score=19&app_key=8d49f16fe034b98b&_test_user=test01" "http://127.0.0.1"

3) 附带文本数据,比如通过"浏览"选择本地的card.txt并上传发送post请求,

其中-F 为带文件的形式发送post请求,   blob为文本框中的name元素对应的属性值。<type="text" name="blob">

#curl -F "blob=@card.txt;type=text/plain" "http://172.16.102.208:8089/wiapi/score?leaderboard_id=7778a8143f111272&score=40&app_key=8d49f16fe034b98b&_test_user=test01"

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值