Linux 常用命令

陌路散爱 提交于 2019-12-12 06:06:01

Linux系统查看硬件信息命令总结

lscpu

lscpu命令能够查看 CPU 和处理单元的信息,该命令没有任何其他选项或者别的功能。

lspci

lspci可以用来列出所有的 PCI 总线,还有与 PCI 总线相连的设备的详细信息,比如 VGA 适配器、显卡、网络适配器、usb 端口、SATA 控制器等。

lspci

可以通过运行下面的命令来过滤出特定设备的信息,例如关于显卡信息:

lspci -v | grep "VGA" -A 12

lshw

lshw是一个通用的工具,可以列出多种硬件单元的详细或者概要的信息,比如 CPU、内存、usb 控制器、硬盘等。lshw能够从各个“/proc”文件中提取出相关的信息。

lshw -short

lsscsi

通过运行下面的命令可以列出像硬盘和光驱等 scsi/sata 设备的信息:

lsusb

lsusb命令能够列出 USB 控制器和与 USB 控制器相连的设备的详细信息。默认情况下,lsusb命令只打印出概要信息,可以通过使用-v参数打印每一个usb端口的详细信息。

inxi

inxi是一个 bash 脚本,能够从系统的多个来源和命令获取硬件信息,并打印出一个非技术人员也能看懂的友好的报告。

默认情况下,Ubuntu 上没有安装inxi。可以通过运行下面命令来安装Inxi:

sudo apt-get install inxi

安装完Inxi之后,通过运行下面命令能够得到硬件相关的信息:

inxi -Fx

df

df命令能够列出不同分区的概要信息、挂载点、已用的和可用的空间,可以在使用df命令的时候加上-H参数(易读格式)。

df -H

free

通过使用free命令可以查看系统中使用的、闲置的和 RAM 的总体数量。

free -m

dmidecode

dmidecode命令与其他命令不同。该命令是从硬件中的 DMI 表中读取信息的。

要查看处理器的信息,运行下面命令:

sudo dmidecode -t processor

要查看内存的信息,运行下面命令:

sudo dmidecode -t memory

要查看 bios 的信息,运行下面命令:

sudo dmidecode -t bios

hdparm

hdparm命令可以用来显示像硬盘这样的 sata 设备的信息。

sudo hdparm

看硬件信息命令总结

--------------------------------------------------------------------------------------------------------------------------------

Linux进程管理命令总结

ps

‘ps’是Linux 中最基础的浏览系统中的进程的命令。能列出系统中运行的进程,包括进程号、命令、CPU使用量、内存使用量等。下述选项可以得到更多有用的消息。

ps -a - 列出所有运行中/激活进程

ps -ef |grep - 列出需要进程 ps -aux - 显示进程信息,包括无终端的(x)和针对用户(u)的进程:如USER, PID, %CPU, %MEM等

pstree

linux中,每一个进程都是由其父进程创建的。此命令以可视化方式显示进程,通过显示进程的树状图来展示进程间关系。如果指定了pid了,那么树的根是该pid,不然将会是init(pid: 1)。

 top

‘top’是一个更加有用的命令,可以监视系统中不同的进程所使用的资源。它提供实时的系统状态信息。显示进程的数据包括 PID、进程属主、优先级、%CPU、%memory等。可以使用这些显示指示出资源使用量。

htop

htop与top很类似,但是htop是交互式的文本模式的进程查看器。它通过文字图形化地显示每一个进程的CPU和内存使用量、swap使用量。使用上下光标键选择进程,F7和F8改变优先级,F9杀死进程。Htop不是系统默认安装的,所以需要额外安装。

nice

通过nice命令的帮助,用户可以设置和改变进程的优先级。提高一个进程的优先级,内核会分配更多CPU时间片给这个进程。默认情况下,进程以0的优先级启动。进程优先级可以通过top命令显示的NI(nice value)列查看。

进程优先级值的范围从-20到19。值越低,优先级越高。

nice <优先值> <进程名> - 通过给定的优先值启动一个程序

上述命令例子中,可以看到‘top’命令获得了-3的优先值。

renice

renice命令类似nice命令。使用这个命令可以改变正在运行的进程优先值。注意,用户只能改变属于他们自己的进程的优先值。

renice -n -p - 改变指定进程的优先值

初始优先值为0的3806号进程优先值已经变成了4.

renice -u -g - 通过指定用户和组来改变进程优先值

kill

这 个命令用于发送信号来结束进程。如果一个进程没有响应杀死命令,这也许就需要强制杀死,使用-9参数来执行。注意,使用强制杀死的时候一定要小心,因为进 程没有时机清理现场,也许写入文件没有完成。如果我们不知道进程PID或者打算用名字杀死进程时候,killall就能派上用场。

kill <pid>kill -9 <pid>killall -9 - 杀死所有拥有同样名字的进程

如果你使用kill,你需要知道进程ID号。pkill是类似的命令,但使用模式匹配,如进程名,进程拥有者等。

pkill <进程名>

---------------------------------------------------------------------------------------------------------

Linux中一些简单常用的文本处理方式

文本检索

grep 命令可以完成简单的文本搜索任务。

先来准备一份文本材料,把 grep 的帮助页保存为文本文件:

> man grep | col -b > grephelp.txt

检索 grephelp.txt 文件中所有包含 “find” 这个单词的文本行:

> grep "find" grephelp.txt      To find all occurrences of the word `patricia' in a file:      To find all occurrences of the pattern `.Pp' at the beginning of a line:      To find all lines in a file which do not contain the words `foo' or

希望匹配到的文本使用不同的颜色显示,可以添加 –color 选项,默认的颜色是红色。

> grep --color "find" grephelp.txt

希望在匹配结果中显示文件名和行号,使用 -H 选项可以显示文件名,使用 -n 选项可以显示行号:

> grep -H -n --color "find" grephelp.txt grephelp.txt:252:     To find all occurrences of the word `patricia' in a file: grephelp.txt:256:     To find all occurrences of the pattern `.Pp' at the beginning of a line: grephelp.txt:265:     To find all lines in a file which do not contain the words `foo' or

很多时候,需要知道匹配行前后的上下文。-A 和 -B 这两个选项会是你的好朋友。-A n 表示显示匹配行以及其后的 n 行;-B n 表示显示匹配行以及之前的 n 行。现在,匹配行的前后分别额外显示两行:

> grep -A 2 -B 2 -H -n --color "find" grephelp.txt grephelp.txt-250- grephelp.txt-251-EXAMPLES grephelp.txt:252:     To find all occurrences of the word `patricia' in a file: grephelp.txt-253- grephelp.txt-254-       $ grep 'patricia' myfile -- -- grephelp.txt-254-       $ grep 'patricia' myfile grephelp.txt-255- grephelp.txt:256:     To find all occurrences of the pattern `.Pp' at the beginning of a line: grephelp.txt-257- grephelp.txt-258-       $ grep '^/.Pp' myfile -- -- grephelp.txt-263-     match any character. grephelp.txt-264- grephelp.txt:265:     To find all lines in a file which do not contain the words `foo' or grephelp.txt-266-     `bar': grephelp.txt-267-

如果需要查找所有不包含 “find” 的文本行,使用 -v 选项即可。

grep 还有两个变体,egrep 和 fgrep。相对于仅支持基本正则模式(BREs)的 grep 来说,egrep 支持扩展正则模式(EREs),因而检索能力更为强大;fgrep 是所有三个工具中速度最快的一个,因为它完全不支持正则模式。

 

文本替换

tr 命令可以完成简单的字符转换任务。例如,可以通过 tr 把 grephelp.txt 文件转换为全文大写:

> cat grephelp.txt | tr '[:lower:]' '[:upper:]'

简而言之,tr 的工作就是把第一个集合中的字符转换为第二个集合中的相应的字符。常用的字符集合有下面这些:

  • [:alnum:]:字母数字
  • [:alpha:]:字母
  • [:cntrl:] :控制字符
  • [:digit:]:数字
  • [:graph:]: 图形字符
  • [:lower:]:小写字母
  • [:print:]:可打印字符
  • [:punct:]:标点符号
  • [:space:]:空白字符
  • [:upper:]:大写字母
  • [:xdigit:]:十六进制数字

tr 命令的应用场景非常受限,如果希望进行更加灵活的模式替换,我们还有 sed(也就是 stream editor,流编辑器)。

把文件中所有的 “find” 文本替换为 “search”:

> sed "s/find/search/g" grephelp.txt

这 条命令中,s 表示执行“替换操作”,/find/search/ 表示把 “find” 替换为 “search”,g 表示对一行中所有的匹配进行替换。sed 默认把处理结果打印到标准输出,我们可以通过重定向把处理结果转储到一个新文件中,或者使用选项 -i 把结果直接写回原文件(有风险,需谨慎):

> sed -i "s/find/search/g" grephelp.txt

把文件中所有的数字 n 替换为 “–n–” 的形式:

> sed -E "s/([0-9]+)/--/1--/g" grephelp.txt

选项 -E 表示在处理过程中使用扩展的正则模式(EREs),替换命令中的 /1 表示引用正则表达式的第一个捕获分组。请注意,-E 这个选项只在 Mac OS X 系统和 FreeBSD 系统上有效,其他 Unix 系统需要使用另一个等效的选项 -r。

文本去重

> cat -n sonnet116.txt      1    Let me not to the marriage of true minds      2    Admit impediments. Love is not love      3    Which alters when it alteration finds,      4    Or bends with the remover to remove:      5    O, no! it is an ever-fix`ed mark,      6    O, no! it is an ever-fix`ed mark,      7    That looks on tempests and is never shaken;      8    It is the star to every wand'ring bark,      9    Whose worth's unknown, although his heighth be taken.     10    Love's not Time's fool, though rosy lips and cheeks     11    Love's not Time's fool, though rosy lips and cheeks     12    Love's not Time's fool, though rosy lips and cheeks     13    Within his bending sickle's compass come;     14    Love alters not with his brief hours and weeks,     15    But bears it out even to the edge of doom:     16    If this be error and upon me proved,     17    I never writ, nor no man ever loved.

这是莎士比亚的一首十四行诗,只可惜第5行和第10行有重复(而且第10行重复了3次)。怎么查看文本中重复的行呢?uniq 命令可以帮助你。

> uniq -d sonnet116.txt O, no! it is an ever-fix`ed mark, Love's not Time's fool, though rosy lips and cheeks

选项 -d 表示仅输出重复的行。如果需要去重,使用不带选项的 uniq 命令就可以了:

> uniq sonnet116.txt Let me not to the marriage of true minds Admit impediments. Love is not love Which alters when it alteration finds, Or bends with the remover to remove: O, no! it is an ever-fix`ed mark, That looks on tempests and is never shaken; It is the star to every wand'ring bark, Whose worth's unknown, although his heighth be taken. Love's not Time's fool, though rosy lips and cheeks Within his bending sickle's compass come; Love alters not with his brief hours and weeks, But bears it out even to the edge of doom: If this be error and upon me proved, I never writ, nor no man ever loved.

想要查看每一行究竟重复了多少次使用选项 -c:

> uniq -c sonnet116.txt    1 Let me not to the marriage of true minds    1 Admit impediments. Love is not love    1 Which alters when it alteration finds,    1 Or bends with the remover to remove:    2 O, no! it is an ever-fix`ed mark,    1 That looks on tempests and is never shaken;    1 It is the star to every wand'ring bark,    1 Whose worth's unknown, although his heighth be taken.    3 Love's not Time's fool, though rosy lips and cheeks    1 Within his bending sickle's compass come;    1 Love alters not with his brief hours and weeks,    1 But bears it out even to the edge of doom:    1 If this be error and upon me proved,    1 I never writ, nor no man ever loved.

文本排序

假设有这样一个报表文件,第一列是月份,第二列是当月的销售个数:

> cat report.txt March,19 June,50 February,17 May,18 August,16 April,31 May,18 July,26 January,24 August,16

这个文件的内容不仅顺序是乱的,而且还有重复。我希望按字母表顺序排序,可以下面这个命令:

> sort report.txt April,31 August,16 August,16 February,17 January,24 July,26 June,50 March,19 May,18 May,18

选项 -u (表示 unique)可以在排序结果中去除重复行:

> sort -u report.txt April,31 August,16 February,17 January,24 July,26 June,50 March,19 May,18

能不能按照月份排序呢?选项 -M (表示 month-sort)可以帮助我们:

> sort -u -M report.txt January,24 February,17 March,19 April,31 May,18 June,50 July,26 August,16

按照第二列的数字进行排序也是很简单的:

> sort -u -t',' -k2 report.txt August,16 February,17 May,18 March,19 January,24 July,26 April,31 June,50

上面的例子中,选项 -t’,' 表示以逗号为分隔符对文本进行列分割;-k2 表示对第2列进行排序。

当然了,把结果逆序排列也并非不可能:

> sort -u -r -t',' -k2 report.txt June,50 April,31 July,26 January,24 March,19 May,18 February,17 August,16

文本统计

wc 命令用来完成文本统计工作,通过使用不同的选项,它可以统计文件中的字节数(-c),字符数(-m),单词数(-w)与行数(-l)。

例如,查看 grephelp.txt 这个文件总共有多少个单词:

> wc -w grephelp.txt     1571 grephelp.txt

查看 sonnet116.txt 这个文件总共有多少不重复的行(废话,十四行诗当然是有14行):

> uniq sonnet116.tx6 | wc -l       14

-------------------------------------------------------------------------------------

Linux中grep搜索简单用法

有测试文件test.txt一枚,内容如下

aaabbbccc

AAADDDEEE

abc

sdfjasldjf

bcd

okm

ABC

一、基本搜索常用

1、现在想把abc okm筛选出来

grep "abc\|okm" test.txt

结果:

abc

okm

2、把带a的行筛选出来,不区分大小写

grep -i "a" test.txt

结果:

aaabbbccc

AAADDDEEE

abc

sdfjasldjf

ABC

3、把带a的行筛选出来,只要小写的

grep "a" test.txt

结果:

aaabbbccc

abc

sdfjasldjf

4、把出现abc筛选出来,不区分大小写

grep -i "abc" test.txt

结果:

abc

sdfjabcasldjf

ABC

5、把出现abc整个单词的筛选出来

grep -w "abc" test.txt

结果:

abc

6、把出现abc整个单词筛选出来,不区分大小写

grep -wi "abc" test.txt

结果:

abc

ABC

 

二、

明确要求搜索子目录:grep -r

或忽略子目录:grep -d skip

三、正则匹配

格式

  grep [options]

      [options]主要参数:

      -c:只输出匹配行的计数。

  -I:不区分大 小写(只适用于单字符)。

  -h:查询多文件时不显示文件名。

  -l:查询多文件时只输出包含匹配字符的文件名。

  -n:显示匹配行及 行号。

  -s:不显示不存在或无匹配文本的错误信息。

  -v:显示不包含匹配文本的所有行。

  pattern正则表达式主要参数:

  \: 忽略正则表达式中特殊字符的原有含义。

  ^:匹配正则表达式的开始行。

  $: 匹配正则表达式的结束行。

  \<:从匹配正则表达 式的行开始。

  \>:到匹配正则表达式的行结束。

  [ ]:单个字符,如[A]即A符合要求 。

  [ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。

  。:所有的单个字符。

  * :有字符,长度可以为0。

      grep -i pattern files :不区分大小写地搜索。默认情况区分大小写,

  grep -l pattern files :只列出匹配的文件名,

  grep -L pattern files :列出不匹配的文件名,

  grep -w pattern files :只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical’),

  grep -C number pattern files :匹配的上下文分别显示[number]行,

  grep pattern1 | pattern2 files :显示匹配 pattern1 或 pattern2 的行,

  grep pattern1 files | grep pattern2 :显示既匹配 pattern1 又匹配 pattern2 的行。

  grep -n pattern files 即可显示行号信息

  grep -c pattern files 即可查找总行数

简单实例

grep ‘test’ d*

显示所有以d开头的文件中包含 test的行。

grep ‘test’ aa bb cc

显示在aa,bb,cc文件中匹配test的行。

grep ‘[a-z]\{5\}’ aa

显示所有包含每个字符串至少有5个连续小写字符的字符串的行。

more test.txt | grep '[a-b]' 范围 ;如[A-Z]即A,B,C一直到Z都符合要求

结果:

aaabbbccc

abc

sdfjabcasldjf

bcd

grep '^a' test.txt 搜索以a开头的

结果:

aaabbbccc

abc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!