快乐的Linux命令行
1. 引言
for who: 新手; 其他平台转过来的人; 熟手参考书
2. 什么是 Shell
命令历史
history
[root@470904 update]# history | tail -10
1727 ls
1728 pwd
1729 mkdir update
1730 cd update/
1731 ls
1732 pwd
1733 scp *sql root@172.16.206.131:/root/test/chengchuanjie/publicdb/update
1734 history
1735 history | wc -l
1736 history | tail -10
[root@470904 update]# !1728
pwd
/mnt/shared/public/update
[root@470904 update]#
历史命令无时间戳解决方法
# 临时生效
export HISTTIMEFORMAT='%F %T '
# 永久生效
echo 'export HISTTIMEFORMAT="%F %T "' >> ~/.bashrc
source ~/.bashrc
- 检查效果
[root@470904 update]# export HISTTIMEFORMAT='%F %T '
[root@470904 update]# history | tail -10
1732 2025-02-11 18:19:36 pwd
1733 2025-02-11 18:20:16 scp *sql root@172.16.206.131:/root/test/chengchuanjie/publicdb/update
1734 2025-02-12 09:22:03 history
1735 2025-02-12 09:22:06 history | wc -l
1736 2025-02-12 09:22:18 history | tail -10
1737 2025-02-12 09:22:27 pwd
1738 2025-02-12 09:23:15 echo $HISTSIZE
1739 2025-02-12 09:56:41
1740 2025-02-12 09:56:43 export HISTTIMEFORMAT='%F %T '
1741 2025-02-12 09:56:48 history | tail -10
虚拟控制台
可以通过 Ctrl+Alt+F1 到 Ctrl+Alt+F6 访问
Shell终端快捷键
ctrl+a:跳转到开头
ctrl+e:跳转到结尾
ctrl+u:删除到开头
ctrl+k:删除到结尾
ctrl+w:删除一个 word
ctrl+->:向右跳转一个 word
ctrl+<-:向左跳转一个 word
3. 文件系统中跳转
pwd 打印当前目录
ls 短格式查看当前目录下的文件
ls -l 长格式查看当前目录下的文件
ls -d 查看目录
ls -al 查看隐藏文件
cd 进入当前用户家目录
cd - 进入到上次的目录
cd ~postgres 直接进入 postgres 用户家目录
cd /usr/local/bin 进入绝对路径
4. 探究操作系统
ls -l
ls -lt 按照文件 mtime 从新到老排序
ls -ltr 按照文件 mtime 从老到新排序
ls -lSh 按照文件大小, 从大到小排序
ls -lS 按照文件大小, 从大到小排序(目录值算 4KB )
file 查看文件类型
less 可上下翻译
more
ln -s target_file link_file 软连接
ln target_file link_file 硬链接
查看文件与尤其是目录占用大小, 从大到小排序 du -sh * | sort -rh
[root@470904 ~]# du -sh * | sort -rh
171M NVIDIA_CUDA-11.3_Samples
94M VirtualBox-6.1-6.1.50_161033_el7-1.x86_64.rpm
74M UTP_VTE
46M virtualmw.tgz
11M Oracle_VM_VirtualBox_Extension_Pack-6.1.50.vbox-extpack
428K lxm
164K res
160K res2
76K bak
40K storcli.log
8.0K ps3cli.log
4.0K Videos
4.0K thinclient_drives
4.0K Templates
4.0K sha256sum.txt
4.0K Public
4.0K ppp.pcap
4.0K Pictures
4.0K perl5
4.0K password_and_ip.txt
4.0K openEuerpassword
4.0K Music
4.0K MD5SUMS.txt
4.0K ifcfg-enp134s0f0-1
4.0K Downloads
4.0K Documents
4.0K Desktop
如果想排除某些目录 比如 /proc
[root@localhost ~]# du -sh /* --exclude=/proc |sort -rh
6.8G /Upgrade
6.4G /program
5.4G /usr
3.9G /VMS-B2101.1.2.6.250226
3.0G /VMS-B2101.1.2.6.250226.tar.gz
3.0G /VMS-B2101.1.2.6.250225.tar.gz
3.0G /VMS-B2101.1.2.5.250212.tar.gz
2.9G /home
2.7G /VMS-B2101.1.1.9.L11.250214.tar.gz
2.6G /VMS-B2101.1.1.9.202411282248.tar.gz
2.4G /VMS-B2101.1.1.5.L06.250221.tar.gz
1.5G /var
336M /opt
218M /boot
49M /etc
20M /mnt
18M /run
18M /driverfile
7.9M /dev
5.1M /root
2.8M /tmp
2.2M /UNVGuard_config_DB
1012K /Guard_config_DB
932K /Config_DB
76K /ts
16K /lost+found
16K /bash_completion.d
4.0K /srv
4.0K /media
4.0K /afs
0 /sys
0 /sbin
0 /lib64
0 /lib
0 /bin
5. 文件目录操作
touch test
mkdir test_dir
mkdir -p test_dir
cp test test2
cp test test2
/bin/cp test test2
mv test test3
mv test2 test_dir
cp -r test_dir test_dir2
rm
rm -rf
ln -s
6. 使用命令
什么是命令
- 可执行程序(文件)
- shell默认自带的命令
- shell 函数
- 命令别名
显示命令类型
type <command>
[root@localhost ~]# type pwd
pwd is a shell builtin
[root@localhost ~]#
[root@localhost ~]# type cp
cp is aliased to 'cp -i'
[root@localhost ~]#
[root@localhost ~]# type /bin/cp
/bin/cp is /bin/cp
[root@470904 os]# type which
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
显示可执行程序的位置
[root@localhost ~]# which ls
/usr/bin/ls
查看帮助
help cd
cd --help
man iptables
info ls
**alias 构建自己的命令**
7. 重定向
标准输入文件: stdin
标准输出文件: stdout
标准错误输出: stderr
echo test | passwd --stdin test
标准输出重定向与标准错误重定向
下面两这个命令是等效的
[root@localhost ~]# ls -l /bin/usr > res.log 2>&1
[root@localhost ~]# cat res.log
ls: cannot access '/bin/usr': No such file or directory
[root@localhost ~]#
[root@localhost ~]# ls -l /bin/usr &> res.log
[root@localhost ~]#
[root@localhost ~]# cat res.log
ls: cannot access '/bin/usr': No such file or directory
注意: 标准错误重定向必须在标准输出重定向之后, 否则不起作用
- 处理不需要的输出
/bin/bash test.sh >/dev/null 2>&1
/bin/bash test.sh &>/dev/null
标准输入重定向
cat data.sql
cat < data.sql
管道线
[root@localhost ~]# ls /root | sort -u | uniq -c
1 anaconda-ks.cfg
1 dbbk.sh.pid
1 logs
1 lxm
1 optlog_create_db.sql
1 optlog_create_obj.sql
1 optlog_init.sql
1 procedure.log
1 res2.log
1 res.log
1 sessionStore
1 spark
1 VMS-B2105.1.1.7.241102.tar.gz
1 zookeeper.out
head / tail
[root@localhost ~]# head -5 optlog_create_obj.sql
\connect optlog;
create domain int32 as int;
-- create imos table: tbl_version
[root@localhost ~]#
[root@localhost ~]# tail -5 optlog_create_obj.sql
COMMENT ON TABLE tbl_optsubtype_info IS '操作类型字典表';
COMMENT ON COLUMN tbl_optsubtype_info.opertype IS '业务类型枚举';
COMMENT ON COLUMN tbl_optsubtype_info.opersubtype IS '操作类型枚举';
COMMENT ON COLUMN tbl_optsubtype_info.opersubtypename IS '操作类型名称';
tee 从 stdin 读取数据并输出到stdout 与文件中
ll /usr/bin | tee ls.txt |grep zip
8. 从 Shell 眼中看世界
花括号
[root@localhost ~]# echo $((3+2))
5
[root@localhost ~]# echo $((3-2))
1
[root@localhost ~]# echo $((3*2))
6
[root@localhost ~]# echo $((3/2))
1
[root@localhost ~]# echo $((3**2))
9
[root@localhost ~]# echo $((3%2))
1
如何对浮点数进行计算
data=$(python<<EOF
print(3.14*2)
EOF
)
echo $data
[root@470904 uap]# data=$(python<<EOF
> print(3.14*2)
> EOF
> )
[root@470904 uap]# echo $data
6.28
双引号
[root@470904 uap]# echo "`date`"
Fri Feb 14 12:03:28 CST 2025
单引号
[root@470904 uap]# echo '`date`'
`date`
转义字符
[root@localhost lxm]# echo -e "hello\nworld"
hello
world
# 输出颜色
9 . 键盘高级技巧
ctrl+a:跳转到开头
ctrl+e:跳转到结尾
ctrl+u:删除到开头
ctrl+k:删除到结尾
ctrl+w:删除一个 word
ctrl+->:向右跳转一个 word
ctrl+<-:向左跳转一个 word
!! 执行上次历史命令
!10 执行history 中第 10 条命令
10 . 权限
owner; group; other
read; write; execute
chmod 改变权限
chmod u+x
chmod u-x
chmod +x 等效于 chmod a+x
chmod u+x,go=rw
umask
默认是 0022
su
环境变量还是当前用户
su - 用户名
环境变量变为切换之后的用户
sudo
visudo
普通用户密码执行 root 操作
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
postgres ALL=(ALL) NOPASSWD: ALL
chown
chown -R postgres:postgres /home/postgres/pgsql
11.进程
11.1 进程如何工作
内核进程-->init 进程(PID 为 1) -->/etc/rc.localxxx 脚本(系统服务脚本)
ps –报告当前进程快照
# 查看进程信息
ps -ef |grep java
# 查看进程的启动时间
ps -eo 'lstart,cmd,pid,ppid' | grep java
# 监控某个进程的状态变化
watch -n 1 'ps -p 1234 -o %cpu,%mem,etime'
[root@VM-0-12-centos ~]# cat test.sh
sleep 100
[root@VM-0-12-centos ~]# sh test.sh
^Z
[1]+ Stopped sh test.sh
[root@VM-0-12-centos ~]# jobs -l
[1]+ 31565 Stopped sh test.sh
[root@VM-0-12-centos ~]# bg %1
[1]+ sh test.sh &
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[1]+ 31565 Running sh test.sh &
[root@VM-0-12-centos ~]# ps -p 31565 -o pid,%cpu,%mem,etime,cmd
PID %CPU %MEM ELAPSED CMD
31565 0.0 0.0 00:27 sh test.sh
[root@VM-0-12-centos ~]#
top –显示任务
# 以 batch mode 显示前10 个进程信息
top -b -n 1 | head -17
# 自动刷新
watch -n 2 'top -b -n 1 | head -17'
top
是一个强大的系统监控工具,掌握以下技巧可提高效率:
- 按
P
、M
、T
快速排序,找出高 CPU、高内存, 高CPU 时间消耗进程 - 使用
top -b -n 1
结合grep
进行自动化分析。 - 配合
watch
进行定期监控,或结合cron
记录日志。 - 隐藏空闲进程(
i
)或指定用户(u
)查看特定进程。
jobs –列出活跃的任务
bg – 让进程继续在后台运行。
fg –将后台进程恢复到前台运行
[root@VM-0-12-centos ~]# cat test.sh
sleep 100
[root@VM-0-12-centos ~]# sh test.sh
^Z
[1]+ Stopped sh test.sh
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[1]+ 15365 Stopped sh test.sh
[root@VM-0-12-centos ~]# bg %1
[1]+ sh test.sh &
[root@VM-0-12-centos ~]# jobs -l
[1]+ 15365 Running sh test.sh &
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# bg %1
-bash: bg: job 1 already in background
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# fg %1
sh test.sh
^C
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[root@VM-0-12-centos ~]#
kill –给一个进程发送信号
常见信号:(kill -l)
信号 | 数值 | 作用 |
---|---|---|
SIGHUP |
1 | 终止进程并重新加载配置 |
SIGINT |
2 | 终止进程(相当于 Ctrl+C) |
SIGKILL |
9 | 强制终止进程(不可捕获、不可忽略) |
SIGTERM |
15 | 终止进程(默认) |
SIGSTOP |
19 | 暂停进程(可恢复) |
SIGCONT |
18 | 恢复暂停的进程 |
# kill -0 检查进程是否存在
if kill -0 1234 2>/dev/null; then
echo "进程 1234 存在"
else
echo "进程 1234 不存在"
fi
# kill -9 1234
[root@VM-0-12-centos ~]# sh test.sh &
[1] 14561
[root@VM-0-12-centos ~]# ps -ef |grep test.sh
root 14561 13464 0 15:13 pts/1 00:00:00 sh test.sh
root 14590 13464 0 15:13 pts/1 00:00:00 grep --color=auto test.sh
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# kill -STOP 14561
[1]+ Stopped sh test.sh
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[1]+ 14561 Stopped (signal) sh test.sh
[root@VM-0-12-centos ~]# kill -CONT 14561
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[1]+ 14561 Running sh test.sh &
[root@VM-0-12-centos ~]# kill -STOP 14561
[1]+ Stopped sh test.sh
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[1]+ 14561 Stopped (signal) sh test.sh
[root@VM-0-12-centos ~]# bg %1
[1]+ sh test.sh &
[root@VM-0-12-centos ~]#
[root@VM-0-12-centos ~]# jobs -l
[1]+ 14561 Running sh test.sh &
killall –杀死指定名字的进程
killall -9 java
shutdown –关机或重启系统
# 现在立即关机
shutdown now
# 设置 5 分钟后关机
shutdown +5
# 22:00 关机
shutdown 22:00
# 立即重启
shutdown -r now
# 十分钟之后重启
shutdown -r +10
# 设置 23:00 重启
shutdown -r 23:00
# 如果已经设置了关机或重启任务,可以通过以下命令取消:
shutdown -c
# init 0: 关机
# init 6 :重启
在使用 Ctrl-c 的情况下,会发送一个叫做 INT(Interrupt, 中断)的信号;
当使用 Ctrl-z 时,则发送一个叫做 TSTP(Terminal Stop,终端停止)的信号。
12. Shell 环境
登录 Shell 与 非登录 Shell
类别 | 登录 Shell | 非登录 Shell |
---|---|---|
触发方式 | ssh user@host |
直接运行 bash |
su - username |
su username |
|
TTY 登录 (Ctrl+Alt+F2 ) |
运行 Shell 脚本 (./script.sh ) |
|
bash --login |
终端内打开新 bash |
|
配置文件 | /etc/profile → ~/.bash_profile |
/etc/bashrc → ~/.bashrc |
主要影响 | 影响全局环境变量,如 $PATH |
影响交互式命令,如 alias |
变量加载 | export 变量影响所有子进程 |
仅影响当前 Shell |
13. VI 简介
1. 基本模式
Vim 有三种主要模式:
- 普通模式 (Normal Mode):用于导航和编辑。
- 插入模式 (Insert Mode):用于编辑文本。
- 命令模式 (Command Mode):用于执行命令。
默认情况下,Vim 启动时处于 普通模式。
2. 在不同模式之间切换
- 进入插入模式:
i
(在光标前插入)
或I
(在行首插入) - 退出插入模式:
Esc
- 进入命令模式:
:
(在普通模式下输入) - 进入可视模式:
v
(按字符选择) 或V
(按行选择)
3. 基本导航命令
- 移动光标:
h
:向左移动一个字符j
:向下移动一行k
:向上移动一行l
:向右移动一个字符
- 跳到行首:
0
- 跳到行尾:
$
- 跳到文件开始:
gg
- 跳到文件结尾:
G
- 跳到第 N 行:
N
(如:10
跳到第 10 行) - 跳到当前行的某个字符:
f<char>
(如:fa
跳到当前行第一个a
字符) - 跳到当前行的下一个字符:
t<char>
(如:ta
跳到当前行第一个a
字符之前)
4. 文本编辑命令
- 删除字符:
x
(删除光标所在字符) - 删除到行尾:
D
- 删除到行首:
d0
- 删除一行:
dd
- 删除多行:
dN
(删除从当前行到第 N 行) - 剪切选中的文本:
d
(可视模式下) - 复制一行:
yy
- 复制多行:
yN
(复制当前行和以下的 N 行) - 粘贴:
p
(粘贴到光标后) 或P
(粘贴到光标前) - 撤销:
u
- 重做:
Ctrl + r
- 替换字符:
r<char>
(将光标所在字符替换为<char>
) - 替换一个单词:
cw
(替换当前光标处的单词)
5. 查找与替换
- 查找文本:
/text
(向下查找text
) - 查找上一个文本:
?text
(向上查找text
) - 查找下一个匹配项:
n
- 查找上一个匹配项:
N
- 替换单个匹配项:
:s/old/new
- 替换当前行的所有匹配项:
:s/old/new/g
- 替换整个文件中的所有匹配项:
:%s/old/new/g
- 替换并询问确认:
:%s/old/new/gc
(c
表示确认每次替换)
6. 可视模式
- 按字符选择:
v
(进入可视模式,按字符选择) - 按行选择:
V
(进入可视模式,按行选择) - 块选择:
Ctrl + v
(进入块选择模式) - 删除选中的文本:
d
- 复制选中的文本:
y
- 粘贴选中的文本:
p
7. 文件操作
- 保存文件:
:w
- 保存并退出:
:wq
或ZZ
- 退出不保存:
:q!
- 只读模式下退出:
:q
- 另存为:
:w filename
- 打开新文件:
:e filename
- 关闭文件:
:bd
(删除当前缓冲区)
8. 窗口管理
- 水平分割窗口:
:split
或:sp
- 垂直分割窗口:
:vsplit
或:vs
- 切换窗口:
Ctrl + w
然后按h/j/k/l
(向左/下/上/右切换) - 关闭窗口:
:q
9. 配置与设置
- 显示行号:
:set number
或:set nu
- 关闭行号:
:set nonumber
或:set nonu
- 高亮搜索结果:
:set hlsearch
- 关闭搜索高亮:
:set nohlsearch
- 启用自动换行:
:set wrap
- 禁用自动换行:
:set nowrap
- 启用行尾空格显示:
:set list
- 禁用行尾空格显示:
:set nolist
10. 撤销与重做
- 撤销:
u
- 重做:
Ctrl + r
- 撤销多个操作:
u
(多次按u
可撤销多个操作) - 恢复已删除的文本:
p
11. 其他有用的命令
- 保存并退出:
:wq
或ZZ
- 跳转到文件顶部:
gg
- 跳转到文件底部:
G
- 查看帮助文档:
:help
- 查找命令的帮助文档:
:help <command>
(如:help :wq
)
12. 快捷键总结
快捷键 | 描述 |
---|---|
i |
进入插入模式(在光标前) |
I |
进入插入模式(行首) |
a |
进入插入模式(光标后) |
A |
进入插入模式(行尾) |
Esc |
退出插入模式 |
yy |
复制当前行 |
dd |
删除当前行 |
p |
粘贴剪贴板内容 |
u |
撤销上一个操作 |
Ctrl + r |
重做上一个操作 |
/ |
查找文本(向下) |
? |
查找文本(向上) |
n |
查找下一个匹配 |
N |
查找上一个匹配 |
:w |
保存文件 |
:q |
退出 Vim |
:wq |
保存并退出 |
:q! |
强制退出不保存 |
总结
Vim 是一个非常强大的文本编辑器,具有丰富的命令和模式。掌握了上述命令,你可以更加高效地编辑和管理文件。在 Vim 中练习和熟悉命令可以大大提高你的工作效率。
14. 自定制 Shell 提示符
15. 软件包管理
## 安装 rpm 包
rpm -iVh *.rpm --force
## 升级 rpm 包
rpm -Uvh *.rpm --nodeps
## 卸载
rpm -e git --force
## 解压 rpm 包
rpm2cpio *.rpm | cpio -idmv
## 查看哪个软件包安装了/usr/bin/vim 这个文件
rpm -qf /usr/bin/vim