Linux是一种开源的操作系统,掌握Linux基础知识和基本命令是每个互联网技术部门以及IT爱好者必备技能,本文将展开聊聊Linux相关的基础知识与命令。(本文将以CentOS 7.9版本示例)
基本命令详解
显示系统时间
[root@bj-test-centos ~]# date
2024年 08月 01日 星期四 15:42:04 CST
显示为时间戳
[root@bj-test-centos ~]# date +%s
1722498471
显示时区信息
[root@bj-test-centos ~]# date -R
Thu, 01 Aug 2024 15:49:34 +0800
显示硬件时钟
[root@bj-test-centos ~]# hwclock -r
2024年08月01日 星期四 15时58分23秒 -0.140614 秒
[root@bj-test-centos ~]#
显示日历
[root@bj-test-centos ~]# cal 8 2024
八月 2024
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
关机和重启
一分钟后关机,可用shutdown -c取消关机。
[root@bj-test-centos ~]# shutdown
Shutdown scheduled for 四 2024-08-01 16:18:37 CST, use 'shutdown -c' to cancel.
[root@bj-test-centos ~]#
Broadcast message from root@bj-test-centos (Thu 2024-08-01 16:17:37 CST):The system is going down for power-off at Thu 2024-08-01 16:18:37 CST!
命令执行后会广播已登录用户,作为提醒。
一分钟后重启,同样可用shutdown -c取消重启
[root@bj-test-centos ~]# shutdown -r
Shutdown scheduled for 四 2024-08-01 16:31:21 CST, use 'shutdown -c' to cancel.
[root@bj-test-centos ~]#
Broadcast message from root@bj-test-centos (Thu 2024-08-01 16:30:21 CST):The system is going down for reboot at Thu 2024-08-01 16:31:21 CST!
显示当前用户信息
[root@bj-test-centos ~]# who am i
root pts/0 2024-08-01 16:32 (来源ip)
显示当前已登录的用户信息
[root@bj-test-centos ~]# who am i
root pts/0 2024-08-01 16:32 (来源ip)
显示当前所有登录用户信息
[root@bj-test-centos ~]# w
16:35:08 up 3 min, 1 user, load average: 0.01, 0.03, 0.02
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 ip 16:32 4.00s 0.01s 0.00s w
会话管理
会话是指用户与系系统或应用程序的交互,比如说打开一个终端窗口,在里面输入命令,终端给出执行结果,这样就称为会话。
在工作中,由于会话管理工具属于后台运行的窗口,执行命令过程中,为了防止网络波动,影响命令的中断,所以要选择一个合适的会话管理工具实现稳定的工作环境。
同时会话管理工具也可以在一个窗口中访问多个会话,对于同时运行多个命令程序有很大帮助。
以下是两个常用的会话管理工具,可自行选择使用。
安装screen
[root@bj-test-centos ~]# sudo yum install screen
启动screen
[root@bj-test-centos ~]# screen
查看当前会话
[root@bj-test-centos ~]# screen -ls
There is a screen on:
2804.dayunzi (Attached)
1 Socket in /var/run/screen/S-root.
退出screen,输入exit即可。
安装tmux
[root@bj-test-centos ~]# sudo yum install tmux
启动tmux
[root@bj-test-centos ~]# tmux
查看当前所有会话
[root@bj-test-centos ~]# tmux ls
0: 1 windows (created Thu Aug 1 17:34:09 2024) [138x38]
3: 1 windows (created Thu Aug 1 19:50:12 2024) [138x38]
8: 1 windows (created Thu Aug 1 20:10:30 2024) [138x38] (attached)
退出tmux,输入exit即可 。
启动会话管理工具后,操作页面和普通命令行页面相同,只是开了一个稳定的窗口,当窗口中的命令没有执行完前,一定不能exit关闭,一旦关闭进程就中断了。
输出信息echo
打印输出默认换行
[root@bj-test-centos ~]# echo "hello world"
hello world
转义输出
[root@bj-test-centos ~]# echo -e 'a\x0Ab'
a
b
"\x0A"是换行符的十六进制表示形式。
输出变量
[root@bj-test-centos ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
echo还有一些高级用法,可以将输出的结果附带颜色,可自行前往查看用法。