开发实用Linux命令

分类总结工作实用命令

一、查找类

1、怎么查找opt目录下大于1M的文件

       find /opt -size +1M

附:find命令常用参数介绍[在众多文件或目录中查好需要的文件和目录]

参数如下:是从磁盘查找。

①:按照文件名称查询:

     find filename

     find -name filename(一般通配查好,查找filename*)

     find . -name filename(当前目录下查好指定的文件)

     find / -name filename(从根目录以及所有子目录下查找文件,一般我们不知道文件位于哪个目录下是查找)

②:按照文件大小查找:

     find 目录 -size n

            -n大小小于n的文件

            +n大小大与n的文件

            n大小等于n的文件

 ③:按照文件目录深度查好:  

     find /目录 -mindepth n(从指定的目录下的n级目录下查找)

二、过滤类

  grep过滤文件关键内容

    grep "关键字" 文件

语法:grep [options] PATTERN [FILE]

 ①:过滤关键字显示行号,不区分大小写

   grep -ni "关键字内容" 文件

 ②:查找文件过滤指定内容的行

   grep -v "过滤的内容" 文件

 ③:查找包含dddd,eeee,fffd的行,采用正则表达式

   grep -E "dddd|eeee|fffd" 文件

④:查找以d开头的行

   grep ^d 文件

⑤:查找以ddf开头且结尾的行

    grep ^ddf$ 文件

⑥:查找关键字,颜色展示,这样会展示关键字虽在行中的所有内容

    cat  文件 | grep -ni --color "关键字"-

⑦:只想打印匹配到的关键字【only】

    cat 文件 | grep -noi  "关键字"

⑧:展示匹配关键字的前后几行

    -A num :展示匹配行的后num行

    -B num :展示匹配行的前num行

    -C num: 展示匹配行的前后num行

    cat 文件 | grep -C 2 --color "关键字"

⑨:过滤多个条件

    cat log.txt | grep 条件一 | grep 条件二 | grep 条件三;

    grep 条件一 | grep 条件二 | grep 条件三 文件

    cat log.txt grep -E "条件一|条件二|条件三"

案例:过滤目录文件大小

    du -sh *| sort -rn | head -n 10

案例:过滤第四列内存使用率降序

    ps aux |sort -rn -k4 |awk '{print S1,S2,$3,$4}' | head -n 5

-n 按数值大小排序
-t 指定分隔符,没有指定时,以空白作为分隔符
-k 排序字段 如: -k1 (按第1个字段排序)   -k 1.2, 3.4 (按从第1个字段的第2个字符起,到第3个字段第4个字符止,进行排序)
-b 忽略每行开始的空格
-o 结果输出文件
-r 以相反顺序排序
-m 合并已经有序的文件,不排序
-f 忽略大小写
-d 排序时只按字母、数字或空格,忽略其它字符
-u 去除重复的行

  sort aa.txt |uniq -c |sort -rn【排序和统计降序】

  cat 2.txt |sort -n | uniq -c[打印2.txt的文件次数,只能处理重复的位置的重复]

  ls的列出文件目录.

选项作用
-a显示指定路径中的所有文件,包括隐藏文件
-l显示文件的详细信息,包括文件类型,权限,所属用户,所属用户组,文件大小,上一次修改时间等
-h文件大小以KBytes为单位显示
-S按照文件大小顺序显示,默认从大到小;若要从小到大,可使用-Sr

 ls -alhS【根据文件大小排序】

压缩文件搜索文件,支持多个文件搜索,支持文件多个压缩.

gunzip access_log.gz grep "/api" access_log gzip access_log

zgrep "/api" access_log.gz access_log_1.gz

zcat access.tar.gz | grep -a '/api' zgrep -a "/api" access.tar.gz

zcat  解压文件并将内容输出到标准输出
zcmp  解压文件并且 byte by byte 比较两个文件
zdiff 解压文件并且 line by line 比较两个文件
zgrep 解压文件并且根据正则搜索文件内容
ztest - Tests integrity of compressed files.
zupdate - Recompresses files to lzip format.

附录:10种grep的高级用法

第一种高级用法:精确匹配

grep命令的 -w 选项就派上用场了。它会精确匹配整个单词,而非单词的一部分

$ grep -w "magic" file.txt

第二种高级用法:忽略大小写

$ grep -i "linux" file.txt

第三种高级用法:反向匹配

有时候,我们想要查找不包含某个特定单词或短语的行。grep命令的 -v 选项可以帮助我们实现这一目标.

$ grep -v "error" file.txt

第四种高级用法:行号显示

当我们需要知道匹配内容所在的行号时,grep命令的 -n 选项非常有用。它就像是一位贴心的标记员,为你每一次成功的匹配都打上了行号。

grep -n "warning" file.txt

第五种高级用法:显示匹配上下文

grep -C 2 "keyword" file.txt

第六种高级用法:递归搜索

有时候,我们需要在目录及其子目录中递归搜索文件。grep命令的 -r 选项可以帮助我们实现这一目标

grep -r "pattern" directory/

第七种高级用法:统计匹配行数

有时候,我们只关心匹配行的数量而不关心具体内容。grep命令的 -c 选项可以帮助我们实现这一目标

grep -c "pattern" file.txt

第八种高级用法:使用正则表达式

正则表达式是一种强大的模式匹配工具,能够更灵活地定义搜索模式。grep命令支持使用正则表达式进行搜索

grep "^[A-Za-z]+$" file.txt

第九种高级用法:使用文件作为模式输入

有时候,我们需要在文件中提供多个模式,以便同时搜索它们。grep命令的 -f 选项可以帮助我们实现这个目标.

 grep -f patterns.txt file.txt

第十种高级用法:输出匹配结果到文件

有时候,我们希望将匹配的结果保存到一个文件中,以便后续处理。Shell命令的 > 重定向操作符可以帮助我们实现这一目标.

 grep "pattern" file.txt > output.txt

三、查看端口占用情况

①:lsof -i:指定端口【lsof(list open files)是一个列出当前系统打开文件的工具。】

【netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息】

②:netstat -anp| grep 指定端口

③:netstat -tunlp| grep 指定端口

附:常用参数如下:netstat.

netstat [选项]
 
-a或--all:显示所有连线中的Socket; 
-A<网络类型>或--<网络类型>:列出该网络类型连线中的相关地址; 
-c或--continuous:持续列出网络状态; 
-C或--cache:显示路由器配置的快取信息; 
-e或--extend:显示网络其他相关信息; 
-F或--fib:显示FIB; 
-g或--groups:显示多重广播功能群组组员名单; 
-h或--help:在线帮助; 
-i或--interfaces:显示网络界面信息表单; 
-l或--listening:显示监控中的服务器的Socket; 
-M或--masquerade:显示伪装的网络连线; 
-n或--numeric:直接使用ip地址,而不通过域名服务器; 
-N或--netlink或--symbolic:显示网络硬件外围设备的符号连接名称; 
-o或--timers:显示计时器; 
-p或--programs:显示正在使用Socket的程序识别码和程序名称; 
-r或--route:显示Routing Table; 
-s或--statistice:显示网络工作信息统计表; 
-t或--tcp:显示TCP传输协议的连线状况; 
-u或--udp:显示UDP传输协议的连线状况; 
-v或--verbose:显示指令执行过程; 
-V或--version:显示版本信息; 
-w或--raw:显示RAW传输协议的连线状况; 
-x或--unix:此参数的效果和指定"-A unix"参数相同; 
--ip或--inet:此参数的效果和指定"-A inet"参数相同。

【四、查看指定行号的日志文件】 

①、sed输出指定的行号
cat temp.log | sed -n '200p' 
②、awk输出指定的行号
cat temp.log | awk 'NR==200'
③、先获取前面的200行,再取最后一行
cat temp.log | head -n 200 | tail -n1 

【五、测试端口连通性】

①、telnet ip port
ip是指测试主机的ip
port是指测试主机的端口
②、curl ip:port
ip是指测试主机的ip
port是指测试主机的端口
③、wget ip:port
wget探测端口
④、lsof -i:port
lsof 命令来查看某一端口是否开放,如果有显示说明已经开放了,如果没有显示说明没有开放

 【六、Base64编码,Java实现和Python的RSA版本实现,openssl】

编码:echo "StringBuffer" | openssl base64

解码:echo "U3RyaW5nQnVmZmVyCg=="|openssl base64 -d 

编码:echo StringBuffer | base64

解码:echo U3RyaW5nQnVmZmVyCg== |base64 -d

 Java版本Base64

import java.util.Base64;   
    /**
     * jdk版本
     */
    public static void getBase64ForJdk(){
        String str="AbstractClass";
        // Base64编码
        String encodeStr = new String(Base64.getEncoder().encode(str.getBytes(StandardCharsets.UTF_8)));
        System.out.println(encodeStr);
        // Base64解码
        String decodeStr = new String(Base64.getDecoder().decode(encodeStr.getBytes(StandardCharsets.UTF_8)));
        System.out.println(decodeStr);
    }
Apache Codec版本
import org.apache.commons.codec.binary.Base64;    
    /**
     * Apache Codec版本
     */
    public static void getBase64ForCodec(){
        String str="AbstractClass";
        // Base64编码
        String encodeStr=new String(Base64.encodeBase64(str.getBytes(StandardCharsets.UTF_8)));
        System.out.println(encodeStr);
        // Base64解码
        String decodeStr = new String(Base64.decodeBase64(encodeStr.getBytes(StandardCharsets.UTF_8)));
        System.out.println(decodeStr);
    }
Apache Tomcat版本
import org.apache.tomcat.util.codec.binary.Base64;    
    /**
     * Apache Tomcat版本
     */
    public static void getBase64ForTomcat(){
        String str="AbstractClass";
        // Base64编码
        String encodeStr=new String(Base64.encodeBase64(str.getBytes(StandardCharsets.UTF_8)));
        System.out.println(encodeStr);
        // Base64解码
        String decodeStr = new String(Base64.decodeBase64(encodeStr.getBytes(StandardCharsets.UTF_8)));
        System.out.println(decodeStr);
    }

有多种开源包实现的Base64编码算法.

MD5消息摘要算法实现

    public static String md5(String str){
        try {
            MessageDigest md5 = MessageDigest.getInstance(Constants.MD5);
            //加密后的字符串
            byte[] src = md5.digest(str.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(src);
        } catch (Exception e) {
            throw ExceptionUtils.mpe(e);
        }
    }

【七、远程复制,远程服务器之前可以配置免密登录】

 【sshpass::可以在命令行直接使用密码来进行远程连接和远程拉取文件

scp local_file remote_username@remote_ip:remote_folder
scp /home/space/music/1.mp3 root@www.runoob.com:/home/root/others/music 

 【八、跳过证书校验下载文件】

①、wget
# 跳过证书校验
wget https://img-home.csdnimg.cn/images/20201218055848.png --no-check-certificate
# 伪装成浏览器
wget --user-agent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16" https://img-home.csdnimg.cn/images/20201218055848.png
# 重命名
wget -O code.png https://img-home.csdnimg.cn/images/20201218055848.png
# 断点续传
wget -c https://img-home.csdnimg.cn/images/20201218055848.png
②、curl【可以指定使用具体的ssl协议,Nginx中的ssl协议中会有设置具体支持哪些tls1.0\1.1\1.2\1.3版本】
curl -k --tlsv1 -O https://192.168.100.58/uploads/835538633.jpeg

 curl验证http2.0,curl使用一个叫做nghttp2的库来提供http2帧层的支持。curl依赖于nghttp2 1.0以上版本

curl --version【curl查看是否支持http2.0】

 curl -k --http2 -v https://search.bilibili.com【验证是否开启http2.0】

磁盘文件空间
①、df -h
②、du -sh *
③、df -i

  curl -w诊断问题

通过CURL命令可以方便的查询各种http请求的问题,-w参数对于我们诊断问题非常重要,以下是-w参数对应的一些变量以及对应的解释:

url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形。
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 开始传输时间。在发出请求之后,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 当连接到远程的ftp服务器时的初始路径。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))
curl -o /dev/null -s -w "time_connect: %{time_connect}\ntime_starttransfer: %{time_starttransfer}\ntime_nslookup:%{time_namelookup}\ntime_total: %{time_total}\n" "https://www.baidu.com"

总请求时间:0.064s,网络连接耗时:0.013s,dns解析耗时:0.001s,网络传输:0.064s

【九、快速清空大文件】

  Linux 下清空或删除大文件内容的 5 种方法

 ①、通过重定向到 Null 来清空文件内容
 > access.log 
 ②、使用 ‘true’ 命令重定向来清空文件
 : > access.log
 true > access.log
 ③、使用 cat/cp/dd 实用工具及 /dev/null 设备来清空文件
 cat /dev/null > access.log
 cp /dev/null access.log
 ④、使用 echo 命令清空文件
 echo "" > access.log
 echo > access.log
 ⑤、使用 truncate 命令来清空文件内容
 truncate -s 0 access.log

十、Linux服务器信息

查看服务器信息:

uname -a

执行后,会输出当前系统的内核版本号、主机名、内核发行版号等信息。其中,-a参数表示显示所有信息。

lsb_release

cat /etc/issue

cat /etc/os-release

hostnamectl

cat /proc/version

dmesg | grep "Linux version"

cat /etc/redhat-release

【十一、gz压缩】

1、压缩文件
gzip -c test.txt >test.txt.gz
2、压缩目录
gzip -r dirname
gzip -r test
3、解压缩
gzip -d test.txt.gz

十二、外部命令

htop命令,CPU/内存使用率

yum install htop -y

htop命令 

CPU利用率

内存利用率

负载情况,看CPU的忙碌情况,一般是CPU核数的75%左右.

网络使用情况:

yum install nload -y

 nload

可以选择对应的网卡

参数指标说明:

  • Curr:当前网速

  • Avg:平均网速

  • Min:最小网速

  • Max:最大网速

  • Ttl:总流量

使用iostat查看磁盘I/O

yum install sysstat -y

输入iostat命令后,磁盘总体读写情况如上所示。磁盘负载主要关注2个指标:%idle,%util

%idle:表示CPU除去等待磁盘I/O以外的空闲时间百分比,这个指标应该要保证在70%以上
%util:该设备用于I/O操作的时间百分比,这个指标需要保证在70%以下,当到达100%时表示已经满负载。为了降低磁盘负载,可以采用性能更高的磁盘(OSD,PCIE)或者降低磁盘的操作频率(异步写、合并写)

持续不断更新中.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道之简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值