您的位置:首页 > 运维架构 > Linux

【Linux就该这么学 20期培训笔记 02】新手必须掌握的 Linux 命令

2019-05-05 22:12 1211 查看

本章知识点

  1. 强大好用的SHELL
  2. 执行查看帮助命令
  3. 常用系统工作命令
  4. 系统状态检测命令
  5. 工作目录切换命令
  6. 文本文件编辑命令
  7. 文件目录管理命令
  8. 打包压缩与搜索命令

Linux命令大全(手册)

强大好用的SHELL

  • Shell (也称为终端)充当的是人与内核(硬件)之间的翻译官

  • 主流
    Linux
    系统默认使用的
    Shell
    Bash
    1:通过上下方向键来调取过往执行过的Linux命令;
    2:命令或参数仅需输入前几位就可以用Tab键补全;
    3:具有强大的批处理脚本;
    4:具有实用的环境变量功能。

执行查看帮助命令

  • 常见执行Linux命令的格式:

    命令名称 [命令参数] [命令对象]

    注意,命令名称、命令参数、命令对象之间请用空格键分隔。

  • 命令参数的长格式与短格式

短格式+短格式:可合并
长格式+短桥式、长格式+长格式:不可以合并

  • man 命令可查看关于命令的帮助信息
man <需要帮助的命令>

常用系统工作命令

1. echo命令

echo命令用于在终端输出字符串或变量提取后的值,格式为“echo [字符串 | $变量]”

[root@linuxprobe ~]# echo $SHELL
/bin/bash

2.date命令

date命令用于显示及设置系统的时间或日期,格式为“date [选项] [+指定的格式]”

  • 按照“年-月-日 小时:分钟:秒”的格式查看当前系统时间

    [root@linuxprobe ~]# date "+%Y-%m-%d %H:%M:%S"
    2017-08-24 16:29:12
  • 将系统的当前时间设置为2017年9月1日8点30分

    [root@linuxprobe ~]# date -s "20170901 8:30:00"
    Fri Sep 1 08:30:00 CST 2017
  • 使用date命令并按照默认的格式查看当前的系统时间

    [root@linuxprobe ~]# date
    Fri Sep 1 08:30:01 CST 2017
  • date命令中的参数%j可用来查看今天是当年中的第几天
    [root@linuxprobe ~]# date "+%j"
    244

3.reboot命令

reboot命令用于重启系统,其格式为reboot。

  • 由于重启计算机这种操作会涉及硬件资源的管理权限,因此默认只能使用root管理员来重启,其命令如下
    [root@linuxprobe ~]# reboot

4.poweroff命令

poweroff命令用于关闭系统,其格式为poweroff。

  • 该命令与reboot命令相同,都会涉及硬件资源的管理权限,因此默认只有root管理员才可以关闭电脑,其命令如下:
    [root@linuxprobe ~]# poweroff

5.wget命令

wget命令用于在终端中下载网络文件,格式为“wget [参数] 下载地址”

  • 尝试使用wget命令从本书的配套站点中下载本书的最新pdf格式电子文档,这个文件的完整路径为https://www.linuxprobe.com/docs/LinuxProbe.pdf, 执行该命令后的下载效果如下:
    [root@linuxprobe ~]# wget https://www.linuxprobe.com/docs/LinuxProbe.pdf
    --2017-08-24 19:30:12 -- https://www.linuxprobe.com/docs/LinuxProbe.pdf
    Resolving www.linuxprobe.com (www.linuxprobe.com)... 220.181.105.185
    Connecting to www.linuxprobe.com (www.linuxprobe.com)|220.181.105.185|:80...connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 45948568 (44M) [application/pdf]
    Saving to: ‘LinuxProbe.pdf’
    100%[===========================================>] 45,948,568 32.9MB/s in 1.3s
    2017-08-24 19:30:14 (32.9 MB/s) - ‘LinuxProbe.pdf’ saved [45948568/45948568]

6.ps命令

ps命令用于查看系统中的进程状态,格式为“ps [参数]”

进程状态含义如下所示:
R(运行):进程正在运行或在运行队列中等待。
S(中断):进程处于休眠中,当某个条件形成后或者接收到信号时,则脱离该 状态。
D(不可中断):进程不响应系统异步信号,即便用kill命令也不能将其中断。
Z(僵死):进程已经终止,但进程描述符依然存在, 直到父进程调用wait4()系统函数后将进程释放。
T(停止):进程收到停止信号后停止运行。

7.top命令

top命令用于动态地监视进程活动与系统负载等信息,其格式为top

第1行:系统时间、运行时间、登录终端数、系统负载(三个数值分别为1分钟、5分钟、15分钟内的平均值,数值越小意味着负载越低)。
第2行:进程总数、运行中的进程数、睡眠中的进程数、停止的进程数、僵死的进程数。
第3行:用户占用资源百分比、系统内核占用资源百分比、改变过优先级的进程资源百分比、空闲的资源百分比等。其中数据均为CPU数据并以百分比格式显示,例如“97.1 id”意味着有97.1%的CPU处理器资源处于空闲。
第4行:物理内存总量、内存使用量、内存空闲量、作为内核缓存的内存量。
第5行:虚拟内存总量、虚拟内存使用量、虚拟内存空闲量、已被提前加载的内存量。

8.pidof命令

pidof命令用于查询某个指定服务进程的PID值,格式为“pidof [参数] [服务名称]”

  • 每个进程的进程号码值(PID)是唯一的,因此可以通过PID来区分不同的进程。例如,可以使用如下命令来查询本机上sshd服务程序的PID:
    [root@linuxprobe ~]# pidof sshd
    2156

9.kill命令

kill命令用于终止某个指定PID的服务进程,格式为“kill [参数] [进程PID]”

  • 接下来,我们使用kill命令把上面用pidof命令查询到的PID所代表的进程终止掉,其命令如下所示。这种操作的效果等同于强制停止sshd服务
    [root@linuxprobe ~]# kill 2156

10.killall命令

killall命令用于终止某个指定名称的服务所对应的全部进程,格式为:“killall [参数] [服务名称]”

[root@linuxprobe ~]# pidof httpd
13581 13580 13579 13578 13577 13576
[root@linuxprobe ~]# killall httpd
[root@linuxprobe ~]# pidof httpd
[root@linuxprobe ~]#

系统状态检测命令

1.ifconfig命令

ifconfig命令用于获取网卡配置与网络状态等信息,格式为“ifconfig [网络设备] [参数]”

[root@linuxprobe ~]# ifconfig
eno16777728: flags=4163  mtu 1500
inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
inet6 fe80::20c:29ff:fec4:a409  prefixlen 64  scopeid 0x20
ether 00:0c:29:c4:a4:09  txqueuelen 1000  (Ethernet)
RX packets 36  bytes 3176 (3.1 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 38  bytes 4757 (4.6 KiB)
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 0  (Local Loopback)
RX packets 386  bytes 32780 (32.0 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 386  bytes 32780 (32.0 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.uname命令

uname命令用于查看系统内核与系统版本等信息,格式为“uname [-a]”

[root@linuxprobe ~]# uname -a
Linux linuxprobe.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux

顺带一提,如果要查看当前系统版本的详细信息,则需要查看redhat-release文件,其命令以及相应的结果如下:

[root@linuxprobe ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)

3.uptime命令

uptime用于查看系统的负载信息,格式为uptime

[root@linuxprobe ~]# uptime
22:49:55 up 10 min, 2 users, load average: 0.01, 0.19, 0.18

4.free命令

free用于显示当前系统中内存的使用量信息,格式为“free [-h]”

[root@linuxprobe ~]# free -h

5.who命令

who用于查看当前登入主机的用户终端信息,格式为“who [参数]”

[root@linuxprobe ~]# who

6.last命令

last命令用于查看所有系统的登录记录,格式为“last [参数]”

[root@linuxprobe ~]# last
root       pts/0       :0               Mon Aug  24 17:52   still  logged in
root       :0          :0               Mon Aug  24 17:52   still  logged in
(unknown   :0          :0               Mon Aug  24 17:50 - 17:52   (00:02)
reboot     system boot 3.10.0-123.el7.x Tue Aug  25 01:49 - 18:17   (-7:-32)
root       pts/0       :0               Mon Aug  24 15:40 - 08:54   (7+17:14)
root       pts/0       :0               Fri Jul  10 10:49 - 15:37   (45+04:47)
………………省略部分登录信息………………

7.history命令

history命令用于显示历史执行过的命令,格式为“history”

  • 执行history命令能显示出当前用户在本地计算机中执行过的最近 1000 条命令记录。如果觉得1000不够用,还可以自定义
    /etc/profile
    文件中的HISTSIZE变量值。
  • 在使用history命令时,如果使用 -c 参数则会清空所有的命令历史记录。
  • 还可以使用 !编码数字 的方式来重复执行某一次的命令。

8.sosreport命令

sosreport命令用于收集系统配置及架构信息并输出诊断文档,格式为sosrepor

[root@linuxprobe ~]# sosreport
sosreport (version 3.0)
This command will collect diagnostic and configuration information from
this Red Hat Enterprise Linux system and installed applications.

An archive containing the collected information will be generated in
/var/tmp and may be provided to a Red Hat support representative.
Any information provided to Red Hat will be treated in accordance with
the published support policies at:

https://access.redhat.com/support/

The generated archive may contain data considered sensitive and its
content should be reviewed by the originating organization before being
passed to any third party.

No changes will be made to system configuration.
Press ENTER to continue, or CTRL-C to quit. 此处敲击回车来确认收集信息

Please enter your first initial and last name [linuxprobe.com]:此处敲击回车来确认主机编号
Please enter the case number that you are generating this report for:此处敲击回车来确认主机编号
Running plugins. Please wait ...
Running 70/70: yum...
Creating compressed archive...
Your sosreport has been generated and saved in:

/var/tmp/sosreport-linuxprobe.com-20170905230631.tar.xz
The checksum is: 79436cdf791327040efde48c452c6322
Please send this file to your support representative.

工作目录切换命令

1.pwd命令

pwd命令用于显示用户当前所处的工作目录,格式为“pwd [选项]”

[root@linuxprobe etc]# pwd
/etc

2.cd命令

cd命令用于切换工作路径,格式为“cd [目录名称]”

  • 使用
    cd -
    命令返回到上一次所处的目录
  • 使用
    cd..
    命令进入上级目录
  • 使用
    cd ~
    命令切换到当前用户的家目录

3.ls命令

ls命令用于显示目录中的文件信息,格式为“ls [选项] [文件]

  • 使用ls命令的
    -a
    参数看到全部文件(包括隐藏文件)
  • 使用
    -l
    参数可以查看文件的属性、大小等详细信息

文本文件编辑命令

1.cat命令

cat命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]”

  • 如果在查看文本内容时还想顺便显示行号的话,不妨在cat命令后面追加一个-n参数:
[root@linuxprobe ~]# cat -n initial-setup-ks.cfg
1    #version=RHEL7
2    # X Window System configuration information
3    xconfig  --startxonboot
4
5    # License agreement
6    eula --agreed
7    # System authorization information
8    auth --enableshadow --passalgo=sha512
9    # Use CDROM installation media
10    cdrom
11    # Run the Setup Agent on first boot
12    firstboot --enable
13    # Keyboard layouts
14    keyboard --vckeymap=us --xlayouts='us'
15    # System language
16    lang en_US.UTF-8
………………省略部分输出信息………………

2.more命令

more命令用于查看纯文本文件(内容较多的),格式为“more [选项]文件

[root@linuxprobe ~]# more initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
network  --bootproto=dhcp --hostname=linuxprobe.com
--More--(43%)

3.head命令

head命令用于查看纯文本文档的前N行,格式为“head [选项] [文件]”

  • 查看文本中前20行的内容:
[root@linuxprobe ~]# head -n 20  initial-setup-ks.cfg
#version=RHEL7
# X Window System configuration information
xconfig  --startxonboot

# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
# Keyboard layouts
keyboard --vckeymap=us --xlayouts='us'
# System language
lang en_US.UTF-8

ignoredisk --only-use=sda
# Network information
network  --bootproto=dhcp --device=eno16777728 --onboot=off --ipv6=auto
[root@linuxprobe ~]#

4.tail命令

tail命令用于查看纯文本文档的后N行或持续刷新内容,格式为“tail [选项] [文件]”

  • tail命令最强悍的功能是可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用,此时的命令格式为“tail -f 文件名”:
[root@linuxprobe ~]# tail -f /var/log/messages
May  4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack
May  4 07:56:38 localhost gnome-session: Window manager warning: Log level 16: STACK_OP_ADD: window 0x1e00001 already in stack
May  4 07:56:38 localhost vmusr[12982]: [ warning] [Gtk] gtk_disable_setlocale() must be called before gtk_init()
May  4 07:56:50 localhost systemd-logind: Removed session c1.
Aug  1 01:05:31 localhost systemd: Time has been changed
Aug  1 01:05:31 localhost systemd: Started LSB: Bring up/down networking.
Aug  1 01:08:56 localhost dbus-daemon: dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Aug  1 01:08:56 localhost dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Aug  1 01:08:57 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
Aug  1 01:08:57 localhost dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'

5.tr命令

tr命令用于替换文本文件中的字符,格式为“tr [原始字符] [目标字符]”

  • 例如,把某个文本内容中的英文全部替换为大写:
[root@linuxprobe ~]# cat anaconda-ks.cfg | tr [a-z] [A-Z]
#VERSION=RHEL7
# SYSTEM AUTHORIZATION INFORMATION
AUTH --ENABLESHADOW --PASSALGO=SHA512

# USE CDROM INSTALLATION MEDIA
CDROM
# RUN THE SETUP AGENT ON FIRST BOOT
FIRSTBOOT --ENABLE
IGNOREDISK --ONLY-USE=SDA
# KEYBOARD LAYOUTS
KEYBOARD --VCKEYMAP=US --XLAYOUTS='US'
# SYSTEM LANGUAGE
LANG EN_US.UTF-8

# NETWORK INFORMATION
NETWORK --BOOTPROTO=DHCP --DEVICE=ENO16777728 --ONBOOT=OFF --IPV6=AUTO
NETWORK --HOSTNAME=LOCALHOST.LOCALDOMAIN
# ROOT PASSWORD
ROOTPW --ISCRYPTED $6$PDJJF42G8C6PL069$II.PX/YFAQPO0ENW2PA7MOMKJLYOAE2ZJMZ2UZJ7BH3UO4OWTR1.WK/HXZ3XIGMZGJPCS/MGPYSSOI8HPCT8B/
# SYSTEM TIMEZONE
TIMEZONE AMERICA/NEW_YORK --ISUTC
USER --NAME=LINUXPROBE --PASSWORD=$6$A9V3INSTNBWEIR7D$JEGFYWBCDOOOKJ9SODECCDO.ZLF4OSH2AZ2SS2R05B6LZ2A0V2K.RJWSBALL2FEKQVGF640OA/TOK6J.7GUTO/ --ISCRYPTED --GECOS="LINUXPROBE"
# X WINDOW SYSTEM CONFIGURATION INFORMATION
XCONFIG --STARTXONBOOT
# SYSTEM BOOTLOADER CONFIGURATION
BOOTLOADER --LOCATION=MBR --BOOT-DRIVE=SDA
AUTOPART --TYPE=LVM
# PARTITION CLEARING INFORMATION
CLEARPART --NONE --INITLABEL

%PACKAGES
@BASE
@CORE
@DESKTOP-DEBUGGING
@DIAL-UP
@FONTS
@GNOME-DESKTOP
@GUEST-AGENTS
@GUEST-DESKTOP-AGENTS
@INPUT-METHODS
@INTERNET-BROWSER
@MULTIMEDIA
@PRINT-CLIENT
@X11

%END

6.wc命令

wc命令用于统计指定文本的行数、字数、字节数,格式为“wc [参数] 文本”

[root@linuxprobe ~]# wc -l /etc/passwd
38 /etc/passwd

7.stat命令

stat命令用于查看文件的具体存储信息和时间等信息,格式为“stat 文件名称”

[root@linuxprobe ~]# stat anaconda-ks.cfg
File: ‘anaconda-ks.cfg’
Size: 1213 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 68912908 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2017-07-14 01:46:18.721255659 -0400
Modify: 2017-05-04 15:44:36.916027026 -0400
Change: 2017-05-04 15:44:36.916027026 -0400
Birth: -

8.cut命令

cut命令用于按“列”提取文本字符,格式为“cut [参数] 文本”

  • 例如,提取以冒号(:)为间隔符的第一列的内容:
[root@linuxprobe ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
unbound
colord
usbmuxd
avahi
avahi-autoipd
libstoragemgmt
saslauth
qemu
rpc
rpcuser
nfsnobody
rtkit
radvd
ntp
chrony
abrt
pulse
gdm
gnome-initial-setup
postfix
sshd
tcpdump
linuxprobe

9.diff命令

diff命令用于比较多个文本文件的差异,格式为“diff [参数] 文件”

  • 使用cat命令分别查看diff_A.txt和diff_B.txt文件的内容,然后进行比较:
[root@linuxprobe ~]# cat diff_A.txt
Welcome to linuxprobe.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@linuxprobe ~]# cat diff_B.txt
Welcome tooo linuxprobe.com

Red Hat certified
Free Linux LeSSonS
////////.....////////
Professional guidance
Linux Course
  • 使用带有-c参数的diff命令来描述文件内容具体的不同:
[root@linuxprobe ~]# diff -c diff_A.txt diff_B.txt
*** diff_A.txt 2017-08-30 18:07:45.230864626 +0800
--- diff_B.txt 2017-08-30 18:08:52.203860389 +0800
***************
*** 1,5 ****
! Welcome to linuxprobe.com
Red Hat certified
! Free Linux Lessons
Professional guidance
Linux Course
--- 1,7 ----
! Welcome tooo linuxprobe.com
!
Red Hat certified
! Free Linux LeSSonS
! ////////.....////////
Professional guidance
Linux Course
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: