阅读 376

linux专题(一)必须掌握的命令

简介

之前也用linux系统,但是只是浅层次的操作几个命令,所以萌生出系统学习的想法

本文主要用于学习linux的常用命令,本文的目录结构与参考目录大致相同,但是会包含一些实践

主要参考

及命令大全https://www.linuxcool.com/

强大好用shell

linux的内核负责完成对硬件资源的分配,可以让各种硬件设备各司其职,协同运行,不能直接修改内核参数,这样风险太大了,应该基于系统调用接口来管理计算机,但是直接通过系统提供的API调用的话又太多了,所以外层则有了服务程序,

Shell就是终端程序的统称,负责调用相关的程序服务去完成指定工作

注:Shell与Bash是好汉关系,Bash是Shell终端程序的一部分

执行命令的必备知识

1.格式

linux的命令格式: 命令名称 [命令参数] [命令对象]

**命令名称 **:表达想要做的事情,如创建用户、查看文件等

**[命令参数] **:用于对命令的调整

[命令对象] :一般指要处理的资源的名称

注: 命令名称、命令参数与命令对象之间要用空格进行分隔,且严格区分大小写字母。

2.接口手册

执行命令man man 查看自己的帮助信息 如下结果

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# man man


MAN(1)                                         Manual pager utils                                         MAN(1)

NAME
       man - an interface to the on-line reference manuals
#联机参考手册的接口
SYNOPSIS
       man [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m system[,...]] [-M path] [-S
       list] [-e extension] [-i|-I] [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r
       prompt]  [-7]  [-E  encoding]  [--no-hyphenation]  [--no-justification]  [-p  string]  [-t]  [-T[device]]
       [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
       man -k [apropos options] regexp ...
       man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
       man -f [whatis options] page ...
       man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt]  [-7]
       [-E encoding] [-p string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] file ...
       man -w|-W [-C file] [-d] [-D] page ...
       man -c [-C file] [-d] [-D] page ...
       man [-?V]

DESCRIPTION
       man  is  the  system‘s  manual  pager. Each page argument given to man is normally the name of a program,
       utility or function.  The manual page associated with each of these arguments  is  then  found  and  dis‐
       played.  A section, if provided, will direct man to look only in that section of the manual.  The default
       action is to search in all of the available sections, following a pre-defined order and to show only  the
       first page found, even if page exists in several sections.

       The table below shows the section numbers of the manual followed by the types of pages they contain.

 Manual page man(1) line 1 (press h for help or q to quit)

man命令中常用按键以及简介

按键 作用
空格键 向下翻一页
PaGe down 向下翻一页
PaGe up 向上翻一页
home 直接前往首页
end 直接前往尾页
/ 从上至下搜索某个关键词,如“/linux”
? 从下至上搜索某个关键词,如“?linux”
n 定位到下一个搜索到的关键词
N 定位到上一个搜索到的关键词
q 退出帮助文档

man命令中帮助信息的结构以及意义

结构名称 代表意义
NAME 命令的名称
SYNOPSIS 参数的大致使用方法
DESCRIPTION 介绍说明
EXAMPLES 演示(附带简单说明)
OVERVIEW 概述
DEFAULTS 默认的功能
OPTIONS 具体的可用选项(带介绍)
ENVIRONMENT 环境变量
FILES 用到的文件
SEE ALSO 相关的资料
HISTORY 维护历史与联系方式
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# 
#上面这部分内容叫做终端提示符
#root为用户名
#主机名为iZ2ze9fp041k3gtah0ztnmZ
#当前目录为~即home
#结尾是#代表管理员以?结尾代表普通用户

3.快捷键技巧

1.TAB键

用于命令、参数、文件的内容补全,如想执行clear但是想不起来全部命令了可以输入

cl :即cl+点击两下Tab按钮

#展示了以cl开头的全部命令
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cl
clear           clock           clockdiff       cloud-id        cloud-init      cloud-init-per  
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cl

不仅命令可以这样文件也可以这样

#点击两个Tab键可以展示
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cd /etc/lo
locale.conf     localtime       login.defs      logrotate.conf  logrotate.d/    
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cd /etc/lo

2.Ctrl+c键

表示当前进程进行终止操作

如下执行watch -n 5 uptime查看系统不咋情况,直到Ctrl+c才会终止

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# watch -n 5 uptime
Every 5.0s: uptime                                                                         Mon Jul 12 15:48:48 2021

 15:48:48 up 17 days,  3:02,  2 users,  load average: 0.00, 0.01, 0.05

3.Ctrl+d键

代表减排输入结束,简称EOF 直接退出

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# logout
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(39.107.126.152-阿里云) at 15:50:21.

Type `help‘ to learn how to use Xshell prompt.
[C:\~]$ 

4.Ctrk+l键

代表清屏

常用系统工作命令

echo

格式:echo [字符串] [$变量]

简介:用于在终端设备上输出字符串或变量提取后的值 ,$表示提取变量的实际值

如下方

#第一个echo表示输出变量,第二个echo表示输出字符串hello
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# echo $(echo hello)
hello
#第一个echo表示输出字符串和变量,第二个表示输出字符串
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# echo hi $(echo zhao56)
hi zhao56
#和变量配置输出
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# echo $SHELL
/bin/bash

date

格式: date [OPTION]... [+FORMAT]或date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

简介: 用于显示或设置系统的时间与日期

下方为常见的显示参数格式

参数 简介
%S 秒(00~59)
%M 分钟(00~59)
%H 小时(00~23)
%I 小时(00~12)
%m 月份(1~12)
%p 显示出AM或PM
%a 缩写的工作日名称(例如:Sun)
%A 完整的工作日名称(例如:Sunday)
%b 缩写的月份名称(例如:Jan)
%B 完整的月份名称(例如:January)
%q 季度(1~4)
%y 简写年份(例如:20)
%Y 完整年份(例如:2020)
%d 本月中的第几天
%j 今年中的第几天
%n 换行符(相当于按下回车键)
%t 跳格(相当于按下Tab键)
#显示时间
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# date
Mon Jul 12 16:05:28 CST 2021
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# date "+%Y-%m-%d %H:%M:%S"
2021-07-12 16:05:32
#修改时间
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# date -s "20210711 8:30:00"
Sun Jul 11 08:30:00 CST 2021
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# date
Sun Jul 11 08:30:03 CST 2021
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# date "+%Y-%m-%d %H:%M:%S"
2021-07-11 08:30:08

timedatectl

格式:timedatectl [OPTIONS...] COMMAND ...

简介: 设置系统的时间 ,如发现电脑时间与实际不符,必须要调整当前时间

如下:COMMAND中常见的参数

参数 简介
status 显示状态信息
list-timezones 列出已知时区
set-time 设置系统时间
set-timezone 设置生效时区
#查看系统时间和时区
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# timedatectl status
      Local time: Sun 2021-07-11 08:34:03 CST
  Universal time: Sun 2021-07-11 00:34:03 UTC
        RTC time: Mon 2021-07-12 16:11:33
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         ‘timedatectl set-local-rtc 0‘.
#由于之前改过,将时间改回来,发现改不回来了,醉了,然后直接下包更新
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# yum install -y ntpdate
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Determining fastest mirrors
base                                                                                        | 3.6 kB  00:00:00     
docker-ce-stable                                                                            | 3.5 kB  00:00:00     
epel                                                                                        | 4.7 kB  00:00:00     
extras                                                                                      | 2.9 kB  00:00:00     
updates                                                                                     | 2.9 kB  00:00:00     
(1/3): updates/7/x86_64/primary_db                                                          | 8.8 MB  00:00:00     
(2/3): epel/x86_64/updateinfo                                                               | 1.0 MB  00:00:00     
(3/3): epel/x86_64/primary_db                                                               | 6.9 MB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================
 Package                 Arch                   Version                                 Repository            Size
===================================================================================================================
Installing:
 ntpdate                 x86_64                 4.2.6p5-29.el7.centos.2                 base                  87 k

Transaction Summary
===================================================================================================================
Install  1 Package

Total download size: 87 k
Installed size: 121 k
Downloading packages:
ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm                                                  |  87 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : ntpdate-4.2.6p5-29.el7.centos.2.x86_64                                                          1/1 
  Verifying  : ntpdate-4.2.6p5-29.el7.centos.2.x86_64                                                          1/1 

Installed:
  ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2                                                                         

Complete!
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# ntpdate us.pool.ntp.org
12 Jul 16:21:06 ntpdate[31579]: step time server 69.89.207.99 offset 113803.099491 sec
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# date
Mon Jul 12 16:21:13 CST 2021

reboot

简介:用于重启系统

poweroff

简介:用于关闭电脑

wget

格式:wget [OPTION]... [URL]...

简介:在终端命令行中下载网络文件

wget命令中的OPTION

参数 简介
-b 后台下载模式
-P 下载到指定目录
-t 最大尝试次数
-c 断点续传
-p 下载页面内所有资源,包括图片、视频等
-r 递归下载

ps

格式:ps [options]

简介: 用于查看系统中的进程状态

ps命令中的options

参数 简介
-a 显示所有进程(包括其他用户的进程)
-u 用户以及其他详细信息
-x 显示没有控制终端的进程
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# ps
  PID TTY          TIME CMD
15795 pts/0    00:00:00 sh
16135 pts/0    00:00:00 sh
16220 pts/0    00:00:00 sh
16288 pts/0    00:00:00 sh
31409 pts/0    00:00:00 bash
31589 pts/0    00:00:00 ps
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# ps -a
  PID TTY          TIME CMD
31590 pts/0    00:00:00 ps
[root@iZ2ze9fp041k3gtah0ztnmZ etc]# ps -u
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       983  0.0  0.0 110208   752 ttyS0    Ss+  Jun25   0:00 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 v
root      1163  0.0  0.0 115544  1552 tty1     Ss+  Jun25   0:00 -bash
root     15795  0.0  0.0  12012  1684 pts/0    Ss+  Jul02   0:00 /bin/sh
root     16135  0.0  0.0  12012  1684 pts/0    Ss+  Jul02   0:00 /bin/sh
root     16220  0.0  0.0  12012  1684 pts/0    Ss+  Jul02   0:00 /bin/sh
root     16288  0.0  0.0  12012  1680 pts/0    Ss+  Jul02   0:00 /bin/sh
root     31409  0.0  0.1 115684  2180 pts/0    Ss   15:49   0:00 -bash
root     31591  0.0  0.0 155452  1856 pts/0    R+   16:29   0:00 ps -u
[root@iZ2ze9fp041k3gtah0ztnmZ etc]#  ps -x
  PID TTY      STAT   TIME COMMAND
    1 ?        Ss     0:11 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    2 ?        S      0:00 [kthreadd]
    4 ?        S<     0:00 [kworker/0:0H]
    6 ?        S      0:06 [ksoftirqd/0]
    7 ?        S      0:00 [migration/0]
    8 ?        S      0:00 [rcu_bh]
    9 ?        R      2:14 [rcu_sched]

Linux系统中时刻运行着许多进程,如果能够合理地管理它们,则可以优化系统的性能。五种常见的进程状态,分别为运行、中断、不可中断、僵死与停止,其各自含义如下所示。

R(运行):进程正在运行或在运行队列中等待。

S(中断):进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该状态。

D(不可中断):进程不响应系统异步信号,即便用kill命令也不能将其中断。

Z(僵死):进程已经终止,但进程描述符依然存在, 直到父进程调用wait4()系统函数后将进程释放。

T(停止):进程收到停止信号后停止运行。

除了上面的五种常见进程状态,还有可能是高优先级(<),低优先级(N),被锁进内存(L),包含子进程(s)以及多线程(l)这五种补充形式。

pstree

格式:pstree [ -a ] [ -c ] [ -h | -H PID ] [ -l ] [ -n ] [ -p ] [ -g ] [ -u ] [ -A | -G | -U ] [ PID | USER ]
pstree -V

简介: 以树状图的形式展示进程之间的关系

[root@iZ2ze9fp041k3gtah0ztnmZ etc]# pstree
systemd─┬─AliYunDun───24*[{AliYunDun}]
        ├─AliYunDunUpdate───5*[{AliYunDunUpdate}]
        ├─agetty
        ├─aliyun-service───6*[{aliyun-service}]
        ├─assist_daemon───7*[{assist_daemon}]
        ├─atd
        ├─auditd───{auditd}
        ├─chronyd
        ├─containerd───9*[{containerd}]
        ├─3*[containerd-shim─┬─sh]
        │                    └─13*[{containerd-shim}]]
        ├─containerd-shim─┬─sh
        │                 └─12*[{containerd-shim}]
        ├─crond
        ├─dbus-daemon
        ├─dhclient
        ├─dockerd───11*[{dockerd}]
        ├─login───bash
        ├─lvmetad
        ├─master─┬─pickup
        │        └─qmgr
        ├─polkitd───6*[{polkitd}]
        ├─rsyslogd───2*[{rsyslogd}]
        ├─sshd───sshd───bash───pstree
        ├─systemd-journal
        ├─systemd-logind
        ├─systemd-udevd
        └─tuned───4*[{tuned}]

top

格式:top -hv | -bcHiOSs -d secs -n max -u|U user -p pid(s) -o field -w [cols]

简介: 动态地监视进程活动及系统负载等信息

[root@iZ2ze9fp041k3gtah0ztnmZ etc]# top
top - 16:53:26 up 17 days,  4:07,  2 users,  load average: 0.00, 0.04, 0.05
Tasks:  83 total,   1 running,  82 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1881820 total,   173328 free,   265696 used,  1442796 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  1442520 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                        
 1064 root      10 -10  145112  25132   6156 S  0.7  1.3 190:18.74 AliYunDun                                      
    1 root      20   0  125508   3516   2052 S  0.0  0.2   0:11.07 systemd                                        
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd                                       
    4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H                                   
    6 root      20   0       0      0      0 S  0.0  0.0   0:06.10 ksoftirqd/0                                    
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0                                    
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh                                         
    9 root      20   0       0      0      0 S  0.0  0.0   2:14.68 rcu_sched                                      
   10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain                                  
   11 root      rt   0       0      0      0 S  0.0  0.0   0:04.40 watchdog/0                                     
   13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs                                            

top命令执行结果的前5行为系统整体的统计信息,其所代表的含义如下。

第1行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为1分钟、5分钟、15分钟内的平均值,数值越小意味着负载越低)。

第2行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数。

第3行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比等。其中数据均为CPU数据并以百分比格式显示,例如“99.9 id”意味着有99.9%的CPU处理器资源处于空闲。

第4行:物理内存总量、内存空闲量、内存使用量、作为内核缓存的内存量。

第5行:虚拟内存总量、虚拟内存空闲量、虚拟内存使用量、已被提前加载的内存量。

nice

格式:nice [OPTION] [COMMAND [ARG]...]

简介: 用于调整进程的优先级

pidof

格式: pidof [参数] 服务名称

简介: 查询某个指定服务进程的PID号码值

[root@iZ2ze9fp041k3gtah0ztnmZ etc]# pidof sshd
31407 965

kill

格式: kill [参数] 进程的PID

简介: 用于终止某个指定PID值的服务进程,如果提示进程无法被终止,则可以添加-9表示最高级别强制杀死进程

killall

格式:killall [-Z CONTEXT] [-u USER] [ -eIgiqrvw ] [ -SIGNAL ] NAME...

? killall -l, --list
? killall -V, --version

简介: 终止某个指定名称的服务所对应的全部进程 ,通常复杂软件的服务程序会有多个进程协同工作

系统状态检测命令

ifconfig

格式: ifconfig [参数] [网络设备]

简介: 获取网卡配置与网络状态等信息

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# ifconfig
eth0: flags=4163  mtu 1500
        inet 172.26.145.205  netmask 255.255.240.0  broadcast 172.26.159.255
        inet6 fe80::216:3eff:fe0e:aaee  prefixlen 64  scopeid 0x20
        ether 00:16:3e:0e:aa:ee  txqueuelen 1000  (Ethernet)
        RX packets 3032430  bytes 3718282421 (3.4 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1141936  bytes 208838256 (199.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 46  bytes 16489 (16.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 46  bytes 16489 (16.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

uname

格式: uname [-a]

简介: 用于查看系统内核版本与系统架构等信息

#内核名称
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# uname
Linux
#内核名称、主机名、内核发行版本、节点名、压制时间、硬件名称、硬件平台、处理器类型以及操作系统名称等信息
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# uname -a
Linux iZ2ze9fp041k3gtah0ztnmZ 3.10.0-1160.25.1.el7.x86_64 #1 SMP Wed Apr 28 21:49:45 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
#当前系统版本信息
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

uptime

简介: 查看系统的负载信息 。 显示当前系统时间、系统已运行时间、启用终端数量以及平均负载值等信息。平均负载值指的是系统在最近1分钟、5分钟、15分钟内的压力情况(下面加粗的信息部分),负载值越低越好

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# uptime
 09:22:08 up 17 days, 20:36,  2 users,  load average: 0.00, 0.01, 0.05

负载值越低越好指的是对运维人员来讲的,越低越安全省心。但是公司购置的硬件设备如果长期处于空闲状态,明显是种资源浪费,老板也不会开心。所以我们建议是负载值保持在1左右,不要超过5就好。

free

格式: free [-h]

简介: 用于显示当前系统中内存的使用量信息

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1881820      264348      101652         792     1515820     1443300
Swap:             0           0           0
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# free -h
#			 内存总量	    已用量	     空闲量 进程共享的内存量  缓存的内存量	可用量
              total        used        free      shared  buff/cache   available
Mem:           1.8G        258M         99M        792K        1.4G        1.4G
Swap:            0B          0B          0B

who

简介: 用于查看当前登入主机的用户终端信息

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# who
root     tty1         2021-06-25 12:47
root     pts/0        2021-07-13 09:10 

last

简介: 调取主机的被访记录

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# last
root     pts/0        111.162.142.170  Tue Jul 13 09:10   still logged in   
root     pts/0        111.162.139.36   Mon Jul 12 15:50 - 02:10  (10:19)    
root     pts/0        111.162.139.36   Mon Jul 12 14:16 - 15:50  (01:34)    
root     pts/0        117.14.112.82    Mon Jul  5 17:25 - 17:28  (00:03)    
root     tty1                          Fri Jun 25 12:47   still logged in   
reboot   system boot  3.10.0-1160.25.1 Fri Jun 25 20:46 - 09:39 (17+12:52)  
reboot   system boot  3.10.0-1160.25.1 Fri Jun 25 20:44 - 12:46  (-7:-58)   

wtmp begins Fri May 21 12:08:55 2021

ping

格式: ping [参数] 主机地址

简介: 测试主机之间的网络连通性

参数 作用
-c 总共发送次数
-l 指定网卡名称
-i 每次间隔时间(秒)
-W 最长等待时间(秒)
#ping 百度
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# ping -c 4 www.baidu.com
PING www.a.shifen.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=50 time=15.2 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=2 ttl=50 time=15.1 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=3 ttl=50 time=15.1 ms
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=4 ttl=50 time=15.1 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 15.128/15.174/15.264/0.102 ms
#ping 不存在的ip
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# ping -c 4 1.2.3.4
PING 1.2.3.4 (1.2.3.4) 56(84) bytes of data.
^C
--- 1.2.3.4 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 2999ms

tracepath

格式: tracepath [参数] 域名

简介: 用于显示数据包达到目的主机途中所经过的路由信息

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# tracepath www.baidu.com
 1?: [LOCALHOST]                                         pmtu 1500
 1:  10.36.100.190                                         1.564ms 
 1:  10.36.100.190                                         1.370ms 
 2:  10.36.92.21                                           5.139ms 
 3:  10.255.59.165                                         1.099ms 
 4:  10.102.248.246                                       11.248ms 
 5:  116.251.94.21                                         3.104ms 
 6:  211.90.1.49                                           7.890ms asymm  8 
 7:  61.55.133.161                                         7.219ms 
 8:  202.99.167.13                                         7.916ms 
 9:  61.182.184.105                                       12.374ms 
10:  221.194.45.142                                       15.070ms asymm 11 
11:  221.194.45.154                                       15.738ms 
12:  221.194.45.142                                       16.489ms asymm 11 
13:  no reply
14:  no reply
15:  no reply
16:  no reply
^C

netstat

格式: netstat [参数]

简介: 显示如网络连接、路由表、接口状态等的网络相关信息

参数 作用
-a 显示所有连接中的Socket
-p 显示正在使用的Socket信息
-t 显示TCP协议的连接状态
-u 显示UDP协议的连接状态
-n 使用IP地址,不使用域名
-l 仅列出正在监听的服务状态
-i 现在网卡列表信息
-r 显示路由表信息
#网络状况
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 iZ2ze9fp041k3gtah:35168 100.100.30.25:http      ESTABLISHED
tcp        0     36 iZ2ze9fp041k3gtah0z:ssh dns170.online.tj.:63648 ESTABLISHED
tcp        0      0 iZ2ze9fp041k3gtah:39790 100.100.18.120:https    TIME_WAIT  
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN     
udp        0      0 0.0.0.0:bootpc          0.0.0.0:*                          
udp        0      0 localhost:323           0.0.0.0:*                          
udp6       0      0 localhost:323           [::]:*   
#网卡
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# netstat -i
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
br-6ca40081787b  1500        4      0      0 0            13      0      0      0 BMRU
docker0          1500    49143      0      0 0         56707      0      0      0 BMRU
eth0             1500  3034046      0      0 0       1143331      0      0      0 BMRU
lo              65536       46      0      0 0            46      0      0      0 LRU
veth9478981      1500        7      0      0 0            17      0      0      0 BMRU
veth2cd6e72      1500        4      0      0 0            13      0      0      0 BMRU
veth4bef9d1      1500       27      0      0 0            35      0      0      0 BMRU
veth877df8c      1500        8      0      0 0            16      0      0      0 BMRU
veth93a2e9d      1500        4      0      0 0            12      0      0      0 BMRU

history

格式: history [-c]

简介: 用于显示执行过的命令历史 ,可以用“!编码数字” 来重复执行某一次命令 -c清空历史命令

[root@iZ2ze9fp041k3gtah0ztnmZ ~]# history
 1010  last
 1011  ping -c 4 www.baidu.com
 1012  ping -c 4 1.2.3.4
 1013  clear
 1014  tracepath www.baidu.com
 1015  netstat -a
 1016  betstat -i
 1017  netstat -i
 1018  netstat
 1019  history
 #重复执行某一次命令
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# !1011
ping -c 4 www.baidu.com
PING www.a.shifen.com (110.242.68.4) 56(84) bytes of data.
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=1 ttl=50 time=16.8 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=2 ttl=50 time=16.9 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=3 ttl=50 time=16.8 ms
64 bytes from 110.242.68.4 (110.242.68.4): icmp_seq=4 ttl=50 time=16.8 ms

--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 16.858/16.882/16.912/0.094 ms
#清除历史记录
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# history -c
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# history
   22  history

查找定位文件命令

pwd

简介: 显示用户当前所处的工作目录

[root@iZ2ze9fp041k3gtah0ztnmZ local]# pwd
/usr/local

cd

格式: cd [参数] [目录]

简介: 切换当前的工作路径 。

命令 作用
cd - 返回上一次所处目录
cd.. 进入上级目录
cd ~ 切换到当前用户的home目录
cd ~username 切换到其他用户的home目录
[root@iZ2ze9fp041k3gtah0ztnmZ local]# cd -
/root
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# cd /usr/local/
[root@iZ2ze9fp041k3gtah0ztnmZ local]# cd ..
[root@iZ2ze9fp041k3gtah0ztnmZ usr]# cd ~
[root@iZ2ze9fp041k3gtah0ztnmZ ~]# pwd
/root
[root@iZ2ze9fp041k3gtah0ztnmZ ~]

ls

格式: ls [参数] [文件名称]

简介: 用于显示目录中的文件信息 可以用-a全部文件、-l详细信息、-d权限及属性

[root@iZ2ze9fp041k3gtah0ztnmZ local]# ls
aegis  bin  docker  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@iZ2ze9fp041k3gtah0ztnmZ local]# ls -a
.  ..  aegis  bin  docker  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@iZ2ze9fp041k3gtah0ztnmZ local]# ls -l
total 48
drwxr-xr-x  7 root root 4096 Jul  8 11:07 aegis
drwxr-xr-x. 2 root root 4096 May 21 12:06 bin
drwxr-xr-x  3 root root 4096 Jun 25 15:58 docker
drwxr-xr-x. 2 root root 4096 Apr 11  2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11  2018 games
drwxr-xr-x. 2 root root 4096 Apr 11  2018 include
drwxr-xr-x. 3 root root 4096 May 21 12:05 lib
drwxr-xr-x. 3 root root 4096 May 21 12:05 lib64
drwxr-xr-x. 2 root root 4096 Apr 11  2018 libexec
drwxr-xr-x. 2 root root 4096 Apr 11  2018 sbin
drwxr-xr-x. 7 root root 4096 Jul 12 23:45 share
drwxr-xr-x. 2 root root 4096 Apr 11  2018 src
[root@iZ2ze9fp041k3gtah0ztnmZ local]# ls -al
total 56
drwxr-xr-x. 14 root root 4096 Jun 25 15:58 .
drwxr-xr-x. 13 root root 4096 May 21 11:52 ..
drwxr-xr-x   7 root root 4096 Jul  8 11:07 aegis
drwxr-xr-x.  2 root root 4096 May 21 12:06 bin
drwxr-xr-x   3 root root 4096 Jun 25 15:58 docker
drwxr-xr-x.  2 root root 4096 Apr 11  2018 etc
drwxr-xr-x.  2 root root 4096 Apr 11  2018 games
drwxr-xr-x.  2 root root 4096 Apr 11  2018 include
drwxr-xr-x.  3 root root 4096 May 21 12:05 lib
drwxr-xr-x.  3 root root 4096 May 21 12:05 lib64
drwxr-xr-x.  2 root root 4096 Apr 11  2018 libexec
drwxr-xr-x.  2 root root 4096 Apr 11  2018 sbin
drwxr-xr-x.  7 root root 4096 Jul 12 23:45 share
drwxr-xr-x.  2 root root 4096 Apr 11  2018 src
[root@iZ2ze9fp041k3gtah0ztnmZ local]# ls -ld /etc/
drwxr-xr-x. 84 root root 4096 Jul 11 08:43 /etc/

tree

简介: 用于以树状图的形式列出目录内容及结构 ,但是这个需要安装

[root@iZ2ze9fp041k3gtah0ztnmZ local]# tree
-bash: tree: command not found
[root@iZ2ze9fp041k3gtah0ztnmZ local]# yum install tree
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
#创建test文件夹,进入test创建A和b两个文件夹
[root@iZ2ze9fp041k3gtah0ztnmZ test]# mkdir test
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cd test/
[root@iZ2ze9fp041k3gtah0ztnmZ test]# mkdir A
[root@iZ2ze9fp041k3gtah0ztnmZ test]# mkdir b
[root@iZ2ze9fp041k3gtah0ztnmZ test]# tree
.
├── A
└── b

find

格式: find [查找范围] 寻找条件

简介: 按照指定条件来查找文件所对应的位置

参数 作用
-name 匹配名称
-perm 匹配权限(mode为完全匹配,-mode为包含即可)
-user 匹配所有者
-group 匹配所有组
-mtime -n +n 匹配修改内容的时间(-n指n天以内,+n指n天以前)
-atime -n +n 匹配访问文件的时间(-n指n天以内,+n指n天以前)
-ctime -n +n 匹配修改文件权限的时间(-n指n天以内,+n指n天以前)
-nouser 匹配无所有者的文件
-nogroup 匹配无所有组的文件
-newer f1 !f2 匹配比文件f1新但比f2旧的文件
--type b/d/c/p/l/f 匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
-size 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-prune 忽略某个目录
-exec …… {}; 后面可跟用于进一步处理搜索结果的命令
[root@iZ2ze9fp041k3gtah0ztnmZ local]# find test
test
test/b
test/A
[root@iZ2ze9fp041k3gtah0ztnmZ local]# find tes*
test
test/b
test/A
[root@iZ2ze9fp041k3gtah0ztnmZ local]# find tes
find: ‘tes’: No such file or directory

在整个文件系统中找出所有归属于linuxprobe用户的文件并复制到/root/findresults目录

[root@iZ2ze9fp041k3gtah0ztnmZ local]# find / -user linuxprobe -exec cp -a {} /root/findresults/ 

locate

格式: locate 文件名称

简介: 用于按照名称快速搜索文件所对应的位置 , find精准但是效率低,locate 用于常见的、有知道大概名称的文件

#命令不存在
[root@iZ2ze9fp041k3gtah0ztnmZ local]# locate test
-bash: locate: command not found
#安装
[root@iZ2ze9fp041k3gtah0ztnmZ local]# yum install locate
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.
#第一次使用没创建索引
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# locate test
locate: can not stat () `/var/lib/mlocate/mlocate.db‘: No such file or directory
#创建索引
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# updatedb
#查看索引文件
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# ls -l /var/lib/mlocate/mlocate.db 
-rw-r----- 1 root slocate 4296306 Jul 13 11:09 /var/lib/mlocate/mlocate.db
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# 
#查询whereis
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# locate whereis
/usr/bin/whereis
/usr/share/bash-completion/completions/whereis
/usr/share/man/man1/whereis.1.gz
/var/lib/docker/overlay2/f2592fc4bb0320fb400f14d3d135260675769fa7d069317306779a41523d2c71/merged/usr/share/bash-completion/completions/whereis

whereis

格式: whereis 命令名称

简介: 按照名称快速搜索二进制程序(命令)、源代码以及帮助手册文件所对应的位置 ,也是基于 updatedb命令所生成出的索引库文件进行搜索

[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# whereis pwd
pwd: /usr/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz

which

格式: which 命令名称

简介: 用于按照指定名称快速搜索二进制程序(命令)所对应的位置 , which命令是在PATH变量所指定的路径中,按照指定条件搜索命令所在的路径。也就是说如果我们既不关心同名文件(find与locate)、也不关心命令所对应的源代码和帮助文件(whereis)

[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# which locate
/usr/bin/locate
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# which whereis
/usr/bin/whereis
[root@iZ2ze9fp041k3gtah0ztnmZ mlocate]# which find
/usr/bin/find

文本文件编辑命令

cat

格式: cat [参数] 文件名称

简介: 查看纯文本文件(内容较少的) -n表示显示行号,信息太长会不停的滚动

#创建b.json文件
[root@iZ2ze9fp041k3gtah0ztnmZ test]# vim b.json
#查看文件
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat b.json 
123123345345
123412321
#带行号
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat -n b.json 
     1	123123345345
     2	123412321
     3	

more

格式: more [参数] 文件名称

简介: 查看纯文本文件(内容较多的) ,最下方会显示百分比,空格或回车可以翻页

[root@iZ2ze9fp041k3gtah0ztnmZ sys]# cd /etc/sysconfig/
[root@iZ2ze9fp041k3gtah0ztnmZ sysconfig]# ll
total 128
-rw-r--r--. 1 root root  145 May 21 11:56 anaconda
##########省略######
-rw-r--r--  1 root root 6228 Apr  1  2020 sysstat.ioconf

[root@iZ2ze9fp041k3gtah0ztnmZ sysconfig]# more sysstat.ioconf 
#
# sysstat.ioconf
#
#  Copyright (C) 2004, Red Hat, Inc.
#
#  Maintained by Sebastien Godard (sysstat [at] orange.fr)
#
#  This file gives iostat and sadc a clue about how to find whole
#   disk devices in /proc/partitions and /proc/diskstats
#  Authoritative source is: linux/Documentation/devices.txt
#
# line format, general record:
#   major:name:ctrlpre:ctrlno:devfmt:devcnt:partpre:partcnt:description
#
#  major:		major # for device
#  name:		base of device name
#  ctrlpre:		string to use in generating controller designators
#			 eg: the c in c0d2p6, decimal formatting implied
#			‘*‘ means none or irrelevant
#			‘x‘: exception... record contains a specific name
#                       for a specific minor #, stored in the ctrlno field
#  ctrlno:		which controller of this type is this
#  devfmt:		type of device naming convention
#	a:		alpha: xxa, xxb, ... xxaa, xxab, ... xxzz
#	%string:	string to use in generating drive designators,
#			 eg: the ‘d‘ in c0d2p6 , decimal formatting implied
#	d:		no special translations (decimal formatting)
#  devcnt:		how many whole devs per major number
--More--(17%)

格式: head [参数] 文件名称

简介: 于查看纯文本文件的前N行

#只查10行
[root@iZ2ze9fp041k3gtah0ztnmZ sysconfig]# head -n 10  sysstat.ioconf 
#
# sysstat.ioconf
#
#  Copyright (C) 2004, Red Hat, Inc.
#
#  Maintained by Sebastien Godard (sysstat [at] orange.fr)
#
#  This file gives iostat and sadc a clue about how to find whole
#   disk devices in /proc/partitions and /proc/diskstats
#  Authoritative source is: linux/Documentation/devices.txt


tail

格式: tail [参数] 文件名称

简介: 查看纯文本文件的后N行或持续刷新文件的最新内容 。tail -f 文件名持续刷新一个文件的内容

#查看后十行
[root@iZ2ze9fp041k3gtah0ztnmZ sysconfig]# tail -n 10  sysstat.ioconf 
242:232:10:
243:232:11:
244:232:12:
245:232:13:
246:232:14:
247:232:15:

# 240..254: LOCAL/Experimental
# 255: reserved for big dev_t expansion
[root@iZ2ze9fp041k3gtah0ztnmZ sysconfig]# tail -f /var/log/messages
Jul 13 10:39:49 iZ2ze9fp041k3gtah0ztnmZ yum[689]: Installed: tree-1.6.0-10.el7.x86_64
Jul 13 10:40:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3058 of user root.
Jul 13 10:50:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3059 of user root.
Jul 13 11:00:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3060 of user root.
Jul 13 11:01:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3061 of user root.
Jul 13 11:05:40 iZ2ze9fp041k3gtah0ztnmZ yum[778]: Installed: mlocate-0.26-8.el7.x86_64
Jul 13 11:10:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3062 of user root.
Jul 13 11:20:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3063 of user root.
Jul 13 11:30:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3064 of user root.
Jul 13 11:40:01 iZ2ze9fp041k3gtah0ztnmZ systemd: Started Session 3065 of user root.

tr

格式: tr [原始字符] [目标字符]

简介: 用于替换文本内容中的字符

#查看文本
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat b.json 
123123345345
123412321
#替换
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat b.json | tr [1-3] [3-5]
345345545545
345434543

wc

格式: wc [参数] 文件名称

简介: 统计指定文本文件的行数、字数或字节数

参数 作用
-l 只显示行数
-w 只显示单词数
-c 只显示字节数
#统计有多少用户
[root@iZ2ze9fp041k3gtah0ztnmZ test]# wc -l /etc/passwd
23 /etc/passwd

stat

格式: stat 文件名称

简介: 查看文件的具体存储细节和时间等信息

[root@iZ2ze9fp041k3gtah0ztnmZ test]# stat b.json 
  File: ‘b.json’
  Size: 24        	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1053454     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-07-13 11:30:40.546848742 +0800
Modify: 2021-07-13 11:30:33.073625205 +0800
Change: 2021-07-13 11:30:33.073625205 +0800
 Birth: -
#Access Time(最后一次内容被访问的时间,简称Atime)
#Modify Time(最后一次内容被修改的时间,简称Mtime)
#Change Time(文件属性最后一次被修改的时间,简称Ctime)

grep

格式: grep [参数] 文件名称

简介: 用于按行提取文本内容 最常用两个参数-n-v

参数 作用
-b 将可执行文件(binary)当作文本文件(text)来搜索
-c 仅显示找到的行数
-i 忽略大小写
-n 显示行号
-v 反向选择——仅列出没有“关键词”的行。
#查找出当前系统中不允许登录系统的所有用户信息:
[root@iZ2ze9fp041k3gtah0ztnmZ test]# grep /sbin/nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
dockerroot:x:997:994:Docker User:/var/lib/docker:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
#带行号
[root@iZ2ze9fp041k3gtah0ztnmZ test]# grep -n /sbin/nologin /etc/passwd
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10:operator:x:11:0:operator:/root:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:chrony:x:998:996::/var/lib/chrony:/sbin/nologin
20:nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
21:tcpdump:x:72:72::/:/sbin/nologin
22:dockerroot:x:997:994:Docker User:/var/lib/docker:/sbin/nologin
23:ntp:x:38:38::/etc/ntp:/sbin/nologin
#结果行数
[root@iZ2ze9fp041k3gtah0ztnmZ test]# grep -c /sbin/nologin /etc/passwd
19
#当前目录中,查找后缀有 file 字样的文件中包含 test 字符串的文件
[root@iZ2ze9fp041k3gtah0ztnmZ test]# grep 123 *.json
123123345345
123412321

cut

格式: cut [参数] 文件名称

简介: 按列提取文本内容

#以行的形式查看
[root@iZ2ze9fp041k3gtah0ztnmZ test]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
#`-d`指定间隔符号,`-f`指定要查看的列数
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cut -d : -f 1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
chrony
nscd
tcpdump
dockerroot
ntp

diff

格式: diff [参数] 文件名称A 文件名称B

简介: 比较多个文件之间内容的差异 -c描述文件的具体不同 --brief判断文件是否相同

[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat b.json 
123123345345
123412321

[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat c.json 
123123345345
3412321

[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat d.json 
123123345345
123412321

[root@iZ2ze9fp041k3gtah0ztnmZ test]# 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# diff --brief b.json c.json 
Files b.json and c.json differ
[root@iZ2ze9fp041k3gtah0ztnmZ test]# diff --brief b.json d.json
#没输出
[root@iZ2ze9fp041k3gtah0ztnmZ test]#

[root@iZ2ze9fp041k3gtah0ztnmZ test]# diff  b.json d.json 
[root@iZ2ze9fp041k3gtah0ztnmZ test]# diff  b.json c.json 
2c2
< 123412321
---
> 3412321
[root@iZ2ze9fp041k3gtah0ztnmZ test]# diff -c  b.json c.json 
*** b.json	2021-07-13 11:30:33.073625205 +0800
--- c.json	2021-07-13 13:01:17.804477845 +0800
***************
*** 1,3 ****
  123123345345
! 123412321
  
--- 1,3 ----
  123123345345
! 3412321


uniq

格式: uniq [参数] 文件名称

简介: 去除文本中连续的重复行

#只能去除连续的重复行
[root@iZ2ze9fp041k3gtah0ztnmZ test]# cat e.json 
zhao56
zhao56
zhao45
zhao56
hahaha
123
[root@iZ2ze9fp041k3gtah0ztnmZ test]# uniq e.json 
zhao56
zhao45
zhao56
hahaha
123

sort

格式: sort [参数] 文件名称

简介: 对文本内容进行再排序

参数 作用
-f 忽略大小写
-b 忽略缩进与空格
-n 以数值型排序
-r 反向排序
-u 去除重复行
-t 指定间隔符
-k 设置字段范围

原文:https://www.cnblogs.com/zhao56/p/15007220.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐