Linux基础操作-常用命令

一、常用系统工作命令

1)echo命令

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

  1. 查看echo的帮助信息
/bin/echo --help
  1. echo的使用
语法:echo [参数] [变量或string]
选项 说明
-n 输出字符串不换行
-e \a 蜂鸣器报警
-e \b 删除前一个字符(后面要有内容)
-e \c 最后不加换行
-e \e 输出转义字符
-e \f 换行,光标停在原处
-e \n 换行
-e \r 光标移动到首行,不换行
-e \\ 输出\
-e \t 水平Tab
-e \v 垂直Tab
-E 禁用转译字符
  1. 示例

echo -n “hello\nworld!”

xxxxx@xxxxx:~$ echo -n "hello\nworld!"
hello\nworld!

echo -e “\ahello world!”

xxxxx@xxxxx:~$ echo -e "\ahello world!"
hello world!

echo -e “h\bello world!”

xxxxx@xxxxx:~$ echo -e "h\bello world!"
ello world!

echo -e “h\cello world!”

xxxxx@xxxxx:~$ echo -e "h\cello world!"
h(base) xxxxx@xxxxx:~$

echo -e “h\eello world!”

xxxxx@xxxxx:~$ echo -e "h\eello world!"
hllo world!

echo -e “h\fello world!”

xxxxx@xxxxx:~$ echo -e "h\fello world!"
h
 ello world!

echo -e “h\nello world!”

xxxxx@xxxxx:~$ echo -e "h\nello world!"
h
ello world!

echo -e “h\rello world!”

xxxxx@xxxxx:~$ echo -e "h\rello world!"
ello world!

echo -e “h\\ello world!”

xxxxx@xxxxx:~$ echo -e "h\\\ello world!"
h\ello world!

echo -e “h\tello world!”

xxxxx@xxxxx:~$ echo -e "h\tello world!"
h	ello world!

echo -e “h\vello world!”

xxxxx@xxxxx:~$ echo -e "h\vello world!"
h
 ello world!

echo -E “h\vello world!”

xxxxx@xxxxx:~$  echo -E "h\vello world!"
h\vello world!
  1. 打印变量
    查看系统环境变量:env
    echo $PWD(显示用户当前所处的工作目录)
xxxxx@xxxxx:~$ echo $PWD
/home/xxxxxx

echo $PATH(查看PATH环境变量)

xxxxx@xxxxx:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

2)ps命令

  1. ps的使用
语法:ps [参数]
选项 说明
-a 显示一个终端的所有进程,除会话引线外
-u 显示当前用户进程及内存的使用情况
-x 显示没有控制终端的进程
-l 长格式显示更加详细的信息
-x 显示没有控制终端的进程
-e 显示所有进程
-f 全格式,包括命令行
  1. 几种固定使用
ps -aux:可以查看系统中所有的进程
ps -le:可以查看系统中所有的进程,而且还能看到进程的父进程的 PID 和进程优先级
ps -l:只能看到当前 Shell 产生的进程
ps -T -p <pid>:查看指定进程的所有线程
ps -Lf <pid>:查看指定进程的所有线程的详细信息
xxxxx@xxxxx:~$ ps -aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.2  0.3 168128 13292 ?        Ss   22:26   0:03 /sbin/init a
root           2  0.0  0.0      0     0 ?        S    22:26   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   22:26   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   22:26   0:00 [rcu_par_gp]
root           5  0.0  0.0      0     0 ?        I<   22:26   0:00 [netns]
参数 说明
USER 该进程是由哪个用户产生的
PID 进程的 ID
%CPU 该进程占用 CPU 资源的百分比,占用的百分比越高,进程越耗费资源
%MEM 该进程占用物理内存的百分比,占用的百分比越高,进程越耗费资源
VSZ 该进程占用虚拟内存的大小,单位为 KB
RSS 该进程占用实际物理内存的大小,单位为 KB
TTY 该进程是在哪个终端运行的。其中,tty1 ~ tty7 代表本地控制台终端,tty1~tty6 是本地的字符界面终端,tty7 是图形终端。pts/0 ~ 255 代表虚拟终端,一般是远程连接的终端,第一个远程连接占用 pts/0,第二个远程连接占用 pts/1,依次増长
STAT -D:不可被唤醒的睡眠状态,通常用于 I/O 情况; -R:该进程正在运行; -S:该进程处于睡眠状态,可被唤醒; -T:停止状态,可能是在后台暂停或进程处于除错状态 ; -Z:僵尸进程。进程已经中止,但是部分程序还在内存当中
START 该进程的启动时间
TIME 进程占用 CPU 的运算时间,注意不是系统时间
COMMAND 产生此进程的命令名
xxxxx@xxxxx:~$ ps -el
F S   UID     PID    PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0       1       0  0  80   0 - 42032 ep_pol ?        00:00:03 systemd
1 S     0       2       0  0  80   0 -     0 kthrea ?        00:00:00 kthreadd
1 I     0       3       2  0  60 -20 -     0 rescue ?        00:00:00 rcu_gp
1 I     0       4       2  0  60 -20 -     0 rescue ?        00:00:00 rcu_par_g
1 I     0       5       2  0  60 -20 -     0 rescue ?        00:00:00 netns
1 I     0       6       2  0  80   0 -     0 worker ?        00:00:00 kworker/0
参数 说明
F 进程标志,说明进程的权限,常见的标志有两个: 1:进程可以被复制,但是不能被执行;4:进程使用超级用户权限;
S 进程状态。具体的状态和"ps -aux"命令中的 STAT 状态一致
UID 运行此进程的用户的 ID
PID 进程的 ID
PPID 父进程的 ID
C 该进程的 CPU 使用率,单位是百分比
PRI 进程的优先级,数值越小,该进程的优先级越高,越早被 CPU 执行
NI 进程的优先级,数值越小,该进程越早被执行
ADDR 该进程在内存的哪个位置
SZ 该进程占用多大内存
WCHAN 该进程是否运行。"-"代表正在运行
CMD 产生此进程的命令名

未完待续

猜你喜欢

转载自blog.csdn.net/qq_41866988/article/details/130589824