Linux常用命令(1)——系统工作命令

echo

在终端设备上数据字符或变量提取后的值。echo [参数] 字符串/$变量

1.输出指定字符串
[root@hostname ~]# echo hello
hello

2.输出某个变量
[root@hostname ~]# echo $SHELL
/bin/bash

3.带转义字符
[root@hostname ~]# echo \$SHELL
$SHELL

4.将字符写入指定文件
[root@hostname ~]# echo "hello">a.txt
[root@hostname ~]# cat a.txt
hello

5.执行命令并输出执行结果
[root@hostname ~]# echo `uptime`
17:35:24 up 39 min, 1 user, load average: 0.03, 0.02, 0.01
#17:35:24-系统当前时间,up 39 min-从上次启动开始系统运行时间,1 user-实际连接数,load average-1、5、15分钟系统平均负载

6.输出带换行符
[root@hostname ~]# echo -e "1\n2\n3"
1
2
3

date

显示或设置系统的时间与日期。data [选项][+输出形式]

1.显示当前时间
[root@hostname ~]# date
Wed Apr 12 17:41:18 CST 2023

2.显示是一年中第几天
[root@hostname ~]# date "+%j"
102

3.按指定格式显示
[root@hostname ~]# date "+%Y-%m-%d %H:%M:%S"
2023-04-12 09:45:10

4.设置当前系统时间
[root@hostname ~]# date -s "20230412 9:43:00"
Wed Apr 12 09:43:00 CST 2023

timedatectl

设置系统时间与日期,与date相比,该命令的设置会写入到配置文件中,不会随系统重启失效。

timedatectl [参数]

1.查看当前系统的时区、日期、时间等信息
[root@hostname ~]# timedatectl
               Local time: Wed 2023-04-12 17:49:52 CST
           Universal time: Wed 2023-04-12 09:49:52 UTC
                 RTC time: Wed 2023-04-12 09:49:52
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

2.关闭NTP时钟同步
[root@hostname ~]# timedatectl set-ntp false

3.设置系统日期及时间
[root@hostname ~]# timedatectl set-time 2023-04-12
[root@hostname ~]# timedatectl set-time 9:52

4.查看可选时区并设置系统时区
[root@linuxprobe ~]# timedatectl list-timezones 
Africa/Abidjan
Africa/Accra
........省略............
[root@hostname ~]# timedatectl set-timezone "Asia/Shanghai"

reboot&poweroff

重启系统[root@hostname~]# reboot
关闭系统[root@hostname~]# poweroff

wget

下载网络文件。wget [参数] 网址

1.指定网址下载文件
[root@hostname ~]# wget 网址

2.下载并自定义本地保存名字
[root@hostname ~]# wget -O 本地名字.pdf 网址

3.断点续传下载
[root@hostname ~]# wget -c 网址

4.后台执行
[root@hostname ~]# wget -b 网址

ps&pstree

查看系统进程状态。 ps [参数]

1.显示进程详细信息
[root@hostname ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.1  0.6 179100 13816 ?        Ss   09:48   0:01 /usr/lib/syste
#USER-进程所有者,PID-进程号,%CPU-运算器占用率,%MEM-内存占用率,%VSZ-虚拟内存占用量(kb),%RSS-占用的固定内存量(kb),%TTY-所在终端,STAT-进程状态,START-被启动时间,TIME-实际CPU使用时间,COMMAND-命令名称与参数
#关于STAT-进程状态
#R-运行,S-中断,D-不可中断,Z-僵死,T-停止高优先级-<,低优先级-N,被锁进内存-L,包含子进程-s,多线程-l

2.与其他指令结合
[root@hostname ~]# ps aux > backup.txt  #当前状态备份
[root@hostname ~]# ps -ef | grep su     #查询su
[root@hostname ~]# ps -u root           #查询指定用户进行信息
[root@hostname ~]# ps aux | sort -rnk 3 #按照第3列%CPU降序排序
[root@hostname ~]# ps aux | sort -rnk 4 #按照第4列%MEM降序排序

3.树状形式显示进程之间关系
[root@hostname ~]# pstree -p #带进程号

top

实时显示系统运行状态。top [参数]

[root@hostname ~]# top
top - 10:22:19 up 34 min,  1 user,  load average: 0.00, 0.00, 0.00
#系统时间10:22:19,开机34分钟,连接数1,1、5、15分钟平均负载0
Tasks: 310 total,   3 running, 307 sleeping,   0 stopped,   0 zombie
#310个进程总数,3个正在执行,307个睡眠中,0个停止,0个僵死
%Cpu(s):  0.3 us,  0.7 sy,  0.0 ni, 98.7 id,  0.0 wa,  0.3 hi,  0.0 si,  0.0 st
MiB Mem :   1966.1 total,    206.7 free,   1213.2 used,    546.2 buff/cache
#物理内存总量1966.1,内存空闲量206。7,内存使用量1213.2,作为内核缓存的内存量546.2
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.    569.5 avail Mem
#虚拟内存总量2048,虚拟内存空闲量2048,虚拟内存使用量0,已被提前加载的内存量569.5

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND           
  2091 root      20   0 2887548 156884  88308 S   0.7   7.8   0:13.05 gnome-shell       
  2490 root      20   0  530308  41820  30360 S   0.3   2.1   0:04.06 gnome-terminal-   
  3244 root      20   0   64000   4796   3920 R   0.3   0.2   0:00.14 top               
     1 root      20   0  179100  13816   9020 S   0.0   0.7   0:01.25 systemd    
.........省略.................

nice

调整进程优先级。nice [参数] 命令或脚本名

1.以优先级5的方式执行脚本
[root@hostname ~]# nice -n -5 ./a.sh

2.最高优先级执行
[root@hostname ~]# nice -n -20 ./a.sh

#优先级(-20~19)

pidof

查询某个进程PID。

[root@hostname ~]# pidof sshd
1037

kill & killall

终止进程。

[root@hostname ~]# kill 1037    #杀死进程
[root@hostname ~]# kill -9 1037 #最高级别强制杀死进程
[root@hostname ~]# killall sshd #杀死某个sshd服务对应所有进程

参考

《Linux就该这么学》,强推!

猜你喜欢

转载自blog.csdn.net/weixin_47505548/article/details/130148704