linux命令行

基础介绍

以ubuntu为例,可以使用PuTTY等远程登录工具进入linux系统终端;如果是虚拟机,则可以使用快捷键 Ctrl+Alt+t 打开虚拟终端,界面如下

linux@ubuntu:~$ 

用户名 linux
主机名 ubuntu
当前路径 ~

shell命令提示符 $ 或者 #

shell命令解析器

shell命令一般格式:
[命令名称] [选项] [参数]
注:
1、三要素之间以空格分开
2、命令名称要严格区分大小写
3、选项一般用 “-” 或 “–” 引导,用以改变命令执行效果
4、参数是命令执行的对象
5、选项和参数大多情况下可以互换位置
给已存在的shell命令起别名

linux@ubuntu:~/work/temp$ alias ll='ls -alF'

注:上述方式只在当前终端有效,如果要全局生效,可以修改配置文件.bashrc,然后重新启动配置文件

linux@ubuntu:~/work/temp$ source ~/.bashrc

删除别名

linux@ubuntu:~/work/temp$ unalias ll

查看系统内核版本

linux@ubuntu:~/work/temp$ uname -r
3.16.0-77-generic

帮助手册man&help&info
查看shell命令

linux@ubuntu:~/work/temp$ man 1 pwd

查看系统调用函数

linux@ubuntu:~/work/temp$ man 2 open

查看标准C库函数

linux@ubuntu:~/work/temp$ man 3 fopen

查看历史记录(最近20条)

linux@ubuntu:~/work/temp$ history 20

清除历史记录

linux@ubuntu:~/work/temp$ history -c

clearCtrl+l 清屏

linux@ubuntu:~/work/temp$ clear

特殊字符

通配符

用于批量处理文件
“?” 代替一个任意字符

linux@ubuntu:~/work/temp/temp2$ ls ?.h
4.h  5.h

“*” 代替任意长度的字符串

linux@ubuntu:~/work/temp/temp2$ ls *.h
4.h  5.h  test3.h  test4.h

“[]” 筛选指定字符

linux@ubuntu:~/work/temp/temp2$ ls [123].c
1.c  2.c  3.c
等价于
linux@ubuntu:~/work/temp/temp2$ ls [1-3].c
1.c  2.c  3.c
过滤指定字符
linux@ubuntu:~/work/temp/temp2$ ls [^12].c
3.c
linux@ubuntu:~/work/temp/temp2$ ls test[1-3].c
test1.c  test2.c

管道符

“|” 可连接多条命令,能完成特殊操作
一般格式:命令1 | 命令2 | 命令3
注:
1、命令1的输出作为命令2的输入,而命令2的输出又作为命令3的输入,依次类推
2、要求命令1要有输出结果的能力,命令2/3要有输入的能力

linux@ubuntu:~/work/temp/temp2$ ls ./
1.c  2.c  3.c  4.h  5.h  test1.c  test2.c  test3.h  test4.h
linux@ubuntu:~/work/temp/temp2$ ls ./ | wc -w
9

wc(word count):统计文件或终端输入的行数&单词数&字符数
ctrl+d 结束终端输入

linux@ubuntu:~/work/temp/temp2$ wc test1.c 
  5  30 179 test1.c
linux@ubuntu:~$ wc
Its white houses with their steeply 
pitched roofs of red tile are spread over 
a hillside where clumps of sturdy Spanish
 chestnuts mark out the slightest
 dips in the terrain.
      5      30     178

wc -l 只统计行数
wc -w 只统计单词数
wc -c 只统计字符数

重定向符

一般格式:命令 重定向符 文件名
输出重定向符 “>” 和 “>>”

文件覆盖
linux@ubuntu:~/work/temp/temp2$ ls > log.txt
文件追加
linux@ubuntu:~/work/temp/temp2$ ls >> log.txt

错误输出重定向
“2>” 和 “2>>” 只将命令错误执行的结果输出重定向到文件
“&>” 和 “&>>” 将命令错误和正确执行的结果输出重定向到文件
输入重定向符 “<”

linux@ubuntu:~/work/temp/temp2$ wc < test1.c 
  5  30 179

置换符

“``” 将置换符中的命令替换成其执行结果

linux@ubuntu:~/work/temp/temp2$ ls `pwd`
1.c  2.c  3.c  4.h  5.h

文件和目录

查看当前的绝对路径

linux@ubuntu:~$ pwd
/home/linux

绝对路径的第一个 / 代表起始的根目录,后面所有的 / 都是分隔符

列出当前路径下的目录和文件

linux@ubuntu:~$ ls
core     Documents  examples.desktop  Pictures  Templates  vimconfig
Desktop  Downloads  my_share          Public    Tools      work

显示目录和文件的详细信息

linux@ubuntu:~$ ls -l
total 18332
-rw-------  1 root  root  18718720 Jun 15  2019 core
drwxr-xr-x  2 linux linux     4096 May 23  2018 Desktop
drwxr-xr-x  2 linux linux     4096 Aug  5  2015 Documents
drwxr-xr-x  2 linux linux     4096 Aug  5  2015 Downloads
-rw-r--r--  1 linux linux     8980 Aug  5  2015 examples.desktop
drwxrwxrwx  2 linux linux     4096 Jan 24  2019 my_share

第1列表示文件类型和所属权限

d 或 -rwxr-xr- -
目录文件/普通文件用户u权限 read write execute用户组g权限 read execute其他人员o权限 read

第3/4列分别是文件所属的用户名linux和用户组名linux
在linux系统共有7种文件类型
目录文件 d
普通文件 -
链接文件 l
字符设备文件 c
块设备文件 b
套接字文件 s
管道文件 p

查看文件的inode号

linux@ubuntu:~/work/temp/temp1$ ls -i log.txt 
1741036 log.txt

chmod修改文件权限

linux@ubuntu:~/work/temp$ chmod ugo+rwx a.out
等价于
linux@ubuntu:~/work/temp$ chmod 777 a.out

查看文件权限,如上面所设置

linux@ubuntu:~/work/temp$ ll a.out 
-rwxrwxrwx 1 linux linux 8519 Aug 19 23:30 a.out*

查看文件类型

linux@ubuntu:~/work/temp$ file a.out 
a.out: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=dabe13c38e463c2d1ce1782b77b2940df679ad26, not stripped

列出当前路径下的所有目录和文件

linux@ubuntu:~$ ls -a
.              Desktop           .lesshst      .vim
..             .dmrc             .local        vimconfig
.android       Documents         .mozilla      .viminfo
.. ..local
代表当前目录代表上层目录表示local为隐藏文件

创建目录(make directory)

linux@ubuntu:~/work$ mkdir temp
linux@ubuntu:~/work$ ls
arrays          led                no_os         temp

创建多级目录

linux@ubuntu:~/work$ mkdir -p temp/temp1/temp2

切换路径(change directory)

linux@ubuntu:~$ cd work/
linux@ubuntu:~/work$

操作已存在的命令或目录/文件名,常用 Tab 键快速补齐

返回上层目录

linux@ubuntu:~/work$ cd ..
linux@ubuntu:~$ 

返回到上一次所在目录

linux@ubuntu:~$ cd -
/home/linux/work
linux@ubuntu:~/work$

新建文件

linux@ubuntu:~/work/temp$ touch test
linux@ubuntu:~/work/temp$ ls -l
total 0
-rw-rw-r-- 1 linux linux 0 Jul 26 23:38 test
linux@ubuntu:~/work/temp$ 

如果文件已存在,则会刷新文件的时间戳

删除文件(remove)

linux@ubuntu:~/work/temp$ rm test

删除目录文件

linux@ubuntu:~/work$ rm -r temp/

拷贝文件(copy)

linux@ubuntu:~/work/temp$ cp test1 temp1/
linux@ubuntu:~/work/temp$ ls -l temp1/
total 0
-rw-rw-r-- 1 linux linux 0 Jul 26 23:51 test1
linux@ubuntu:~/work/temp$

拷贝的同时可以重命名

linux@ubuntu:~/work/temp$ cp test1 temp1/test2
linux@ubuntu:~/work/temp$ ls -l temp1/
total 0
-rw-rw-r-- 1 linux linux 0 Jul 26 23:53 test2
linux@ubuntu:~/work/temp$

拷贝目录文件

linux@ubuntu:~/work/temp$ cp -r temp1/ temp2/
linux@ubuntu:~/work/temp$ ls
temp1  temp2  test1  test2

移动目录/文件(move)

linux@ubuntu:~/work/temp$ mv test3 temp1/

移动的同时可以重命名

linux@ubuntu:~/work/temp$ mv temp1/test3 test4
linux@ubuntu:~/work/temp$ ls
temp1  temp2  test1  test2  test4

移动多个目录/文件

linux@ubuntu:~/work/temp$ mv test1 test2 test4 -t temp2/
linux@ubuntu:~/work/temp$ ls temp2/
test1  test2  test4

编辑文件

默认编辑器 vim

linux@ubuntu:~/work/temp$ vim test5

如果文件test5不存在,vim会自动创建并打开空白文件
打开文件test1并且光标定位到第6行

linux@ubuntu:~/work/temp$ vim test1 +6

同时打开多个文件
水平分屏

linux@ubuntu:~/work/temp$ vim -o2 test1 test2

垂直分屏

linux@ubuntu:~/work/temp$ vim -O2 test1 test2

比较模式

linux@ubuntu:~/work/temp$ vim -d test1 test2

在比较模式下执行 d+p(diff put) / d+o(diff obtain) 合并覆盖差异行

vim命令模式 编辑模式 底行模式 3种工作状态,打开文件后vim默认处于命令模式i / o 键切换到编辑模式空格: 键切换到底行模式Esc 键返回到命令模式

命令模式

介绍命令模式下的常用操作
Ctrl+f 向下翻整页(forward)
Ctrl+b 向上翻整页(back)
Ctrl+d 向下翻半页(down)
Ctrl+u 向上翻半页(up)
Ctrl+e 向上滚一行
Ctrl+y 向下滚一行
Ctrl+z 关闭文件
j 光标下移一行
k 光标上移一行
3 + 光标下移3行
3 - 光标上移3行
h 光标左移一行
l 光标右移一行
zz 光标所在行居中
zt 光标所在行居首(top)
zb 光标所在行居末(bottom)
gg 光标跳到文件首行
G 光标跳到文件末行
Ctrl+o 光标返回至前一个位置
Ctrl+i 光标返回至后一个位置
Ctrl+g 显示当前行信息

yy 复制光标所在的行
5yy 复制自光标以下5行
dd 剪切光标所在的行(如果不粘贴相当于删除)
5yy 剪切自光标以下5行
p 将复制或剪切的内容粘贴到光标所在的下一行
u 撤销当前操作
Ctrl+r 恢复当前操作

代码自动排版技巧
gg = G
gg v G =

底行模式

介绍底行模式下的常用操作
在标识符 : 后输入命令,回车键生效
w 保存文件
w! 强制保存
w! sudo tee% + 3次回车 超强制保存
q 退出文件
q! 强制退出
wq 保存并退出
f 显示当前文件名和路径
356 光标跳到文件第356行
3,7y 复制文件的第3至第7行
3,7d 剪切文件的第3至第7行
sp ./test1 水平分屏打开当前目录下 test1文件
命令模式Ctrl+w k / w j 水平分屏时上下切换光标
res +3 当前窗口高度加3
res -3 当前窗口高度减3
vsp ./test1 垂直分屏打开当前目录下 test1文件
命令模式Ctrl+w h / w l 垂直分屏时左右切换光标
vertical res +3 当前窗口宽度加3
vertical res -3 当前窗口宽度减3
wqa 保存并退出所有文件

3,7s#^#//#g 注释第3至第7行(注释符 //
3,7s#^//##g 删除第3至第7行的注释符 //
3,7s/^/#/g 注释第3至第7行(注释符 #
3,7s/^#//g 删除第3至第7行的注释符 #

命令模式下输入 /? 进入底行模式搜索关键字
/alloc 高亮显示文本中所有alloc关键字
?alloc 高亮显示文本中所有alloc关键字
命令模式下输入n / N ,光标上下跳转至关键字
noh 取消高亮显示(no high)
set ignorecase 搜索关键字时忽略大小写
set noignorecase 搜索关键字时区分大小写
set ff 查看文本格式:fileformat=unix 为类unix系统,fileformat=dos 为windows系统
set ff=unix 转换成类unix系统文本格式
set ff=dos 转换成windows系统文本格式
set nu 显示行号(number)
set nu! 隐藏行号
Man 3 fopen 在man3手册中查询 fopen 库函数
Tlist 显示文件中的函数(需要taglist插件支持)

文件内容输出

文件内容输出到终端

linux@ubuntu:~/work/temp/temp2$ cat test1.c
Its white houses with their steeply 
pitched roofs of red tile are spread over 
a hillside where clumps of sturdy Spanish
 chestnuts mark out the slightest
  dips in the terrain.

输出文件开头几行内容

linux@ubuntu:~/work/temp/temp2$ head -2 test1.c
Its white houses with their steeply 
pitched roofs of red tile are spread over

输出文件末尾几行内容

linux@ubuntu:~/work/temp/temp2$ tail -2 test1.c
 chestnuts mark out the slightest
  dips in the terrain.

归纳排序输出文件内容

linux@ubuntu:~/work/temp/temp2$ sort test1.c 
a hillside where clumps of sturdy Spanish
 chestnuts mark out the slightest
  dips in the terrain.
Its white houses with their steeply 
pitched roofs of red tile are spread over

文件比较

逐行比较文件差异

linux@ubuntu:~/work/temp/temp2$ diff test1.c test2.c 
1,2c1,2
< Its white houses with their steeply 
< pitched roofs of red tile are spread over 
---
> Its red houses with their steeply 
> pitched roofs of white tile are spread over

合并输出文件差异

linux@ubuntu:~/work/temp/temp2$ diff -u test1.c test2.c 
--- test1.c	2021-09-22 01:37:35.240775314 +0800
+++ test2.c	2021-09-22 02:52:51.004565497 +0800
@@ -1,5 +1,5 @@
-Its white houses with their steeply 
-pitched roofs of red tile are spread over 
+Its red houses with their steeply 
+pitched roofs of white tile are spread over 
 a hillside where clumps of sturdy Spanish
  chestnuts mark out the slightest
   dips in the terrain.

比较目录差异

linux@ubuntu:~/work/temp$ diff -urN temp1/ temp2/

文件补丁

1、生成补丁文件

linux@ubuntu:~/work/temp/temp2$ diff test1.c test2.c > patch.c

2、应用补丁

linux@ubuntu:~/work/temp/temp2$ patch test1.c patch.c

撤销补丁

linux@ubuntu:~/work/temp/temp2$ patch -R test1.c patch.c

目录补丁

1、比较目录差异并生成补丁文件

linux@ubuntu:~/work/temp$ diff -urN temp1/ temp2/ > patch.txt

2、切换到需要应用补丁的目录下

linux@ubuntu:~/work/temp$ cd temp1/

3、应用补丁

linux@ubuntu:~/work/temp/temp1$ patch -p1 < ../patch.txt

参数 -p 表示去除几层相对路径
撤销补丁

linux@ubuntu:~/work/temp/temp2$ patch -Rp1 < ../patch.txt

查找文件

linux@ubuntu:~/work/temp$ find temp2/ -name test2
temp2/test2

查找关键字

linux@ubuntu:~/work/temp$ grep -nr "alec2h" temp2/
temp2/test2:6:alec2h

字符串格式
显示所有包含关键字的行

linux@ubuntu:~/work/temp$ grep -nr "alec2h"

显示所有以关键字为首的行

linux@ubuntu:~/work/temp$ grep -nr "^alec2h"

显示所有以关键字为结尾的行

linux@ubuntu:~/work/temp$ grep -nr "alec2h$"

显示只有关键字的行

linux@ubuntu:~/work/temp$ grep -nr "^alec2h$"

利用find和grep组合快速检索

linux@ubuntu:~/work/temp$find ./  -name .git -prune  -o  -name out -prune  -o  -type f -name "*.c"  -print0 | xargs -0  grep --color -nr "alec2h"

文件压缩

常用的3种压缩工具
gzip 在linux中使用广泛,移植性强
bzip2 压缩率高
zip 兼容windows
gzip 压缩

linux@ubuntu:~/work/temp/temp1$ gzip log.txt
可指定压缩等级1~9
linux@ubuntu:~/work/temp/temp1$ gzip -9 log.txt
linux@ubuntu:~/work/temp/temp1$ ls
log.txt.gz

gzip 解压

linux@ubuntu:~/work/temp/temp1$ gzip -d log.txt.gz
或者
linux@ubuntu:~/work/temp/temp1$ gunzip log.txt.gz
linux@ubuntu:~/work/temp/temp1$ ls
log.txt

bzip2 压缩

linux@ubuntu:~/work/temp/temp1$ bzip2 log.txt 
linux@ubuntu:~/work/temp/temp1$ ls
log.txt.bz2

bzip2 解压

linux@ubuntu:~/work/temp/temp1$ bunzip2 log.txt.bz2 
linux@ubuntu:~/work/temp/temp1$ ls
log.txt

zip 压缩

linux@ubuntu:~/work/temp/temp1$ zip log.zip log.txt 
  adding: log.txt (deflated 62%)
linux@ubuntu:~/work/temp/temp1$ ls
log.txt  log.zip

zip 压缩目录

linux@ubuntu:~/work/temp$ zip -r temp1.zip temp1/

zip 解压

linux@ubuntu:~/work/temp/temp1$ unzip log.zip 
Archive:  log.zip
  inflating: log.txt                 
linux@ubuntu:~/work/temp/temp1$ ls
log.txt  log.zip

打包

gzip 和 bzip2不能直接压缩目录,需要将目录打包后压缩
打包文件/目录(仅打包不压缩)

linux@ubuntu:~/work/temp$ tar -cvf temp1.tar temp1/

打包同时使用gzip 压缩

linux@ubuntu:~/work/temp$ tar -czvf temp1.tar.gz temp1/

打包同时使用bzip2 压缩

linux@ubuntu:~/work/temp$ tar -cjvf temp1.tar.bz2 temp1/

解包解压

linux@ubuntu:~/work/temp$ tar -xvf temp1.tar
linux@ubuntu:~/work/temp$ tar -xvf temp1.tar.gz
linux@ubuntu:~/work/temp$ tar -xvf temp1.tar.bz2

链接文件

分为硬链接(相当于文件别名)和软链接(相当于快捷方式)
创建硬链接文件

linux@ubuntu:~/work/temp/temp1$ ln log.txt logy.txt

硬链接特性
1、如果修改硬链接文件或源文件,则另一个文件会同步修改
2、如果删除源文件,不会影响到硬链接文件
3、硬链接文件不占用存储空间,和源文件共享inode号
4、不能对目录创建硬链接
5、不能跨文件系统创建硬链接
创建软链接文件

必须添加源文件的绝对路径
linux@ubuntu:~/work/temp/temp1$ ln -s ~/work/temp/temp1/log.txt logs.txt

软链接特性
1、如果修改软链接文件或源文件,则另一个文件会同步修改
2、如果删除源文件,软链接会失效,如果重新创建同名源文件,软链接会重新生效
3、软链接文件占用存储空间,有独立inode号
4、可以对目录创建软链接
5、在linux环境下可以跨文件系统创建软链接

存储分区

查看磁盘信息

linux@ubuntu:~/work/temp$ df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  2.0G  4.0K  2.0G   1% /dev
tmpfs          tmpfs     395M  3.8M  392M   1% /run
/dev/sda1      ext4       58G   15G   41G  28% /

查看文件存储大小

linux@ubuntu:~/work/temp$ du -sh temp1/
36K	temp1/

查看磁盘分区

linux@ubuntu:~/work/temp/temp1$ sudo fdisk -l
[sudo] password for linux: 

Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders, total 125829120 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00049b94

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   123731967    61864960   83  Linux
/dev/sda2       123734014   125827071     1046529    5  Extended
/dev/sda5       123734016   125827071     1046528   82  Linux swap / Solaris

sd 代表磁盘的类型为SATA或SCSI(hd 代表IDE)
a 代表第一块硬盘
1 为分区编号(主分区1~4,逻辑分区从5开始)

linux和windows的文件系统逻辑结构的差别
在windows中目录属于分区,而在linux中分区属于目录(例如 /dev/sda1)

linux下一切皆文件,其主要目录有
/bin 可执行文件
/dev 设备驱动文件
/etc 配置文件
/lib 库文件
/boot 系统启动文件
/mnt 文件系统挂载点

用户管理

用户配置文件

linux@ubuntu:~/work/temp$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
linux:x:1000:1000:ubuntu:/home/linux:/bin/bash

用户名:密码:用户编码UID:用户组编码GID:主机名:家目录:shell目录
用户组配置文件

linux@ubuntu:~/work/temp$ cat /etc/group
root:x:0:
linux:x:1000:

用户组名:密码:用户组编码GID:

创建用户

linux@ubuntu:~/work/temp$ sudo adduser alec2h
或者切换到root用户下去创建
linux@ubuntu:~/work/temp$ su
Password: 
root@ubuntu:/home/linux/work/temp# adduser alec2h

adduser配置文件

linux@ubuntu:~/work/temp$ cat /etc/adduser.conf

修改用户密码

linux@ubuntu:~/work/temp$ passwd

修改其它用户的密码

linux@ubuntu:~/work/temp$ sudo passwd alec2h

删除用户

linux@ubuntu:~/work/temp$ sudo deluser alec2h

删除用户同时不保留所属家目录

linux@ubuntu:~/work/temp$ sudo deluser --remove-home alec2h

用户组管理

创建用户组

linux@ubuntu:~/work/temp$ sudo addgroup alec2h

删除用户组

linux@ubuntu:~/work/temp$ sudo delgroup alec2h

注:如果用户组内还有成员,则无法删除
修改用户属性

用户alec2h加入用户组linux
linux@ubuntu:~/work/temp$ sudo usermod -g linux alec2h
用户移至新的主目录
linux@ubuntu:~/work/temp$ sudo usermod -d /home/alec2h_new -m -l alec2h_new alec2h

软件包管理

ubuntu支持的deb软件包分为二进制包(.deb)和源码包(.deb-src)

离线安装和卸载

1、需要事先将软件包保存在本地
2、不会检查软件包的依赖关系
安装本地的deb软件包
sudo dpkg -i 软件包
移除已安装的软件(保留配置文件)
sudo dpkg -r 软件名
移除已安装的软件(删除包括配置文件)
sudo dpkg -P 软件名
列出安装软件包的清单
sudo dpkg -L 软件名

在线安装和卸载

1、需要保证网络通畅
2、会自动检查软件依赖关系,如果缺少依赖文件,则自动下载并安装
软件源配置文件 /etc/apt/sources.list
扫描软件源,更新软件列表 /var/lib/apt/lists
sudo apt-get update
检查依赖关系
sudo apt-get check
修复依赖关系
sudo apt-get -f install
下载源码包
sudo apt-get source 软件名
查看软件依赖关系
sudo apt-cache depends 软件名
软件包缓冲区 /var/cache/apt/archives
删除缓冲区的软件包
sudo apt-get clean
删除但保留最新版本的软件包
sudo apt-get autoclean
安装软件
sudo apt-get install 软件名
重装软件
sudo apt-get install 软件名 --reinstall
更新软件
sudo apt-get upgrade
卸载软件(不完全卸载)
sudo apt-get remove 软件名
完全卸载(删除配置文件)
sudo apt-get --purge remove 软件名

进程管理

查看进程信息

linux@ubuntu:~$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 06:14 ?        00:00:03 /sbin/init
root         2     0  0 06:14 ?        00:00:00 [kthreadd]
linux     4913  4904  0 23:41 ?        00:00:00 gnome-pty-helper
linux     4914  4904  0 23:41 pts/7    00:00:00 bash
root      5000     2  0 23:46 ?        00:00:00 [kworker/u64:0]
linux     5006  4914  0 23:50 pts/7    00:00:00 ps -ef

用户名 UID
进程号 PID
父进程号 PPID
查看进程状态

linux@ubuntu:~$ ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  34032  4592 ?        Ss   06:14   0:03 /sbin/init
root         2  0.0  0.0      0     0 ?        S    06:14   0:00 [kthreadd]
linux     4913  0.0  0.0  14836  1908 ?        S    23:41   0:00 gnome-pty-helper
linux     4914  0.0  0.1  27284  5944 pts/7    Ss   23:41   0:00 bash
root      5008  0.0  0.0      0     0 ?        S    23:51   0:00 [kworker/u64:0]
linux     5009  0.0  0.0  22656  2608 pts/7    R+   23:52   0:00 ps -aux

%CPU:CPU占用率
%MEM:内存占用率
VSZ:虚拟内存占用大小
RSS:占用固定内存大小
进程状态 STAT
S:阻塞态(可中断的)
R:运行态/就绪态
Z:僵尸态
T:停止态(ctrl+z暂停)
D:睡眠态(不可中断)
s:进程领导者(存在子进程)
N:低优先级
<:高优先级
L:将页面锁定在内存中(用于实时和自定义IO)
l:多线程
+:前台运行
动态监视进程

linux@ubuntu:~$ top

q退出动态监视
查看进程的树形结构

linux@ubuntu:~$ pstree

Ctrl+c结束前台进程(本质是发送SIGINT信号给进程)
Ctrl+z前台进程转入后台并暂停
后台运行程序

linux@ubuntu:~/work/temp$ ./a.out &
[1] 3455

查看后台进程及对应的执行号

linux@ubuntu:~/work/temp$ jobs

前台转入后台运行或者继续执行暂停的后台进程,参数为执行号

linux@ubuntu:~/work/temp$ bg 1

后台转入前台运行,参数为执行号

linux@ubuntu:~/work/temp$ fg 1

进程信号

查看可用信号

linux@ubuntu:~$ kill -l
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1

给进程发信号

给PID为4914的进程及其子进程发送信号SIGINT(结束进程树)
linux@ubuntu:~$ kill -2 4914
给PID为4914的进程发送信号SIGKILL(立刻杀掉进程)
linux@ubuntu:~$ kill -9 4914
缺省发送信号15(结束进程)
linux@ubuntu:~$ kill 4914

网络管理

查看网卡信息

linux@ubuntu:~$ ifconfig

检查网络链路

linux@ubuntu:~$ ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=128 time=25.7 ms
64 bytes from 14.215.177.38: icmp_seq=2 ttl=128 time=26.2 ms

重启网络服务

linux@ubuntu:~$ sudo /etc/init.d/networking restart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值