zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Linux-学习1

2023-06-13 09:11:24 时间

《Linux就该这么学》学习笔记

《Linux就该这么学》 -刘遄

第二章

一. 常用系统工作命令

1) echo命令

​ echo命令用于在终端输出字符串变量,格式”echo [字符串|$变量]”

[root@localhost ~]# echo Linuxprobe.Com
Linuxprobe.Com
[root@localhost ~]# echo $SHELL
/bin/bash

2) date命令

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

在date命令中输入以”+”号开头的参数,即可按照指定格式来输出系统的时间日期

参数

作用

%t

跳格[Tab键]

%H

小时(00~23)

%I

小时(00~12)

%M

分钟(00~59)

%S

秒(00~59)

%j

今年中的第几天

[root@localhost ~]# date
Thu Apr 22 18:07:48 CST 2021
[root@localhost ~]# date "+%Y-%m-%d%t%H:%M:%S"
2021-04-22    18:08:20
[root@localhost ~]# date "+%j"        #今年第几天
112

3) reboot命令

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

该命令只能使用root管理员来重启

[root@localhost ~]# reboot

4) poweroff命令

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

该命令只能使用root管理员来关闭

poweroff,halt和shutdown -h now

poweroff就是掉电关机了,但实际过程依然是先关闭系统,然后掉电的。

halt是停掉系统而已,并没有掉电,主板是通电的。

shutdown -h now就是halt,后边不写now而是写一个具体时间可以设置几秒后执行这一语句,实际上halt后是掉电关机的。

[root@localhost ~]# poweroff

5) wget命令

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

参数

作用

-b

后台下载模式

-P

下载到指定目录

-t

最大尝试次数

-c

断点续传

-p

下载页面内所有资源,包括图片,视频等

-r

递归下载

尝试下载《Linux就该这么学》这本书的最新pdf格式电子文档,命令

[root@localhost Documents]# wget http://www.linuxprobe.com/docs/LinuxProbe.pdf
--2021-04-22 18:18:52--  http://www.linuxprobe.com/docs/LinuxProbe.pdf
Resolving www.linuxprobe.com (www.linuxprobe.com)... 222.192.186.99, 222.192.186.104, 222.192.186.100, ...
Connecting to www.linuxprobe.com (www.linuxprobe.com)|222.192.186.99|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.linuxprobe.com/docs/LinuxProbe.pdf [following]
--2021-04-22 18:18:55--  https://www.linuxprobe.com/docs/LinuxProbe.pdf
Connecting to www.linuxprobe.com (www.linuxprobe.com)|222.192.186.99|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17676281 (17M) [application/pdf]
Saving to: ‘LinuxProbe.pdf’

LinuxProbe.pdf            100%[===================================>]  16.86M   916KB/s    in 72s     

2021-04-22 18:20:07 (241 KB/s) - ‘LinuxProbe.pdf’ saved [17676281/17676281]

6) ps命令

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

参数

作用

-a

显示所有进程(包括其他用户的进程)

-u

用户以及其他详细信息

-x

显示没有控制终端的进程

Linux系统中有五种常见的进程状态,分别为运行,中断,不可中断,僵死与停止

状态

含义

R(运行)

进程正在运行或在运行队列中等待

S(中断)

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

D(不可中断)

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

Z(僵死)

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

T(停止)

进程收到停止信号后停止运行

以下节选自ps的man手册

Here are the different values that the s, stat and state output specifiers (header "STAT" or
"S") will display to describe the state of a process:
    D    uninterruptible sleep (usually IO)
    I    Idle kernel thread
    R    running or runnable (on run queue)
    S    interruptible sleep (waiting for an event to complete)
    T    stopped by job control signal
    t    stopped by debugger during the tracing
    W    paging (not valid since the 2.6.xx kernel)
    X    dead (should never be seen)
    Z    defunct ("zombie") process, terminated but not reaped by its parent

For BSD formats and when the stat keyword is used, additional characters may be displayed:
    <    high-priority (not nice to other users)
    N    low-priority (nice to other users)
    L    has pages locked into memory (for real-time and custom IO)
    s    is a session leader
    l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
    +    is in the foreground process group

7) top命令

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

top命令可以看作Linux中的“强化版的Windows任务管理器”。

top - 18:41:43 up 43 min,  1 user,  load average: 0.00, 0.00, 0.02
Tasks: 198 total,   1 running, 197 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.1 sy,  0.0 ni, 99.6 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
MiB Mem :   1800.6 total,    175.8 free,    998.3 used,    626.6 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.    634.8 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                       
   1160 mysql     20   0 1773308 371984  34524 S   0.6  20.2   0:18.12 mysqld                       
   2810 root      20   0   65484   5172   4308 R   0.6   0.3   0:00.90 top                           
   2068 gdm       20   0  473968  12312  10560 S   0.3   0.7   0:00.26 gsd-smartcard                 
      1 root      20   0  245300  14072   9148 S   0.0   0.8   0:06.81 systemd                       
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.01 kthreadd                     
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp                       
      4 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_par_gp                   
      6 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/0:0H-kblockd         
      7 root      20   0       0      0      0 I   0.0   0.0   0:00.15 kworker/u256:0-events_unbound 
      8 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 mm_percpu_wq                 
      9 root      20   0       0      0      0 S   0.0   0.0   0:00.05 ksoftirqd/0                   
     10 root      20   0       0      0      0 I   0.0   0.0   0:00.82 rcu_sched                     
     11 root      rt   0       0      0      0 S   0.0   0.0   0:00.00 migration/0                   
     12 root      rt   0       0      0      0 S   0.0   0.0   0:00.00 watchdog/0                   
     13 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/0                       
     14 root      20   0       0      0      0 S   0.0   0.0   0:00.00 cpuhp/1                       
     15 root      rt   0       0      0      0 S   0.0   0.0   0:00.00 watchdog/1                   
     16 root      rt   0       0      0      0 S   0.0   0.0   0:00.00 migration/1                   
     17 root      20   0       0      0      0 S   0.0   0.0   0:00.07 ksoftirqd/1                   
     19 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 kworker/1:0H-kblockd

第一行:系统时间,运行时间,登录终端数,系统负载(三个数值分别是1分钟,5分钟,15分钟内的平均值,三个值越低负载越低)

第二行:进程总数,运行中的进程数,睡眠中的进程数,停止的进程数,僵死的进程数

第三行:用户占用系统资源百分比,系统内核占用资源百分比,改变过优先级的进程资源百分比,空闲的资源百分比等。其中”97.1 id”意味着有97.1%的CPU处理器资源出于空闲。

0.0% us 用户空间占用CPU的百分比。 0.1% sy 内核空间占用CPU的百分比。 0.0% ni 改变过优先级的进程占用CPU的百分比 99.6% id 空闲CPU百分比 0.0% wa IO等待占用CPU的百分比 0.0% hi 硬中断(Hardware IRQ)占用CPU的百分比 0.0% si 软中断(Software Interrupts)占用CPU的百分比

第四行:物理内存总量,内存空闲量,内存使用量,作为内核缓存的内存量

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

8) pidof命令

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

每个进程的PID是唯一的

[root@localhost ~]# pidof sshd		#查询sshd服务的PID
2546 2519 1076

9) kill命令

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

[root@localhost ~]# kill 2156

10) killall命令

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

[root@localhost ~]# killall httpd

注:执行命令的最后加一个&符号,这样命令将进入系统后台来执行


二. 系统状态检测命令

1) ifconfig命令

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

2) uname命令

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

[root@localhost ~]# uname -a
Linux localhost.localdomain 4.18.0-193.el8.x86_64 #1 SMP Fri May 8 10:59:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

查看系统版本的详细信息,则需要查看redhat-release文件

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 8.2.2004 (Core)**

3) uptime命令

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

[root@localhost ~]# uptime
19:18:41 up  1:20,  1 user,  load average: 0.08, 0.08, 0.03

4) free命令

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

[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:          1.8Gi       1.0Gi       131Mi        10Mi       644Mi       607Mi
Swap:         2.0Gi          0B       2.0Gi

交换区swap:

swap 分区通常被称为交换分区,这是一块特殊的硬盘空间,即当实际内存不够用的时候,操作系统会从内存中取出一部分暂时不用的数据,放在交换分区中,从而为当前运行的程序腾出足够的内存空间。也就是说,当内存不够用时,我们使用 swap 分区来临时顶替。

Linux内核为了提高读写效率与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory(缓存内存)。即使你的程序运行结束后,Cache Memory也不会自动释放。这就会导致你在Linux系统中程序频繁读写文件后,你会发现可用物理内存变少。当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap分区中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。

5) who命令

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

[root@localhost ~]# who
root     pts/0        2021-04-22 18:22 (192.168.52.1)

6) last命令

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

[root@localhost ~]# last
root     pts/0        192.168.52.1     Thu Apr 22 18:22   still logged in
root     pts/0        192.168.52.1     Thu Apr 22 18:03 - 18:22  (00:18)
reboot   system boot  4.18.0-193.el8.x Thu Apr 22 17:58   still running
root     pts/0        192.168.52.1     Thu Apr 22 14:57 - 17:12  (02:15)
...

7) history命令

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

8) sosreport命令

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


三. 工作目录切换命令

1) pwd命令

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

2) cd命令

切换工作目录,格式为”cd[目录名称]”

3) ls命令

显示目录文件信息,格式为”ls [选项] [文件]”

参数

含义

-a

显示全部文件(包括隐藏文件)

-l

查看文件的属性,大小等详细信息

-d

查看目录信息

[root@localhost ~]# ls -la
total 108
dr-xr-x---. 16 root root  4096 Apr 22 18:22 .
dr-xr-xr-x. 17 root root   224 Apr 16 20:44 ..
-rw-------.  1 root root  1443 Jan 21 10:32 anaconda-ks.cfg
-rw-------.  1 root root 17010 Apr 22 18:22 .bash_history
-rw-r--r--.  1 root root    18 May 11  2019 .bash_logout
-rw-r--r--.  1 root root   176 May 11  2019 .bash_profile
-rw-r--r--.  1 root root   176 May 11  2019 .bashrc
...

四. 文本文件编辑命令

1) cat命令

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

注:如果要显示行号的话,cat后面加一个”-n”参数

[root@localhost echo_server]# cat hello.txt -n
     1	Hi,
     2	This is dopamine.

2) more命令

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

3) head命令

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

具体见man手册

4) tail命令

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

5) tr命令

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

例,把文本中的所有英文替换成大写

[root@localhost echo_server]# cat hello.txt 
Hi,
This is dopamine.
May be you must strive for a better life.
[root@localhost echo_server]# cat hello.txt | tr [a-z] [A-Z]
HI,
THIS IS DOPAMINE.
MAY BE YOU MUST STRIVE FOR A BETTER LIFE.

6) wc命令

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

参数

含义

-l

只显示行数

-w

只显示单词数

-c

只显示字节数

[root@localhost echo_server]# wc -l hello.txt 
3 hello.txt
[root@localhost echo_server]# wc -c hello.txt 
64 hello.txt
[root@localhost echo_server]# wc -w hello.txt 
13 hello.txt

7) stat命令

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

[root@localhost Desktop]# stat hello.txt 
  File: hello.txt
  Size: 64        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 18016036    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2021-04-22 20:05:41.505688735 +0800
Modify: 2021-04-22 20:05:39.710688589 +0800
Change: 2021-04-22 20:05:39.710688589 +0800
 Birth: -

8) cut命令

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

例,提取文本中以’|’分割的第二列

[root@localhost Desktop]# cat data.txt 
1111|33333|44444|6666
4adf|33333|fafdfd|adfdfa
33333|22222|dadffd|adfdfasf
[root@localhost Desktop]# cut -d'|' -f2 data.txt 
33333
33333
22222

9) diff命令

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

五. 文件目录管理命令

1) touch命令

touch命令用于创建空白文件或设置文件的时间,格式为”touch [选项] [文件]”

参数

作用

-a

仅修改”读取时间”(atime)

-m

仅修改”修改时间”(mtime)

-d

同时修改atime与mtime

[root@localhost Desktop]# ls -l hello.txt 
-rw-r--r--. 1 root root 64 Apr 22 20:05 hello.txt
[root@localhost Desktop]# touch -d "2018-06-08 15:55" hello.txt 
[root@localhost Desktop]# ls -l hello.txt 
-rw-r--r--. 1 root root 64 Jun  8  2018 hello.txt
[root@localhost Desktop]# touch -d "2021-4-22 20:05" hello.txt 
[root@localhost Desktop]# ls -l hello.txt 
-rw-r--r--. 1 root root 64 Apr 22 20:05 hello.txt

2) mkdir命令

mkdir命令用于创建空白的目录,格式为”mkdir [选项] 目录”

3) cp命令

cp命令用于复制文件或目录,格式为”cp [选项] 源文件 目标文件”

  • 如果目标文件是目录,则会把源文件复制到该目录中
  • 如果目标文件也是普通文件,则会询问是否覆盖
  • 如果目标文件不存在,则执行正常的复制操作

参数

作用

-p

保留原始文件的属性

-d

若对象为”链接文件”,则保留该”链接文件”的属性

-r

递归持续复制(用于目录)

-i

若目标文件存在则询问是否覆盖

-a

相当于-pdr

4) mv命令

mv命令用于剪切或将文件重命名,格式为”mv [选项] 源文件 [目标路径|目标文件]”

5) rm命令

rm命令用于删除文件或目录,格式为”rm [选项] 文件”

参数

作用

-f

强制参数,不用反复确认

-r

用来删除目录

6) dd命令

dd命令用于按照指定大小和个数的数据块来复制文件或转换文件,格式为”dd [参数]”

参数

作用

if

输入的文件名称

of

输出的文件名称

bs

设置每个“块”的大小

count

设置要复制“块”的个数

例如,dd可以从/dev/zero设备中取出一个大小为560M的数据块,然后保存为560_file的文件。

[root@localhost Desktop]# dd if=/dev/zero of=560_file count=1 bs=560M
1+0 records in
1+0 records out
587202560 bytes (587 MB, 560 MiB) copied, 3.1307 s, 188 MB/s
[root@localhost Desktop]# ls -l 560_file 
-rw-r--r--. 1 root root 587202560 Apr 23 09:33 560_file

dd不仅局限于复制文件这么简单,它还可以把光驱设备中的光盘制作成iso格式镜像文件。在Windows中这需要借助第三方软件才能实现

[root@localhost Desktop]# dd if=/dev/cdrom of=RHEL-server-7.0-x86_64-LinuxProbe.Com.iso

7) file命令

file命令用于查看文件的类型,格式为”file 文件名”

六. 打包压缩与搜索命令

1) tar命令

tar命令用于对文件进行打包压缩或解压,格式为”tar [选项] [文件]”

参数

作用

-c

创建压缩文件

-x

解开压缩文件

-t

查看压缩包内有哪些文件

-z

用Gzip压缩或解压

-j

用bzip压缩或解压

-v

显示压缩或解压的过程

-f

目标文件名

-p

保留原始的权限与属性

-P

使用绝对路径来压缩

-C

指定解压到的目录

[root@localhost Desktop]# tar -czvf hello.tar.gz hello.txt 
hello.txt

2) grep命令

grep命令用于文本中执行关键词搜索,格式为”grep [选项] [文件]”

参数

作用

-b

将可执行文件(binary)当作文本文件(text)来搜索

-c

仅显示找到的函数

-i

忽略大小写

-n

显示行号

-v

反向选择——仅列出没有“关键词”的行

3) find命令

find命令用于按照指定条件来查找文件,格式为”find [查找路径] 寻找条件 操作”