1.Linux系统操作
本文介绍了Linux系统的基本操作命令,包括命令提示符的识别、shell语法解析、系统关机重启操作、常用命令使用技巧等。主要内容有:1)区分普通用户和管理员命令提示符;2)解释shell命令结构(命令、选项、参数);3)shutdown命令实现定时关机/重启;4)常用命令如passwd修改密码、file检测文件类型、cat查看文件、head/tail查看文件首尾内容、less翻页查看、wc统计文件
文章目录
Linux系统操作
命令提示符
作用:提示计算机正在等待用户的输入
两者权限是不一样的,超级管理员root拥有一切权限,普通用户的一些操作需要提权操作,前提是超级管理员给该普通用户授权
# 代表普通用户提示符,提示符最后是有一个空格。
[hxl@CentOS7 ~]$
# 代表管理员提示符
[root@CentOS7 ~]#

shell 解析字符串的语法
shell命令行,通过空格分隔,包涵三个部分:
- 命令,第一部分必须是命令,代表要执行的程序,其后可能跟着选项或参数。
- 选项,调整命令的行为或作用。通常以一个或两个
-符号开头,例如(-a --all)。 - 参数,典型的参数是命令的目标,命令后面可能接多个参数。
可以类比C语言中的函数的运用,声明函数,调用函数,函数参数,这样想就会能更容易理解
关闭或重启系统
关闭系统(立刻、延迟、定点)
shutdown -h now #立即关机
poweroff #立即关机
shutdown -h 2 #两分钟后关机(单位分钟)
shutdown -h 15:00 #15:00时刻关机
重启系统(同关闭系统)
shutdown -r now
shutdown -r 2
shutdown -r 15:00
取消关机或重启
shutdown -c
❗注意:定时使用的是24小时制,若定时时间今天未到,则今天到点执行,若定时时间已过,则第二天到点执行
bash执行命令
tab 按键
补全功能:补全命令、补全选项、补全参数
tab 一次,就能出结果,说明匹配结果
tab 两次,才补全结果,说明匹配结果不唯一
passwd
更新用户密码
[root@centos7 ~ 13:03:22]# useradd hadoop
[root@centos7 ~ 13:11:41]# passwd hadoop
更改用户 hadoop 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
#删除密码
[root@centos7 ~ 12:26:30]# passwd -d hxl
清除用户的密码 hxl。
passwd: 操作成功
#免交互更改密码
[root@centos7 ~ 13:59:16]# echo 123 | passwd --stdin hxl
更改用户 hxl 的密码 。
passwd:所有的身份验证令牌已经成功更新。
❗注意:二次确认可以强制设置密码
文件查看命令
file
用于检测文件类型
#检测文件类型
[root@centos7 ~ 13:14:27]# file hello.txt
hello.txt: ASCII text
#显示 MIME 类型
[root@centos7 ~ 13:15:25]# file -i hello.txt
hello.txt: text/plain; charset=us-ascii
#检测符号链接指向的文件
[root@centos7 ~ 13:13:43]# file -L /usr/bin/
/usr/bin/: directory
#批量检测多个文件
[root@centos7 ~ 13:14:14]# file *
anaconda-ks.cfg: ASCII text
hello.txt: ASCII text
#检测压缩包内部类型
[root@centos7 ~ 13:16:16]# file -z backup.tar.gz
cat
一次性查看文件的所有内容
-n:显示行号-E:在每行结束处显示"$"
命令配合标准输入和标准输出能够实现复杂的指令
[root@centos7 ~ 15:14:23]# cat
hello
hello
nihao
nihao
#使用ctrl+d 退出输出
[root@centos7 ~ 15:14:56]# cat > hello.txt
hello woeld
[root@centos7 ~ 15:15:29]# cat hello.txt
hello woeld
[root@centos7 ~ 15:15:39]# cat >> hello.txt
ni hao
[root@centos7 ~ 15:15:55]# cat hello.txt
hello woeld
ni hao
#使用<<自定义数据结束符号
[root@centos7 ~ 15:16:02]# cat <<EOF >hello.txt
> line 1
> line 2
> line 3
> EOF
[root@centos7 ~ 15:18:27]# cat hello.txt
line 1
line 2
line 3
head
默认开头10行
head -n +NUM:输出前NUM行(包含NUM行)
[hxl@centos7 / 17:00:27]$ head /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
# -n 查看bashrc文件的前3行
[hxl@centos7 / 17:00:40]$ head -n 3 /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
tail
默认结尾10行
tail -n +NUM:从第NUM行输出到末尾
tail -n -NUM:跳过最后NUM行,输出前面的内容
[hxl@centos7 / 17:01:38]$ tail /etc/bashrc
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
fi
# vim:ts=4:sw=4
PS1='[\[\e[91m\]\u\[\e[93m\]@\[\e[92;1m\]\h\[\e[0m\] \[\e[94m\]\W\[\e[0m\] \[\e[35m\]\t\[\e[0m\]]\[\e[93m\]\$\[\e[0m\] '
# -n 查看bashrc文件的后3行
[hxl@centos7 / 17:01:58]$ tail -n 3 /etc/bashrc
fi
# vim:ts=4:sw=4
PS1='[\[\e[91m\]\u\[\e[93m\]@\[\e[92;1m\]\h\[\e[0m\] \[\e[94m\]\W\[\e[0m\] \[\e[35m\]\t\[\e[0m\]]\[\e[93m\]\$\[\e[0m\] '
结合管道使用
# 查看bashrc文件内第21行到25行的内容
[hxl@centos7 / 17:06:46]$ cat -n /etc/bashrc | head -n 25 | tail -n 5
21 PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
22 fi
23 ;;
24 screen*)
25 if [ -e /etc/sysconfig/bash-prompt-screen ]; then
less
翻页查看,快捷键:
- G:跳转到最后一行
- g:跳转到第一行
/:搜索关键字,默认是忽略大小写,如果不忽略大小写,则直接在less界面按-i,再次-i又会忽略大小写,搜索下一个n,搜索下一个N
wc
用于统计文件或输入中行数、单词数、字节数
-l:仅统计行数-w:仅统计单词数-c:仅统计字节数(UTF-8,一个汉字3个字节)-m:统计字符数-L:显示最长行的长度(字符数)
[root@centos7 ~ 13:21:07]# cat hello.txt
hello Linux
welcome Linux
[root@centos7 ~ 13:21:03]# wc hello.txt
2 4 26 hello.txt
#解释:2行,4个单词,26字节,文件名
[root@centos7 ~ 13:22:00]# wc -l /etc/hosts
2 /etc/hosts
[root@centos7 ~ 13:25:10]# wc -w /etc/hosts
10 /etc/hosts
[root@centos7 ~ 13:25:27]# wc -c /etc/hosts
158 /etc/hosts
[root@centos7 ~ 13:26:09]# wc -m /etc/hosts
158 /etc/hosts
[root@centos7 ~ 13:27:19]# wc -L /etc/hosts
78 /etc/hosts
#统计多个文件数据
[root@centos7 ~ 13:25:42]# wc /etc/hosts /etc/fstab
2 10 158 /etc/hosts
12 60 541 /etc/fstab
14 70 699 总用量
script命令
是一个终端会话记录命令,用于将终端上的所有输入和输出内容完整记录到文件中,方便后续复盘或审计操作
# 记录终端中历史操作,并保存到指定文件,还可以同时记录操作时间
[root@centos7 ~ 16:20:26]# script -t=time.log record.log
Script started, file is record.log
# 或者
[root@centos7 ~ 16:21:34]# script -ttime.log record.log
Script started, file is record.log
[root@centos7 ~ 16:21:47]# echo hello world
hello world
[root@centos7 ~ 16:21:55]# hostname
centos7.hxl.cloud
[root@centos7 ~ 16:22:00]# date
2025年 09月 03日 星期三 16:22:05 CST
[root@centos7 ~ 16:22:10]# echo goodbye
goodbye
[root@centos7 ~ 16:22:17]# exit
exit
Script done, file is record.log
# 查看日志内容
[root@centos7 ~ 16:22:19]# cat record.log
脚本启动于 2025年09月03日 星期三 16时21分44秒
[root@centos7 ~ 16:21:47]# echo hello world
hello world
[root@centos7 ~ 16:21:55]# hostname
centos7.hxl.cloud
[root@centos7 ~ 16:22:00]# date
2025年 09月 03日 星期三 16:22:05 CST
[root@centos7 ~ 16:22:10]# echo goodbye
goodbye
[root@centos7 ~ 16:22:17]# exit
exit
Script done on 2025年09月03日 星期三 16时22分19秒
# 动态回放日志
[root@centos7 ~ 16:23:13]# scriptreplay -t time.log record.log
history命令
history #显示、管理或操作当前用户的历史命令记录
history -c #清理历史命令记录
bash快捷键
| 快捷键 | 作用 |
|---|---|
| Ctrl+a 或者 Home | 光标定位到命令行开头 |
| Ctrl+e 或者 End | 光标定位到命令行末尾 |
| Ctrl+u | 删除光标位置到命令行开头 |
| Ctrl+k | 删除光标位置到命令行末尾 |
| Ctrl+<- | 光标向左跳转一个单词 |
| Ctrl±> | 光标向右跳转一个单词 |
| Ctrl+r | 搜索历史命令 |
| Ctrl+w | 向左删除一个单词 |
| Esc+. | 打印上一个命令的最后一个参数 |
| Ctrl+l(L的小写) | 清空屏幕 |
| Ctrl+d | 登出终端 |
更多推荐



所有评论(0)