pidof
ps
top
ip
uptime
wget
curl
tr
dd
tar
grep
find
Command Detailed
1.pidof
PID get a running program
Example 1:
[root@ken ~]# pidof sshd 24342 23786 3026
2.ps
View the status of system processes
Common combinations of options:
aux: is a format to display the BSD
The items displayed there: USER, PID,% CPU,% MEM, VSZ, RSS, TTY, STAT, START, TIME, COMMAND
-ef: is displayed in a standard format
The items displayed there: UID, PID, PPID, C, STIME, TTY, TIME, CMD
Example 1:
[root@ken ~]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.4 125576 4072 ? Ss Jan11 0:55 /usr/lib/systemd/systemd --switched-root --system --dese root 2 0.0 0.0 0 0 ? S Jan11 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S Jan11 0:05 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< Jan11 0:00 [kworker/0:0H] root 6 0.0 0.0 0 0 ? S Jan11 0:05 [kworker/u2:0] root 7 0.0 0.0 0 0 ? S Jan11 0:00 [migration/0] root 8 0.0 0.0 0 0 ? S Jan11 0:00 [rcu_bh] root 9 0.0 0.0 0 0 ? R Jan11 0:17 [rcu_sched] root 10 0.0 0.0 0 0 ? S< Jan11 0:00 [lru-add-drain] root 11 0.0 0.0 0 0 ? S Jan11 0:02 [watchdog/0] root 13 0.0 0.0 0 0 ? S Jan11 0:00 [kdevtmpfs]
Example 2;
[root@ken ~]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Jan11 ? 00:00:55 /usr/lib/systemd/systemd --switched-root --system --deserialize 22 root 2 0 0 Jan11 ? 00:00:00 [kthreadd] root 3 2 0 Jan11 ? 00:00:05 [ksoftirqd/0] root 5 2 0 Jan11 ? 00:00:00 [kworker/0:0H] root 6 2 0 Jan11 ? 00:00:05 [kworker/u2:0] root 7 2 0 Jan11 ? 00:00:00 [migration/0] root 8 2 0 Jan11 ? 00:00:00 [rcu_bh] root 9 2 0 Jan11 ? 00:00:17 [rcu_sched] root 10 2 0 Jan11 ? 00:00:00 [lru-add-drain] root 11 2 0 Jan11 ? 00:00:02 [watchdog/0] root 13 2 0 Jan11 ? 00:00:00 [kdevtmpfs] root 14 2 0 Jan11 ? 00:00:00 [netns]
3.top
Display linux in the process
Example 1:
[root@ken ~]# top top - 17:03:06 up 2 days, 9:07, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 108 total, 2 running, 106 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1015828 total, 95592 free, 326988 used, 593248 buff/cache KiB Swap: 2097148 total, 2097148 free, 0 used. 481564 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2796 root 20 0 298716 6124 4792 S 0.3 0.6 5:47.31 vmtoolsd 8151 root 20 0 161840 2180 1548 R 0.3 0.2 0:00.05 top 1 root 20 0 125576 4072 2636 S 0.0 0.4 0:56.00 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:05.85 ksoftirqd/0 5 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:05.05 kworker/u2: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 R 0.0 0.0 0:17.41 rcu_sched
Line 1: system time, running time, number of terminals logged, the system load (three values were 1 minute, 5 minutes, and the average value, the smaller value means within 15 minutes of lower load).
Line 2: Total number of processes, the number of running processes, number of processes during sleep, stopping the process, the number of dead in the process.
Line 3: the percentage of resources occupied by the user, the system kernel resources occupancy percentage, percentage change over resources, such as the percentage of idle resources priority process. Where the data are displayed as a percentage of CPU and data format, such as "97.1 id" means that 97.1% of the CPU processor resources idle.
Line 4: total physical memory, memory usage, the amount of free memory, the amount of kernel memory as cache.
Line 5: The total amount of virtual memory, virtual memory usage, virtual memory free amount, the amount of memory that has been loaded in advance.
4.ip
View and set up a network of related content
Example 1: ip View
Support is omitted, the following content can be abbreviated as ip a
[root@ken ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000 link/ether 00:0c:29:99:ea:a6 brd ff:ff:ff:ff:ff:ff inet 172.20.10.6/24 brd 172.20.10.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet6 2408:84f4:48:32ed:20c:29ff:fe99:eaa6/64 scope global mngtmpaddr dynamic valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe99:eaa6/64 scope link valid_lft forever preferred_lft forever
5.uptime
Tell the system running long time
Example 1:
[root@ken ~]# uptime 17:07:49 up 2 days, 7:33, 3 users, load average: 0.00, 0.03, 0.05
6.wget
-P: specify the download path uppercase P
[root@ken-master ~]# wget -P /tmp https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm--2019-05-06 10:58:45-- https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm Resolving repo.zabbix.com (repo.zabbix.com)... 162.243.159.138, 2604:a880:1:20::b82:1001 Connecting to repo.zabbix.com (repo.zabbix.com)|162.243.159.138|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 13712 (13K) [application/x-redhat-package-manager] Saving to: ‘/tmp/zabbix-release-4.0-1.el7.noarch.rpm’ 100%[============================================================================>] 13,712 --.-K/s in 0s 2019-05-06 10:58:46 (185 MB/s) - ‘/tmp/zabbix-release-4.0-1.el7.noarch.rpm’ saved [13712/13712] [root@ken-master ~]# ls /tmp tmpp6ayT2 vmware-root zabbix-release-4.0-1.el7.noarch.rpm
Network for downloading a file in the terminal
[root@ken ~]# wget www.baidu.com --2019-01-13 17:09:49-- http://www.baidu.com/ Resolving www.baidu.com (www.baidu.com)... 163.177.151.110, 163.177.151.109 Connecting to www.baidu.com (www.baidu.com)|163.177.151.110|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2381 (2.3K) [text/html] Saving to: ‘index.html.1’ 100%[===============================================================================>] 2,381 --.-K/s in 0s 2019-01-13 17:09:49 (75.0 MB/s) - ‘index.html.1’ saved [2381/2381]
7.curl
Transmitting or downloading data from the server
[root@ken ~]# curl www.baidu.com <!DOCTYPE html> <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class="fm"> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidde
8.tr
Conversion or delete characters
Example 1:
[root@ken ~]# echo "this is ken" | tr [a-z] [A-Z] THIS IS KEN
Example 2:
[root@ken ~]# cat /etc/passwd | tr [a-z] [A-Z] ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH 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 SYNC:X:5:0:SYNC:/SBIN:/BIN/SYNC SHUTDOWN:X:6:0:SHUTDOWN:/SBIN:/SBIN/SHUTDOWN HALT:X:7:0:HALT:/SBIN:/SBIN/HALT 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
9.dd
Convert and copy files
if = file
read from the file instead of the standard input.
of = file
written to the file instead of standard output go
bs = bytes
one byte read and write bytes
count = blocks
copy only blocks before the block of the input file
Example 1:
[root@ken ~]# dd if=/dev/zero of=test.sh count=100 bs=1M 100+0 records in 100+0 records out 104857600 bytes (105 MB) copied, 0.805793 s, 130 MB/s [root@ken ~]# ls -lh test.sh -rw-r--r-- 1 root root 100M Jan 13 17:23 test.sh
10.tar
Packaging for the file compression or decompression
Common options:
-c | Create a compressed file |
-x | Extract the file |
-t | What view files in the archive |
-with | With Gzip compression or decompression |
-j | Compression or decompression with bzip2 |
-v | Display compression or decompression process |
-f | The destination file name |
-p | Preserve the original permissions and attributes |
-P | Use an absolute path to compress |
-C | Specify the directory to unzip |
Common combination:
czvf: create a compressed file
xzvf: Unzip
Example 1: Creating a compressed file
[ken to set a root @ ~] # tar czcf keysshtargz keyssh [to set a root @ ken ~] # The -l keysshtargz the ls rw-r is - to set a root to set a root r 1 to Jan 618 13 17:26 keys.sh.tar.gz
Example 2: Extract
[ken to set a root @ ~] # tar xzvf keysshtargz keyssh
11.grep
The results for a search keyword in the text, and display matching
Options | description |
-E,–extended-regexp | Mode is an extension of the regular expression (ERE) |
-i,–ignore-case | Ignore case |
-n,–line-number | Print line numbers |
-o,–only-matching | Print only the content match |
-c,–count | Print only the number of lines of each file that matches |
-B,–before-context=NUM | The first few lines of print match |
-A,–after-context=NUM | After a few lines of print match |
-C,–context=NUM | Print a few lines before and after the match |
–color[=WHEN], | Font color matching |
-v,–invert-match | Print does not match the line |
Example 1: Ignore case
[root@ken ~]# echo "this is ken THIS IS KEN" | grep -i 'ken' this is ken THIS IS KEN [root@ken ~]# echo "this is ken THIS IS KEN" | grep 'ken' this is ken THIS IS KEN
Example 2: print only the content match
[root@ken ~]# echo "this is ken THIS IS KEN" | grep -o 'ken' ken [root@ken ~]# echo "this is ken THIS IS KEN" | grep 'ken' this is ken THIS IS KEN
3 Example: the number of lines to print files that match
[root@ken ~]# grep -c 'root' /etc/passwd 2 [root@ken ~]# grep 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin
实例4:打印不匹配的行
[root@ken ~]# echo -e "hi\nthis is ken\nncie to meet you\nbye " | grep -v 'ken' hi ncie to meet you bye
12.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 | 匹配无所有组的文件 |
-type b/d/c/p/l/f | 匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件) |
-size | 匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件) |
-exec …… {} \; | 后面可跟用于进一步处理搜索结果的命令(下文会有演示) |
实例1:当前位置下找出后缀是txt的文件和目录
[root@ken ~]# find ./ -name "*.txt"
实例2:找文件大小是1M的文件
[root@ken ~]# find / -size 1M
实例3:找一般文件
[root@ken ~]# find / -type f
实例4:从/tmp下找出后缀是wps 或doc 或exel的文件
如果出现如下的报错信息,需要在文件名前加上单引号例如:‘ken*’
find: paths must precede expression Usage: find [-H] [-L] [-P] [path...] [expression]
[root@ken ~]# ind /tmp/ -name *.doc -o -name *.exel -o -name *.wps
实例5:将tmp下有执行权限的文件中的执行权限去除
[root@ken ~]# find /tmp -perm /111 -exec chmod a-x {} \;
命令行的通配符
*
?
[0-9]
[a-z]
[A-Z]
作为Linux运维人员,我们有时候也会遇到明明一个文件的名称就在嘴边但就是想不起来的情况。如果就记得一个文件的开头几个字母,想遍历查找出所有以这个关键词开头的文
件,该怎么操作呢?
顾名思义,通配符就是通用的匹配信息的符号,比如星号(*)代表匹配零个或多个字符,问号(?)代表匹配单个字符,中括号内加上数字[0-9]代表匹配0~9之间的单个数字的字
符,而中括号内加上字母[abc]则是代表匹配a、b、c三个字符中的任意一个字符。下面我们就来匹配所有在/dev目录中且以sda开头的文件:
[root@ken ~]# ls /dev/sda* /dev/sda /dev/sda1 /dev/sda2
如果只想查看文件名为sd开头,但是后面还紧跟其他某一个字符的文件的相关信息,该怎么操作呢?这时就需要用到问号来进行通配了。
[root@ken ~]# ls /dev/sd? /dev/sda
除了使用[0-9]来匹配0~9之间的单个数字,也可以用[135]这样的方式仅匹配这三个指定数字中的一个,若没有匹配到,则不会显示出来:
[root@ken ~]# ls /dev/sda[0-9] /dev/sda1 /dev/sda2 [root@ken ~]# ls /dev/sda[a-z] ls: cannot access /dev/sda[a-z]: No such file or directory
一切从“/”开始
在Linux系统中,目录、字符设备、块设备、套接字、打印机等都被抽象成了文件,即刘遄老师所一直强调的“Linux系统中一切都是文件”。既然平时我们打交道的都是文件,那么又
应该如何找到它们呢?在Windows操作系统中,想要找到一个文件,我们要依次进入该文件所在的磁盘分区(假设这里是D盘),然后在进入该分区下的具体目录,最终找到这个文
件。但是在Linux系统中并不存在C/D/E/F等盘符,Linux系统中的一切文件都是从“根(/)”目录开始的,并按照文件系统层次化标准(FHS)采用树形结构来存放文件,以及定义了常
见目录的用途。另外,Linux系统中的文件和目录名称是严格区分大小写的。例如,root、rOOt、Root、rooT均代表不同的目录,并且文件名称中不得包含斜杠(/)。Linux系统中的
文件存储结构如图6-1所示。
图6-1 Linux系统中的文件存储结构
前文提到的FHS是根据以往无数Linux系统用户和开发者的经验而总结出来的,是用户在Linux系统中存储文件时需要遵守的规则,用于指导我们应该把文件保存到什么位置,以及告
诉用户应该在何处找到所需的文件。但是,FHS对于用户来讲只能算是一种道德上的约束,有些用户就是懒得遵守,依然会把文件到处乱放,有些甚至从来没有听说过它。这里并不
是号召各位读者去谴责他们,而是建议大家要灵活运用所学的知识,千万不要认准这个FHS协定只讲死道理,不然吃亏的可就是自己了。在Linux系统中,最常见的目录以及所对应
的存放内容如表6-1所示。
表6-1 Linux系统中常见的目录名称以及相应内容
目录名称 | 应放置文件的内容 |
/boot | 开机所需文件—内核、开机菜单以及所需配置文件等 |
/dev | 以文件形式存放任何设备与接口 |
/etc | 配置文件 |
/home | 用户主目录 |
/bin | 存放单用户模式下还可以操作的命令 |
/lib | 开机时用到的函数库,以及/bin与/sbin下面的命令要调用的函数 |
/sbin | 开机过程中需要的命令 |
/media | 用于挂载设备文件的目录 |
/opt | 放置第三方的软件 |
/root | 系统管理员的家目录 |
/srv | 一些网络服务的数据文件目录 |
/tmp | 任何人均可使用的“共享”临时目录 |
/proc | 虚拟文件系统,例如系统内核、进程、外部设备及网络状态等 |
/usr/local | 用户自行安装的软件 |
/usr/sbin | Linux系统开机时不会使用到的软件/命令/脚本 |
/usr/share | 帮助与说明文件,也可放置共享文件 |
/var | 主要存放经常变化的文件,如日志 |
/lost+found | 当文件系统发生错误时,将一些丢失的文件片段存放在这里 |
在Linux系统中另外还有一个重要的概念—路径。路径指的是如何定位到某个文件,分为绝对路径与相对路径。绝对路径指的是从根目录(/)开始写起的文件或目录名称,而相对路
径则指的是相对于当前路径的写法。我们来看下面这个例子,以帮助大家理解。假如有位外国游客来到中国潘家园旅游,当前内急但是找不到洗手间,特意向您问路,那么您有两种
正确的指路方法。
Absolute path (absolute path): First to fly to China, to Beijing Capital Airport take the Airport Express to Sanyuan Bridge, then transfer to Line 10 to Panjiayuan station, a station sit 34 bus to the farmers in the light, after getting off turn left.
Relative path (relative path): front turn left.
Both methods are correct. If you say is the absolute path, then any foreign tourists can find a toilet Panjiayuan According to this prompt, but too cumbersome. If you say a relative path, although very concise expression, but the
Foreign tourists can only proceed from the current position (not necessarily Panjiayuan) to find the bathroom, and therefore can not guarantee can be found in the bathroom after the previous turn left, we can see the relative path does not have universal.
If you readers now still can not understand the difference between relative and absolute paths, and do not worry, later through practical exercises you can certainly do understand completely. The current specifications suggest that you first remember the role of FHS directory, which will be put to use in the future
field.