[root@localhost ~]# who am i
root pts/1 2022-04-02 22:12 (172.31.10.1)
[root@localhost ~]# who
root :0 2022-04-02 21:35 (:0)
root pts/0 2022-04-02 21:39 (:0)
root pts/1 2022-04-02 22:12 (172.31.10.1)
[root@localhost ~]# w
22:18:32 up 44 min, 3 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root :0 :0 21:35 ?xdm? 35.14s 0.28s /usr/libexec/gnome-session-binary --session gnome-classic
root pts/0 :0 21:39 18:21 0.05s 0.05s bash
root pts/1 172.31.10.1 22:12 0.00s 0.03s 0.01s w
[root@localhost ~]# users
root root root
[root@localhost ~]# tty
/dev/pts/1
[root@localhost ~]# uname
Linux
[root@localhost ~]# uname -n
localhost.localdomain
[root@localhost ~]# uname -i
x86_64
[root@localhost ~]# uname -in
localhost.localdomain x86_64
[root@localhost ~]# uname -p
x86_64
[root@localhost ~]# uname -a
Linux localhost.localdomain 5.4.17-2136.305.5.4.el7uek.x86_64 #2 SMP Mon Mar 28 19:02:05 PDT 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname --help
Usage: uname [OPTION]...
Print certain system information. With no OPTION, same as -s.
-a, --all print all information, in the following order,
except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type or "unknown"
-i, --hardware-platform print the hardware platform or "unknown"
-o, --operating-system print the operating system
--help display this help and exit
--version output version information and exit
GNU coreutils online help: //www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'uname invocation'
[root@localhost ~]# date
Sat Apr 2 23:18:34 CST 2022
# 查看当前日历
[root@localhost ~]# cal
April 2022
Su Mo Tu We Th Fr Sa
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
# 查看2022年1月份
[root@localhost ~]# cal 1 2022
January 2022
Su Mo Tu We Th Fr Sa
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
# 查看2022年整年的日历
[root@localhost ~]# cal 2022
2022
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
...
[dongshan@localhost ~]$ whoami
dongshan
[dongshan@localhost ~]$ su root
Password:
[root@localhost dongshan]# whoami
root
[root@localhost dongshan]# exit
exit
[dongshan@localhost ~]$ whoami
dongshan
# 修改当前用户的密码
[root@localhost ~]# passwd
Changing password for user root.
New password:
# root用户可以修改指定用户的密码
[root@localhost ~]# passwd dongshan
Changing password for user dongshan.
New password:
# 查看指定用户的密码状态
[root@localhost ~]# passwd -S
passwd: This option requires a user name.
[root@localhost ~]# passwd -S root
root PS 1969-12-31 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@localhost ~]# passwd -S dongshan
dongshan PS 1969-12-31 0 99999 7 -1 (Password set, SHA512 crypt.)
# 新增一个cat用户
[root@localhost ~]# useradd cat
# --status(-S)查看用户密码状态,此时未设置密码
[root@localhost ~]# passwd --status cat
cat LK 2022-04-02 0 99999 7 -1 (Password locked.)
# 给cat用户设置密码
[root@localhost ~]# passwd cat
Changing password for user cat.
New password:
BAD PASSWORD: The password fails the dictionary check - it does not contain enough DIFFERENT characters
Retype new password:
passwd: all authentication tokens updated successfully.
# 设置成功后查看密码状态
[root@localhost ~]# passwd --status cat
cat PS 2022-04-02 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@localhost ~]# passwd --help
Usage: passwd [OPTION...]
-k, --keep-tokens keep non-expired authentication tokens
-d, --delete delete the password for the named account (root only)
-l, --lock lock the password for the named account (root only)
-u, --unlock unlock the password for the named account (root only)
-e, --expire expire the password for the named account (root only)
-f, --force force operation
-x, --maximum=DAYS maximum password lifetime (root only)
-n, --minimum=DAYS minimum password lifetime (root only)
-w, --warning=DAYS number of days warning users receives before password expiration (root only)
-i, --inactive=DAYS number of days after password expiration when an account becomes disabled (root only)
-S, --status report password status on the named account (root only)
--stdin read new tokens from stdin (root only)
Help options:
-?, --help Show this help message
--usage Display brief usage message
[root@localhost ~]# whatis passwd
sslpasswd (1ssl) - compute password hashes
passwd (1) - update user's authentication tokens
passwd (5) - password file
[root@localhost ~]# whatis uname
uname (1) - print system information
uname (1p) - return system name
uname (2) - get name and information about current kernel
uname (3p) - get the name of the current system
[root@localhost ~]# whatis --help
Usage: whatis [OPTION...] KEYWORD...
-d, --debug emit debugging messages
-v, --verbose print verbose warning messages
-r, --regex interpret each keyword as a regex
-w, --wildcard the keyword(s) contain wildcards
-l, --long do not trim output to terminal width
-C, --config-file=FILE use this user configuration file
-L, --locale=LOCALE define the locale for this search
-m, --systems=SYSTEM use manual pages from other systems
-M, --manpath=PATH set search path for manual pages to PATH
-s, --sections=LIST, --section=LIST
search only these sections (colon-separated)
-?, --help give this help list
--usage give a short usage message
-V, --version print program version
Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.
Report bugs to cjwatson@debian.org.
# 有8个不同的章节,158最常用
# 第一个章节,为命令使用说明
[root@localhost ~]# man passwd
PASSWD(1)
# 第5个章节,命令的文件说明
[root@localhost ~]# man 5 passwd
PASSWD(5)
# 第8个章节,管理命令,root才有
[root@localhost ~]# man lvm
LVM(8)
# 浏览帮助页信息时,可进入搜索
# /passwd表示向后搜索passwd,?passwd表示向前搜索
# n表示下一个 N表示上一个
# 使用-k搜索关键字
[root@localhost ~]# man -k useradd
luseradd (1) - Add an user
useradd (8) - create a new user or update default new user information
# 使用-f查看命令简要说明,等同与whatis
[root@localhost ~]# man -f useradd
useradd (8) - create a new user or update default new user information
[root@localhost ~]# whatis useradd
useradd (8) - create a new user or update default new user information
# 也是分不同章节,可以继续按p或者n查看前一个或者后一个
# tab跳转下一个超链接(*表示)
# u跳转到上层章节 s或者/可以进入搜索
[root@localhost ~]# info who
File: coreutils.info, Node: who invocation, Prev: users invocation, Up: User information
20.6 'who': Print who is currently logged in
[root@VM-8-12-centos /]# ll /
total 72
# /bin /usr/bin /usr/local/bin 常用的可执行文件
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
# 内核启动时使用的文件
dr-xr-xr-x. 5 root root 4096 Jul 20 2021 boot
drwxr-xr-x 3 root root 4096 Mar 26 20:39 data
# 设备
drwxr-xr-x 20 root root 3040 Mar 26 20:34 dev
# 系统主要配置文件
drwxr-xr-x. 98 root root 12288 Mar 26 20:34 etc
# 家目录
drwxr-xr-x. 3 root root 4096 Jul 20 2021 home
# 函数库
lrwxrwxrwx. 1 root root 7 Mar 7 2019 lib -> usr/lib
# 函数库
lrwxrwxrwx. 1 root root 9 Mar 7 2019 lib64 -> usr/lib64
# 系统崩溃时生成存放
drwx------. 2 root root 16384 Mar 7 2019 lost+found
# 挂载点
drwxr-xr-x. 2 root root 4096 Apr 11 2018 media
# 挂载点
drwxr-xr-x. 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x. 6 root root 4096 Mar 26 19:37 opt
# 虚拟文件,常驻内存,存放系统运行所需要的信息,反馈内核环境
dr-xr-xr-x 112 root root 0 Mar 26 20:34 proc
# root家目录
dr-xr-x---. 7 root root 4096 Mar 26 20:53 root
drwxr-xr-x 27 root root 980 Mar 26 20:35 run
# /sbin /usr/sbin /usr/local/sbin 系统的可执行文件
lrwxrwxrwx. 1 root root 8 Mar 7 2019 sbin -> usr/sbin
# 存放所有与服务器相关的服务
drwxr-xr-x. 2 root root 4096 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Mar 26 20:34 sys
# 临时文件存放
drwxrwxrwt. 9 root root 4096 Apr 2 12:17 tmp
# 存放系统的应用和命令相关的系统数据 是unix system resources 的缩写
drwxr-xr-x. 14 root root 4096 Jan 8 2021 usr
# 存放系统运行过程中经常变化的文件 如log、mail
drwxr-xr-x. 20 root root 4096 Jan 8 2021 var
# 打印当前绝对路径
[root@VM-8-12-centos ~]# pwd
/root
# 跳转到家目录
[root@VM-8-12-centos ~]# cd ~
# 跳转到指定目录
[root@VM-8-12-centos ~]# cd /home/lighthouse/
# 返回上一次的目录
[root@VM-8-12-centos ~]# cd -
/home/lighthouse
# 返回上一次的目录
[root@VM-8-12-centos lighthouse]# cd -
/root
# 列出根目录中的内容
[root@VM-8-12-centos ~]# ls /
bin boot data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# -l 更详细的列出
[root@VM-8-12-centos ~]# ls -l /
total 72
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
dr-xr-xr-x. 5 root root 4096 Jul 20 2021 boot
...
# -a 全部文件 -h 人性化的展示(文件大小展示友好)
[root@VM-8-12-centos ~]# ls -ahl /
total 80K
dr-xr-xr-x. 19 root root 4.0K Apr 2 17:09 .
dr-xr-xr-x. 19 root root 4.0K Apr 2 17:09 ..
lrwxrwxrwx. 1 root root 7 Mar 7 2019 bin -> usr/bin
...
# -d 只展示指定目录的信息
[root@VM-8-12-centos ~]# ls -ald /
dr-xr-xr-x. 19 root root 4096 Apr 2 17:10 /
# 当前有一个my.txt文件
[lighthouse@VM-8-12-centos ~]$ ll
total 4
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:24 my.txt
# 复制
[lighthouse@VM-8-12-centos ~]$ cp my.txt my1.txt
# 复制成功 但发现时间变了
[lighthouse@VM-8-12-centos ~]$ ll
total 8
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:25 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:24 my.txt
# -p 保留特定属性的复制
[lighthouse@VM-8-12-centos ~]$ cp -p my.txt my2.txt
You have new mail in /var/spool/mail/root
# my2.txt的时间戳没有变
[lighthouse@VM-8-12-centos ~]$ ll
total 12
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:25 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:24 my2.txt
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:24 my.txt
# 创建my目录 拷贝my1.txt my2.txt 到my目录中
[lighthouse@VM-8-12-centos ~]$ mkdir my
[lighthouse@VM-8-12-centos ~]$ cp my1.txt my2.txt my
# 验证结果
[lighthouse@VM-8-12-centos ~]$ ls -l my
total 8
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:42 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:42 my2.txt
# -r 递归拷贝my目录中的文件到my1中
[lighthouse@VM-8-12-centos ~]$ cp -r my my1
# 验证
[lighthouse@VM-8-12-centos ~]$ ll my1
total 8
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:43 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 30 Apr 2 17:43 my2.txt
# 另外还有-i交互提示-f强制执行
# 重命名文件夹的使用
$ mv my mymy
# -p 参数会创建所有不存在的目录
$ mkdir -p my/my1/my2
# 同时创建两个文件
[lighthouse@VM-8-12-centos ~]$ touch my.txt my1.txt
[lighthouse@VM-8-12-centos ~]$ ll
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my.txt
[lighthouse@VM-8-12-centos ~]$ ll
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
drwxrwxr-x 3 lighthouse lighthouse 4096 Apr 2 17:50 mymy
-rw-rw-r-- 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
## 递归删除目录mymy
[lighthouse@VM-8-12-centos ~]$ rm -r mymy
[lighthouse@VM-8-12-centos ~]$ ll
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
# 需要服务器开启vsftp服务
C:\Users\eastqi>sftp dongshan@172.31.10.79
dongshan@172.31.10.79's password:
Connected to 172.31.10.79.
# lcd 本地目录跳转
sftp> lcd /
# 展示当前本地目录
sftp> lls
Volume in drive C is Windows-SSD
Volume Serial Number is CECF-E95B
Directory of C:\
2018/12/26 08:32 <DIR> Drivers
2021/12/16 18:16 <DIR> gmall_log
...
[root@localhost ~]# ls
1.jpg anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public :q Templates Videos
# 图片类型
[root@localhost ~]# file 1.jpg
1.jpg: PNG image data, 195 x 140, 8-bit/color RGBA, non-interlaced
# 文本类型
[root@localhost ~]# file win10.txt
win10.txt: UTF-8 Unicode text, with no line terminators
# 可执行文件
[root@localhost ~]# file /bin/pwd
/bin/pwd: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=db6415180d5be29f42ce8ab441b1f3675060d245, stripped
# 符号链接
[root@localhost ~]# file /bin/awk
/bin/awk: symbolic link to `gawk
# 展示文件全部内容
[root@localhost ~]# cat win10.txt
你好hello!你好hello!你好hello!
你好hello!
你好hello!你好hello!
你好hello!
你好hello!
你好hello!
# 展示换行符
cat -A win10.txt
# 去掉多余的换行符
cat -s win10.txt
[root@localhost ~]# cat -s win10.txt
你好hello!你好hello!你好hello!
你好hello!
你好hello!你好hello!
你好hello!
你好hello!
你好hello!
# 显示行号
[root@localhost ~]# cat -bs win10.txt
1 你好hello!你好hello!你好hello!
2 你好hello!
3 你好hello!你好hello!
4 你好hello!
5 你好hello!
6 你好hello!
# 创建新文件并写入内容后按ctrl+d保存退出
[root@localhost ~]# cat > news
haha
hehe
xixi
# 验证写入的内容
[root@localhost ~]# cat news
haha
hehe
xixi
# -n 指定浏览的行数
[root@localhost ~]# head -n 2 news
haha
hehe
# -n 浏览文件最后n行数据
[root@localhost ~]# tail -n 2 news
hehe
xixi
# -f 持续输出文件的最后n行内容
[root@localhost ~]# tail -f -n 2 news
hehe
xixi
# 同时展示行数单词数字符数
[root@localhost ~]# wc win10.txt
12 6 120 win10.txt
# 行数
[root@localhost ~]# wc -l win10.txt
12 win10.txt
# 单词数
[root@localhost ~]# wc -w win10.txt
6 win10.txt
# 字符数
[root@localhost ~]# wc -c win10.txt
120 win10.txt
[root@localhost ~]# more /usr/share/dict/words
# 系统支持的所有shell 一般使用bash
[root@localhost ~]# cat /etc/shells
/bin/sh # Bourn shell
/bin/bash # Bourn-Again shell
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh # C shell
[root@localhost ~]# type pwd
pwd is a shell builtin
# 显示所有包含指定命令的命令
[root@localhost ~]# type -a pwd
pwd is a shell builtin
pwd is /usr/bin/pwd
# 是内置命令
[root@localhost ~]# type -t pwd
builtin
# 显示完整的文件名
[root@localhost ~]# type -P pwd
/usr/bin/pwd
[root@localhost ~]# type useradd
useradd is /usr/sbin/useradd
[root@localhost ~]# type -a useradd
useradd is /usr/sbin/useradd
# 是外部命令
[root@localhost ~]# type -t useradd
file
[root@localhost ~]# type -p useradd
/usr/sbin/useradd
[root@localhost ~]# mkdir my
[root@localhost ~]# cd my
[root@localhost my]# touch my1.txt my2.txt my11.txt my22.txt
# *匹配0或多个字符
[root@localhost my]# ls my*
my11.txt my1.txt my22.txt my2.txt
# ?匹配一个字符
[root@localhost my]# ls my?.txt
my1.txt my2.txt
[root@localhost my]# ls my??.txt
my11.txt my22.txt
# [13] 匹配1或者3
[root@localhost my]# ls my[13].txt
my1.txt
# [^13] 匹配非1和非3
[root@localhost my]# ls my[^13].txt
my2.txt
[root@localhost my]# ls my[^13]?.txt
my22.txt
# 输入ls m,按tab会补全ls my,按2次tab会列出所有my开头的文件
[root@localhost my]# ls my
my11.txt my1.txt my22.txt my2.txt
# 同样使用命令,按两次tab
[root@localhost my]# wh
whatis whereis which while whiptail who whoami
# 跳转到cat的家目录
[root@localhost my]# cd ~cat
[root@localhost cat]# pwd
/home/cat
# 调整到当前用户家目录
[root@localhost cat]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# history
1 yum update
2 ifconfig
3 ping www.baidu.com
...
# !加历史命令编号,可以重新执行该命令
[root@localhost ~]# !2
ifconfig
ens33: flags=4163,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.10.79 netmask 255.255.255.0 broadcast 172.31.10.255
inet6 fe80::8875:bb45:99ae:fc3b prefixlen 64 scopeid 0x20
ether 00:0c:29:50:8f:77 txqueuelen 1000 (Ethernet)
RX packets 7475 bytes 4650783 (4.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3875 bytes 425009 (415.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
...
# ctrl+r 输入ls ,如果不是可以继续按ctrl+r查找
(reverse-i-search)`ls': ls -l /usr/share/man/
# 看效果很容易意会
[root@localhost my]# touch my{1,2,3}.txt
[root@localhost my]# ls
my1.txt my2.txt my3.txt
# 继续看效果
[root@localhost my]# touch {my,your}{1,2,3}.txt
[root@localhost my]# ls
my1.txt my2.txt my3.txt your1.txt your2.txt your3.txt
# 看下面三个示例即可意会,date是打印当前时间的命令
[root@localhost my]# echo "The time is date"
The time is date
[root@localhost my]# echo "The time is `date`"
The time is Mon Apr 4 00:58:44 CST 2022
[root@localhost my]# echo "The time is $(date)"
The time is Mon Apr 4 00:58:58 CST 2022
# 定义两个变量x=4 y=5
[root@localhost my]# x=4 y=5
# 加
[root@localhost my]# echo $[$x+$y]
9
# 减
[root@localhost my]# echo $[$x-$y]
-1
# 乘
[root@localhost my]# echo $[$x*$y]
20
# 除
[root@localhost my]# echo $[$x/$y]
0
# 求余数
[root@localhost my]# echo $[$x%$y]
4
# 阶乘4的5次方
[root@localhost my]# echo $[$x**$y]
1024
# $符号是对变量的引用,因为没有$1变量,所以为空
[root@localhost my]# echo "One dog sale $110"
One dog sale 10
# 使用\转义
[root@localhost my]# echo "One dog sale \$110"
One dog sale $110
# 后面没有要转义时
[root@localhost my]# echo "One dog sale \110"
One dog sale \110
# 转义自己
[root@localhost my]# echo "One dog sale \\110"
One dog sale \110
[root@localhost my]# ls
my1.txt my2.txt my3.txt your1.txt your2.txt your3.txt
# \放在一行最后表示换行
[root@localhost my]# rm -f my\
> 1.txt
[root@localhost my]# ls
my2.txt my3.txt your1.txt your2.txt your3.txt
# *没有转义会打印当前目录所有文件名
[root@localhost my]# echo *** hello ***
my2.txt my3.txt your1.txt your2.txt your3.txt hello my2.txt my3.txt your1.txt your2.txt your3.txt
# 单引号使用
[root@localhost my]# echo '*** hello ***'
*** hello ***
# 双引号使用
[root@localhost my]# echo "*** hello ***"
*** hello ***
# 双引号禁止所有命令行扩展功能但不包括$ ` \ !
[root@localhost my]# echo "sale $100"
sale 00
# 单引号禁止所有命令行扩展功能
[root@localhost my]# echo 'sale $100'
sale $100
[root@localhost /]# ls -l /dev/std*
# 标准错误文件描述符为2
lrwxrwxrwx. 1 root root 15 Apr 3 21:32 /dev/stderr -> /proc/self/fd/2
# 标准输入文件描述符为0
lrwxrwxrwx. 1 root root 15 Apr 3 21:32 /dev/stdin -> /proc/self/fd/0
# 标准输出文件描述符为1
lrwxrwxrwx. 1 root root 15 Apr 3 21:32 /dev/stdout -> /proc/self/fd/1
# -name 通过文件名查找,查找完成后默认的action为打印
[root@localhost my]# find /root/my -name "my*"
/root/my
/root/my/my2.txt
/root/my/my3.txt
# -print 默认action 此外还有-exec {}\; -ok {}\; -ls
[root@localhost my]# find /root/my -name "my*" -print
/root/my
/root/my/my2.txt
/root/my/my3.txt
# -exec 对每个查找到的文件或目录执行操作,示例为执行 ls -l
[root@localhost my]# find /root/my -name "my*.txt" -exec ls -l {} \;
-rw-r--r--. 1 root root 0 Apr 4 00:54 /root/my/my2.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 /root/my/my3.txt
# -ls 类似上面示例的功能
[root@localhost my]# find /root/my -name "my*.txt" -ls
204597923 0 -rw-r--r-- 1 root root 0 Apr 4 00:54 /root/my/my2.txt
204599242 0 -rw-r--r-- 1 root root 0 Apr 4 00:54 /root/my/my3.txt
# -ok 执行操作的时候进行确认,exec的交互模式
[root@localhost my]# find /root/my -name "my*.txt" -ok ls -l {} \;
< ls ... /root/my/my2.txt > ? y
-rw-r--r--. 1 root root 0 Apr 4 00:54 /root/my/my2.txt
< ls ... /root/my/my3.txt > ? y
-rw-r--r--. 1 root root 0 Apr 4 00:54 /root/my/my3.txt
# -mtime(+-n) 修改时间 -8表示在8天内修改的,如果是+8则表示8天前修改
[root@VM-12-4-centos 202203]# find . -mtime -8 -ls
1179662 12 drwxr-xr-x 2 root root 12288 Mar 29 00:04 .
1179735 56 -rw-r--r-- 1 root root 53613 Mar 28 22:42 ./1508454531506110464.jpg
1179822 20 -rw-r--r-- 1 root root 16565 Mar 29 00:04 ./1508475075144187904.jpg
# -atime(+-n) 访问时间
[root@VM-12-4-centos 202203]# find . -atime -8 -ls
1179662 12 drwxr-xr-x 2 root root 12288 Mar 29 00:04 .
1179715 200 -rw-r--r-- 1 root root 202308 Mar 21 17:06 ./1505833132979388416.jpeg
1179693 40 -rw-r--r-- 1 root root 38126 Mar 21 16:53 ./1505829847316824064.jpeg
1179699 24 -rw-r--r-- 1 root root 21368 Mar 21 16:55 ./1505830476810551296.jpeg
# 另外还有-size(+-n) 大小 (512字节*n)的查询条件以及其他
[root@localhost my]# ls
my2.txt my3.txt your1.txt your2.txt your3.txt
# >覆盖 将输出重定向到my.txt
[root@localhost my]# ls -l > my.txt
[root@localhost my]# cat my.txt
total 0
-rw-r--r--. 1 root root 0 Apr 4 00:54 my2.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 my3.txt
-rw-r--r--. 1 root root 0 Apr 4 18:07 my.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 your1.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 your2.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 your3.txt
# >> 追加
[root@localhost my]# date >> my.txt
[root@localhost my]# cat my.txt
total 0
-rw-r--r--. 1 root root 0 Apr 4 00:54 my2.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 my3.txt
-rw-r--r--. 1 root root 0 Apr 4 18:07 my.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 your1.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 your2.txt
-rw-r--r--. 1 root root 0 Apr 4 00:54 your3.txt
Mon Apr 4 18:08:18 CST 2022
# 如下1和3行为标准错误 2行为标准输出
[dongshan@localhost my]$ find /etc/ -name passwd
find: ‘/etc/grub.d’: Permission denied
/etc/passwd
find: ‘/etc/selinux/targeted/active’: Permission denied
...
# 将标准输出重定向到std.out 标准错误重定向到std.err
[dongshan@localhost my]$ find /etc/ -name passwd 1>std.out 2>std.err
[dongshan@localhost my]$ cat std.out
/etc/passwd
/etc/pam.d/passwd
[dongshan@localhost my]$ cat std.err
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/selinux/targeted/active’: Permission denied
...
# 建议使用 2>&1 表示将标准错误重定向到和标准输出一样的文件
[dongshan@localhost my]$ find /etc/ -name passwd 1>std.out 2>&1
[dongshan@localhost my]$ cat std.out
find: ‘/etc/grub.d’: Permission denied
/etc/passwd
find: ‘/etc/selinux/targeted/active’: Permission denied
...
# 不建议使用 &表示所有的文件描述符(0、1、2)
[dongshan@localhost my]$ find /etc/ -name passwd &>std.out
[dongshan@localhost my]$ cat std.out
find: ‘/etc/grub.d’: Permission denied
/etc/passwd
find: ‘/etc/selinux/targeted/active’: Permission denied
[dongshan@localhost my]$ cat my.txt
ABCDE
# 使用tr(translate)命令转成小写字母 输出到屏幕
[dongshan@localhost my]$ tr 'A-Z' 'a-z' < my.txt
abcde
# 输出重定向到my1.txt
[dongshan@localhost my]$ tr 'A-Z' 'a-z' < my.txt > my1.txt
[dongshan@localhost my]$ cat my1.txt
abcde
# -d 删除
[dongshan@localhost my]$ tr -d 'e' < my1.txt
abcd
[dongshan@localhost my]$ cat my.txt
'1', '2021-10-15 12:20:50', '2021-11-17 18:43:22', 'ROOT', '超级管理员', '1', '2', '1', '系统超级管理员角色不可编辑'
'2', '2021-10-15 14:01:11', '2021-11-17 18:43:22', 'AGENT_ROOT', '代理角色', '1', '2', '2', '所有代理的权限在该角色下配置|不可编辑'
'3', '2021-10-26 17:00:36', '2022-01-18 16:55:40', 'AGENT_YW', '业务员', '1', '2', '3', '所有代理业务员的权限在该角色下配置|不可编辑'
'6', '2022-01-02 23:49:20', '2022-01-02 23:49:20', 'admin', '普通管理员', '1', '2', '4', ''
# -f2 剪切第二个字段 -d ',' 分隔符为,
[dongshan@localhost my]$ cut -f2 -d ',' my.txt
'2021-10-15 12:20:50'
'2021-10-15 14:01:11'
'2021-10-26 17:00:36'
'2022-01-02 23:49:20'
# -f1,2,4 第1,2,4三个字段都剪切
[dongshan@localhost my]$ cut -f1,2,4 -d ',' my.txt
'1', '2021-10-15 12:20:50', 'ROOT'
'2', '2021-10-15 14:01:11', 'AGENT_ROOT'
'3', '2021-10-26 17:00:36', 'AGENT_YW'
'6', '2022-01-02 23:49:20', 'admin'
# 构造code.txt文件
[dongshan@localhost my]$ cut -f4 -d ',' my.txt > code.txt
[dongshan@localhost my]$ cat code.txt
'ROOT'
'AGENT_ROOT'
'AGENT_YW'
'admin'
# 构造name.txt文件
[dongshan@localhost my]$ cut -f5 -d ',' my.txt > name.txt
[dongshan@localhost my]$ cat name.txt
'超级管理员'
'代理角色'
'业务员'
'普通管理员'
# 粘贴即横向合并两个文件到cn.txt,默认分隔符为tab
[dongshan@localhost my]$ paste code.txt name.txt > cn.txt
[dongshan@localhost my]$ cat cn.txt
'ROOT' '超级管理员'
'AGENT_ROOT' '代理角色'
'AGENT_YW' '业务员'
'admin' '普通管理员'
# -d 使用指定的分割符分割
[dongshan@localhost my]$ paste -d ',' code.txt name.txt > cn.txt
[dongshan@localhost my]$ cat cn.txt
'ROOT', '超级管理员'
'AGENT_ROOT', '代理角色'
'AGENT_YW', '业务员'
'admin', '普通管理员'
[dongshan@localhost my]$ paste code.txt name.txt > cn.txt
[dongshan@localhost my]$ cat -A cn.txt
'ROOT'^I 'M-hM-6M-^EM-gM-:M-'M-gM-.M-!M-gM-^PM-^FM-eM-^QM-^X'$
'AGENT_ROOT'^I 'M-dM-;M-#M-gM-^PM-^FM-hM-'M-^RM-hM-^IM-2'$
'AGENT_YW'^I 'M-dM-8M-^ZM-eM-^JM-!M-eM-^QM-^X'$
'admin'^I 'M-fM-^YM-.M-iM-^@M-^ZM-gM-.M-!M-gM-^PM-^FM-eM-^QM-^X'$
^I$
# -x 参数将tab转成空格
[dongshan@localhost my]$ col -x < cn.txt > cn1.txt
[dongshan@localhost my]$ cat -A cn1.txt
'ROOT' 'M-hM-6M-^EM-gM-:M-'M-gM-.M-!M-gM-^PM-^FM-eM-^QM-^X'$
'AGENT_ROOT' 'M-dM-;M-#M-gM-^PM-^FM-hM-'M-^RM-hM-^IM-2'$
'AGENT_YW' 'M-dM-8M-^ZM-eM-^JM-!M-eM-^QM-^X'$
'admin' 'M-fM-^YM-.M-iM-^@M-^ZM-gM-.M-!M-gM-^PM-^FM-eM-^QM-^X'$
$
# -t 指定分割符号 -k3 指定排序的列为第三列 -n 使用数字大小排序(默认按ascii码) -r降序 另外还有: -u去重 -f忽略大小写
[dongshan@localhost my]$ sort -t ':' -k3 -n -r /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
cat:x:1001:1001::/home/cat:/bin/bash
dongshan:x:1000:1000:dongshan:/home/dongshan:/bin/bash
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
libstoragemgmt:x:998:995:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin
[dongshan@localhost my]$ cat my.txt
A
A
a
b
b
B
C
E
# 默认去重
[dongshan@localhost my]$ uniq my.txt
A
a
b
B
C
E
# -c 打印重复次数
[dongshan@localhost my]$ uniq -c my.txt
2 A
1 a
2 b
1 B
1 C
1 E
# -i 忽略大小写 -d 只显示重复行
[dongshan@localhost my]$ uniq -cid my.txt
3 A
3 b
# -u 只显示唯一行
[dongshan@localhost my]$ uniq -ciu my.txt
1 C
1 E
# 计算用户数
[dongshan@localhost my]$ cat /etc/passwd | wc -l
44
# 计算单词数
[dongshan@localhost my]$ cat /usr/share/dict/words | wc -l
479828
#
[dongshan@localhost my]$ ls
my1.txt my2.txt my3.txt my.txt
# xargs 将前面的输入作为后面命令的参数
[dongshan@localhost my]$ ls | xargs rm -f
[dongshan@localhost my]$ ls
[dongshan@localhost my]$
# tee将标准输入分流给指定文件和标准输出
[root@localhost my]# cut -f1 -d ':' /etc/passwd | tee my.txt | sort -r | tee sort.txt | more
usbmuxd
unbound
tss
# -s 邮件标题 发送给dongshan用户
# hello ds,i am root 是邮件内容
# .表示结束
[root@localhost my]# mail -s "hello ds" dongshan
hello ds,i am root
.
EOT
# 从my.txt中输入文件内容
[root@localhost my]# mail -s "dongshan 你好" dongshan < my.txt
# 查看邮箱
[dongshan@localhost ~]$ ls -l /var/spool/mail/
total 4
-rw-rw----. 1 cat mail 0 Apr 2 23:36 cat
-rw-rw----. 1 dongshan mail 2252 Apr 5 02:32 dongshan
-rw-rw----. 1 rpc mail 0 Apr 1 21:52 rpc
# cat 查看指定邮箱内容
[dongshan@localhost ~]$ cat /var/spool/mail/dongshan
From root@localhost.localdomain Tue Apr 5 02:30:32 2022
Return-Path: .localdomain>
X-Original-To: dongshan
Delivered-To: dongshan@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
id 5FB834359872; Tue, 5 Apr 2022 02:30:32 +0800 (CST)
Date: Tue, 05 Apr 2022 02:30:32 +0800
To: dongshan@localhost.localdomain
Subject: hello dongshan
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Message-Id: <20220404183032.5FB834359872@localhost.localdomain>
...
# 利用管道 将标准输出作为邮件内容的标准输入
[dongshan@localhost my]$ ls -l| mail -s "hell root" root
# mail可以查看收到的邮件
[root@localhost my]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N 1 dongshan Tue Apr 5 02:42 21/777 "hell root"
># 输入邮件编号1,可以查看1邮件内容
& 1
Message 1:
From dongshan@localhost.localdomain Tue Apr 5 02:42:29 2022
Return-Path: .localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Date: Tue, 05 Apr 2022 02:42:29 +0800
To: root@localhost.localdomain
Subject: hell root
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: dongshan@localhost.localdomain (dongshan)
Status: R
total 8
-rw-r--r--. 1 root root 0 Apr 5 02:19 end.txt
-rw-r--r--. 1 root root 315 Apr 5 02:19 my.txt
-rw-r--r--. 1 root root 315 Apr 5 02:19 sort.txt
# 输入d删除刚才查看的邮件
& d
# 用户信息文件:隔开共7个字段.分别为用户名,x表示登录是否需要密码,用户uid,所属群组gid,用户注释信息,家目录,登录后第一个执行的进程
# 如果把第二个字段x删除,则用户登录就不需要输入密码
[root@localhost ~]# more /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...
# root用户可以访问
[root@localhost ~]# cat /etc/shadow
# 用户名:加密密码:最后一次修改时间:最小修改时间间隔:密码有效期:密码需要变更前的警告天数:密码过期后的宽限时间:账号失效时间:保留字段
# $6$开头表示设置过密码
root:$6$5RAVyN.Jv.ub0uW2$OxzZ.yvwMjdrW6Tr3O/KRXvKJAxpIZYqGPezvR9XIj81QVDobZcG3OCiHnUr726lLUAe6Y9grbi2TG6LrQ08U.::0:99999:7:::
# * 表示未设置过密码
bin:*:18222:0:99999:7:::
...
## !! 表示未设置过密码
tcpdump:!!:19083::::::
...
# 普通用户无权限访问
[dongshan@localhost ~]$ cat /etc/shadow
cat: /etc/shadow: Permission denied
# -S 查看用户密码设置状态
[root@localhost ~]# passwd -S cat
cat PS 2022-04-02 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@localhost ~]# useradd dog
[root@localhost ~]# passwd -S dog
dog LK 2022-04-05 0 99999 7 -1 (Password locked.)
# 解锁账号
[root@localhost ~]# passwd -uf dog
Unlocking password for user dog.
passwd: Success
# 空密码可以登录
[root@localhost ~]# passwd -S dog
dog NP 2022-04-05 0 99999 7 -1 (Empty password.)
# 重新锁定
[root@localhost ~]# passwd -lf dog
Locking password for user dog.
passwd: Success
[root@localhost ~]# passwd -S dog
dog LK 2022-04-05 0 99999 7 -1 (Password locked.)
# 群组名称:x需要密码登录:群组ID:群组成员
[root@localhost ~]# cat /etc/group
root:x:0:root
...
# 一般不为群组设置密码
[root@localhost ~]# cat /etc/gshadow
root:::
...
# 第1字符 d表示目录 -表示文件
# 第234字符 表示本用户权限 r读 w写 x执行 -没有权限
# 第567字符 表示本组权限
# 第8910字符 其他用户权限
[dongshan@localhost my]$ ls -l
total 0
drwxrwxr-x. 2 dongshan dongshan 6 Apr 5 20:41 my
-rw-rw-r--. 1 dongshan dongshan 0 Apr 5 20:41 my.txt
# 符号表示法设置文件权限
# ugoa 分别表示所属用户|所属群组|其他|全部
# +- 表示添加和删除
# rwx 表示读写执行
# 先查看权限
[lighthouse@VM-8-12-centos ~]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
# 给所属用户添加执行权限
[lighthouse@VM-8-12-centos ~]$ chmod u+x my.txt
[lighthouse@VM-8-12-centos ~]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rwxrw-r-- 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
# 给所属用户删除执行权限
[lighthouse@VM-8-12-centos ~]$ chmod u-x my.txt
[lighthouse@VM-8-12-centos ~]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
# 给所属用户所属组其他都添加执行权限
[lighthouse@VM-8-12-centos ~]$ chmod ugo+x my.txt
You have new mail in /var/spool/mail/root
[lighthouse@VM-8-12-centos ~]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rwxrwxr-x 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
# 给所属用户所属组其他都删除执行权限
[lighthouse@VM-8-12-centos ~]$ chmod ugo-x my.txt
[lighthouse@VM-8-12-centos ~]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 2 17:54 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 4 Apr 2 17:55 my.txt
# -R 参数可以递归给子目录中的文件或目录全部添加操作,该参数部分功能只有root有权限
[lighthouse@VM-8-12-centos ~]$ chmod -R a+x my
[lighthouse@VM-8-12-centos ~]$ ls -l my
total 4
-rwxrwxr-x 1 lighthouse lighthouse 0 Apr 7 01:19 my1.txt
-rwxrwxr-x 1 lighthouse lighthouse 4 Apr 7 01:19 my.txt
# 数字表示法设置权限
# 4 2 1 0 分别表示 r w x -
# 使用3组权限和来表示一个文件的权限
# 7=rwx 4=r--
[lighthouse@VM-8-12-centos my]$ chmod 774 my.txt
[lighthouse@VM-8-12-centos my]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 7 01:19 my1.txt
-rwxrwxr-- 1 lighthouse lighthouse 4 Apr 7 01:19 my.txt
# 6=rw- 4=r--
[lighthouse@VM-8-12-centos my]$ chmod 664 my.txt
[lighthouse@VM-8-12-centos my]$ ls -l
-rw-rw-r-- 1 lighthouse lighthouse 0 Apr 7 01:19 my1.txt
-rw-rw-r-- 1 lighthouse lighthouse 4 Apr 7 01:19 my.txt
# 从root切换不需要输入密码
[root@VM-12-4-centos ~]# su lighthouse
# 不会重新设置环境变量
[lighthouse@VM-12-4-centos root]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 非root账号切换需要输入密码
[lighthouse@VM-12-4-centos root]$ su fox
Password:
# 不会重新设置环境变量
[fox@VM-12-4-centos root]$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# su - 命令切换用户 使用-切换会进行重新登录
[root@VM-12-4-centos ~]# su - fox
Last login: Mon Apr 11 12:51:01 CST 2022 on pts/0
[fox@VM-12-4-centos ~]$ pwd
/home/fox
# 重新设置环境变量
[fox@VM-12-4-centos ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/fox/.local/bin:/home/fox/bin
[fox@VM-12-4-centos ~]$ last
root pts/1 106.122.242.201 Mon Apr 11 13:21 still logged in
root pts/0 106.122.242.201 Mon Apr 11 12:37 still logged in
root pts/2 218.66.73.183 Sun Apr 10 15:55 - 17:45 (01:49)
...
# 所属组
[fox@VM-12-4-centos ~]$ groups
fox
# id命令
[fox@VM-12-4-centos ~]$ id
uid=1001(fox) gid=1001(fox) groups=1001(fox)
# linux所有文件系统预设的默认权限是666 目录系统预设的默认权限是777
# 非root账号掩码是002
[fox@VM-12-4-centos ~]$ umask
0002
[fox@VM-12-4-centos ~]$ mkdir my
[fox@VM-12-4-centos ~]$ touch my.txt
# 777掩002=775 666掩002=664
[fox@VM-12-4-centos ~]$ ls -l
drwxrwxr-x 2 fox fox 4096 Apr 11 13:52 my
-rw-rw-r-- 1 fox fox 0 Apr 11 13:52 my.txt
# root账号掩码是022
[root@VM-12-4-centos ~]# umask
0022
[root@VM-12-4-centos ~]# mkdir my
[root@VM-12-4-centos ~]# touch my.txt
# 所以root账号创建的默认权限如下
# 777掩022=755 666掩022=644
[root@VM-12-4-centos ~]# ls -l
drwxr-xr-x 2 root root 4096 Apr 11 13:50 my
-rw-r--r-- 1 root root 0 Apr 11 13:50 my.txt
# 验证掩码的计算不是相减或异或的结果
[fox@VM-12-4-centos ~]$ umask 033
[fox@VM-12-4-centos ~]$ touch my1.txt
# 如下my1.txt的权限=666掩去033的结果
[fox@VM-12-4-centos ~]$ ls -l
drwxrwxr-x 2 fox fox 4096 Apr 11 13:52 my
-rw-r--r-- 1 fox fox 0 Apr 11 13:57 my1.txt
-rw-rw-r-- 1 fox fox 0 Apr 11 13:52 my.txt
# 如果想让后面创建的文件只有自己可读写即rw-------
[fox@VM-12-4-centos ~]$ umask 077
[fox@VM-12-4-centos ~]$ touche my3.txt
[fox@VM-12-4-centos ~]$ ls -l my3.txt
-rw------- 1 fox fox 0 Apr 11 14:02 my3.txt
[fox@VM-12-4-centos ~]$ ls -l my.txt
-rwxr-xr-- 1 fox fox 0 Apr 11 13:52 my.txt
# 第4组特殊权限使用原来ugo的x位置来表示 对应sst
# 使用u作为例子,当原来为x,添加s后用s表示
[fox@VM-12-4-centos ~]$ chmod u+s my.txt
[fox@VM-12-4-centos ~]$ ls -l my.txt
-rwsr-xr-- 1 fox fox 0 Apr 11 13:52 my.txt
# 使用g作为例子,当原来为-,添加s后用S表示
[fox@VM-12-4-centos ~]$ chmod g+s my.txt
[fox@VM-12-4-centos ~]$ ls -l my.txt
-rwsr-sr-- 1 fox fox 0 Apr 11 13:52 my.txt
# o位是用t表示,因为本来是-添加t后用T表示,如果是x则用t表示
[fox@VM-12-4-centos ~]$ chmod o+t my.txt
[fox@VM-12-4-centos ~]$ ls -l my.txt
-rwsr-sr-T 1 fox fox 0 Apr 11 13:52 my.txt
# 还可以用数字表示法修改sst对应421
# 3组权限(5754的后3位)754=rwxr-xr-- 第4组权限(5754的第一位)5=s-t 最后为rwsr-xr-T
[fox@VM-12-4-centos ~]$ chmod 5754 my.txt
[fox@VM-12-4-centos ~]$ ls -l my.txt
-rwsr-xr-T 1 fox fox 0 Apr 11 13:52 my.txt
# 特殊权限suid对可执行权限的作用:是以命令的所有者权限来运行,而不是以执行者的权限来运行
# 特殊权限对目录的作用:sticky只有文件所有者和root才可以删除改目录中的文件,sgid只要是同组目录就可以在该目录中创建文件。
# -i 选项列出第一列的inodes信息
[root@VM-12-4-centos ~]# ls -li
1054899 drwxr-xr-x 2 root root 4096 Apr 11 13:50 my
393884 -rw-r--r-- 1 root root 0 Apr 11 13:50 my.txt
指向另外一个文件的文件,指向其他文件的全路径,可以跨文件系统
# -文件 d目录 l符号连接
[root@VM-12-4-centos ~]# ls -l /bin/vi*
-rwxr-xr-x 1 root root 928200 Dec 16 2020 /bin/vi
lrwxrwxrwx 1 root root 2 Jan 8 2021 /bin/view -> vi
-rwxr-xr-x 1 root root 2337208 Dec 16 2020 /bin/vim
lrwxrwxrwx 1 root root 3 Jan 8 2021 /bin/vimdiff -> vim
# 创建软连接: ln -s 文件名 [连接名]
[root@VM-12-4-centos my]# ln -s my1.txt my1link.txt
[root@VM-12-4-centos my]# ls -l
lrwxrwxrwx 1 root root 7 Apr 11 21:27 my1link.txt -> my1.txt
-rw-r--r-- 1 root root 0 Apr 11 21:26 my1.txt
# 创建软连接路径不对则连接断开
[root@VM-12-4-centos your]# ln -s my/my2.txt your2.txt
[root@VM-12-4-centos your]# ls -l
lrwxrwxrwx 1 root root 10 Apr 11 21:34 your2.txt -> my/my2.txt
# 使用file命令 查看
[root@VM-12-4-centos your]# file your2.txt
your2.txt: broken symbolic link to `my/my2.txt
# 重新创建连接
[root@VM-12-4-centos your]# ln -sf ../my/my2.txt your2.txt
[root@VM-12-4-centos your]# ls -l
lrwxrwxrwx 1 root root 13 Apr 11 21:38 your2.txt -> ../my/my2.txt
# file命令检查是否成功
[root@VM-12-4-centos your]# file your2.txt
your2.txt: symbolic link to `../my/my2.txt'
# 写数据测试连接创建成功
[root@VM-12-4-centos your]# echo "heihei" >> your2.txt
[root@VM-12-4-centos your]# cat your2.txt
heihei
[root@VM-12-4-centos your]# cat ../my/my2.txt
heihei
是一个文件名与i节点之间的对应关系
# 创建硬连接 比创建软连接少了-s参数
[root@VM-12-4-centos my]# ln my3.txt my3link.txt
# inode是一样的 连接数为2
[root@VM-12-4-centos my]# ls -li
1054906 -rw-r--r-- 2 root root 0 Apr 11 21:26 my3link.txt
1054906 -rw-r--r-- 2 root root 0 Apr 11 21:26 my3.txt
# 测试写数据
[root@VM-12-4-centos my]# echo hehe >> my3.txt
[root@VM-12-4-centos my]# cat my3.txt
hehe
[root@VM-12-4-centos my]# cat my3link.txt
hehe
# 不能对目录创建硬连接
[root@VM-12-4-centos ~]# ls
my startredis.sh your
[root@VM-12-4-centos ~]# ln my mylink
ln: ‘my’: hard link not allowed for directory
# d表示目录 -表示文件
[root@VM-12-4-centos ~]# ls -l
drwxr-xr-x 2 root root 4096 Apr 12 13:39 my
-rw-r--r-- 1 root root 147 Dec 2 10:43 startredis.sh
# 符号连接
lrwxrwxrwx 1 root root 25 Dec 3 10:10 /dev/initctl -> /run/systemd/initctl/fifo
# 管道类型
[root@VM-12-4-centos ~]# ls -l /run/systemd/initctl/fifo
prw------- 1 root root 0 Dec 3 10:10 /run/systemd/initctl/fifo
# socket套接字
[root@VM-12-4-centos ~]# ls -l /dev/log
srw-rw-rw- 1 root root 0 Dec 3 10:10 /dev/log
# 字符特殊文件 字符设备如键盘鼠标
[root@VM-12-4-centos ~]# ls -l /dev/tty1
crw--w---- 1 root tty 4, 1 Dec 3 10:10 /dev/tty1
# 块特殊文件 块设备如硬盘
[root@VM-12-4-centos ~]# ls -l /dev/vda
brw-rw---- 1 root disk 253, 0 Dec 3 10:10 /dev/vda
# df命令以kb为单位列出每个文件系统中的空间使用情况
# 加上-h参数优化展示
[root@VM-12-4-centos ~]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 59G 8.5G 48G 15% /
# -i参数查看inode使用情况
[root@VM-12-4-centos ~]# df -i /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/vda1 3932160 116674 3815486 3% /
# du命令以KB为单位显示文件系统磁盘空间使用总量,并将递归的显示所有子目录
[root@VM-12-4-centos ~]# du -h ~
16K /root/my
8.0K /root/.cache/abrt
...
# -s汇总方式展示
[root@VM-12-4-centos ~]# du -sh ~
156K /root
# mount命令查看挂载的全部文件系统
[root@localhost dongshan]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
...
# 将光盘挂载到cdrom
[root@localhost dongshan]# mkdir /media/cdrom
[root@localhost dongshan]# mount /dev/sr0 /media/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
# 查看挂载成功
[root@localhost dongshan]# ls /media/cdrom/
addons EFI EULA GPL images isolinux LiveOS Packages RELEASE-NOTES-U9-en RELEASE-NOTES-U9-en.html repodata RPM-GPG-KEY RPM-GPG-KEY-oracle TRANS.TBL
# eject命令退出挂载 和umount功能一样
[root@localhost dongshan]# eject /media/cdrom
[root@localhost dongshan]# ls -l /media/cdrom/
total 0
[root@VM-12-4-centos my]# cat my1.txt
hehe
haha
[root@VM-12-4-centos my]# cat my2.txt
heihei
[root@VM-12-4-centos my]# cat my3.txt
hehe
# 将上面3个文件合并到my4.txt
[root@VM-12-4-centos my]# cat my?.txt > my4.txt
[root@VM-12-4-centos my]# cat my4.txt
hehe
haha
heihei
hehe
# 安装
[root@VM-12-4-centos my]# yum install unix2dos dos2unix
# unix格式
[root@VM-12-4-centos my]# cat -A my4.txt
hehe$
haha$
# 转dos
[root@VM-12-4-centos my]# unix2dos my4.txt
unix2dos: converting file my4.txt to DOS format ...
[root@VM-12-4-centos my]# cat -A my4.txt
hehe^M$
haha^M$
# 转unix
[root@VM-12-4-centos my]# dos2unix my4.txt
dos2unix: converting file my4.txt to Unix format ...
[root@VM-12-4-centos my]# cat -A my4.txt
hehe$
haha$
# 只展示不同 适用文件较大差异较小
[root@VM-12-4-centos my]# diff my1.txt my2.txt
2c2
< B
---
> b
4d3
< D
8a8
> I
# 展示全部 适用文件较小差异较大
[root@VM-12-4-centos my]# sdiff my1.txt my2.txt
A A
B | b
C C
D <
E E
F F
G G
H H
> I
# 列出所有looke开头的单词
[root@localhost ~]# look looke
Lookeba
looked
looked-for
lookee
...
# 输入tab
[root@localhost my]# cat > my.txt
a b c
# ^I表示tab
[root@localhost my]# cat -A my.txt
a^Ib^Ic$
# 转换
[root@localhost my]# expand my.txt > my1.txt
# 已转换成空格
[root@localhost my]# cat -A my1.txt
a b c$
# c=创建文件 v=详情 f=归档文件名
# 创建归档文件
[root@VM-12-4-centos ~]# tar cvf my.tar my
my/
my/my2.txt
my/my3.txt
my/my1.txt
[root@VM-12-4-centos ~]# ls -l
drwxr-xr-x 2 root root 4096 Apr 13 20:30 my
-rw-r--r-- 1 root root 10240 Apr 13 20:32 my.tar
# 查看归档文件内容 t=列出内容 v=详情
[root@VM-12-4-centos ~]# tar tvf my.tar
drwxr-xr-x root/root 0 2022-04-13 20:30 my/
-rw-r--r-- root/root 16 2022-04-13 01:28 my/my2.txt
-rw-r--r-- root/root 5 2022-04-13 20:30 my/my3.txt
-rw-r--r-- root/root 16 2022-04-13 01:27 my/my1.txt
[root@VM-12-4-centos ~]# ls
my.tar startredis.sh your
# x=抽取文件
[root@VM-12-4-centos ~]# tar xvf my.tar
my/
my/my2.txt
my/my3.txt
my/my1.txt
[root@VM-12-4-centos ~]# ls
my my.tar startredis.sh your
# gzip不能压缩目录
[root@VM-12-4-centos ~]# gzip -vc my
gzip: my is a directory -- ignored
# 不带参数的使用
[root@VM-12-4-centos my]# gzip my*
[root@VM-12-4-centos my]# ls
my1.txt.gz my2.txt.gz my3.txt.gz
[root@VM-12-4-centos my]# gunzip my*
[root@VM-12-4-centos my]# ls
my1.txt my2.txt my3.txt
# v=详情 c=保留原文件
[root@VM-12-4-centos ~]# tar cvf my.tar my
my/
my/my2.txt
my/default.conf
my/my3.txt
my/my1.txt
# z=使用gzip j=使用bzip2
[root@VM-12-4-centos ~]# tar zcvf my1.tar my
my/
my/my2.txt
my/default.conf
my/my3.txt
my/my1.txt
# 压缩后的文件较小
[root@VM-12-4-centos ~]# ls -l
drwxr-xr-x 2 root root 4096 Apr 13 20:51 my
-rw-r--r-- 1 root root 1286 Apr 13 20:56 my1.tar
-rw-r--r-- 1 root root 10240 Apr 13 20:55 my.tar
全称为 global regular expressions/print
# 正则
*=任意个字符
.=任意一个字符
[xyz]=xyz之一
[^xyz]=xyz外的
^=开头 $=结尾
# 列出包含root的行
[root@VM-12-4-centos etc]# grep root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
# -c 列出行数
[root@VM-12-4-centos etc]# grep -c root passwd
2
[root@VM-12-4-centos etc]# grep Root passwd
# -i 忽略大小写
[root@VM-12-4-centos etc]# grep -i Root passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
# -l 列出文件名
[root@VM-12-4-centos etc]# grep -li Root passwd hosts
passwd
# -v 列出没有匹配的行
[root@VM-12-4-centos etc]# grep -vi Root passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
# -w 匹配完整单词
[root@VM-12-4-centos etc]# grep -w bin passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
# bash结尾的行 -n 列出行号
[root@VM-12-4-centos etc]# grep -n bash$ passwd
1:root:x:0:0:root:/root:/bin/bash
26:lighthouse:x:1000:1000::/home/lighthouse:/bin/bash
27:fox:x:1001:1001::/home/fox:/bin/bash
是grep的扩展,等于grep -E ,即加上-E参数
'+' 表示匹配1个或多个前导字符
a|b 匹配a或者b
(RE) 匹配括号中的正则
# -w 完整单词
# 找出完全匹配80或443的行
[root@VM-12-4-centos my]# egrep -w '80|443' default.conf
listen 80;
listen [::]:80;
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#SSL 访问端口号为 443
listen 443 ssl;
# .表示任意一个字符 .+表示匹配任意多个字符
[root@VM-12-4-centos my]# egrep -w '80.+' default.conf
listen 80;
listen [::]:80;
# proxy_pass http://172.17.0.1:8090/backend;
# proxy_pass http://172.17.0.1:8090/app;
proxy_pass http://172.17.0.1:8090/backend;
proxy_pass http://172.17.0.1:8090/app;
# (RE)表示匹配括号中的正则
[root@VM-12-4-centos my]# egrep -w '(80.+)|(443)' default.conf
listen 80;
listen [::]:80;
# proxy_pass http://172.17.0.1:8090/backend;
# proxy_pass http://172.17.0.1:8090/app;
#SSL 访问端口号为 443
listen 443 ssl;
proxy_pass http://172.17.0.1:8090/backend;
proxy_pass http://172.17.0.1:8090/app;
# 与grep -E 功能一样
[root@VM-12-4-centos my]# grep -Ew '(80.+)|(443)' default.conf
listen 80;
listen [::]:80;
# proxy_pass http://172.17.0.1:8090/backend;
# proxy_pass http://172.17.0.1:8090/app;
#SSL 访问端口号为 443
listen 443 ssl;
proxy_pass http://172.17.0.1:8090/backend;
proxy_pass http://172.17.0.1:8090/app;
fixed-character grep,相比grep 更快,其模式不使用正则,等于grep -F
# 这里的.就是.
[root@VM-12-4-centos my]# fgrep '.' default.conf
#access_log /var/log/nginx/host.access.log main;
...
# 这里的.是匹配任意一个字符
[root@VM-12-4-centos my]# grep '.' default.conf
server {
listen 80;
listen [::]:80;
# 等于grep -F
[root@VM-12-4-centos my]# grep -F '.' default.conf
#access_log /var/log/nginx/host.access.log main;
...
#
[root@VM-12-4-centos my]# cat my1.txt
A a a a
B b b b
# 将每行的第一个空格替换为;
[root@VM-12-4-centos my]# cat my1.txt | sed -e 's/ /;/'
A;a a a
B;b b b
# g 标志将每行的全部空格替换为;
[root@VM-12-4-centos my]# cat my1.txt | sed -e 's/ /;/g'
A;a;a;a
B;b;b;b
# 不改变源文件
[root@VM-12-4-centos my]# cat my1.txt
A a a a
B b b b
# 可以使用两次s
[root@VM-12-4-centos my]# cat my1.txt | sed -e 's/ /;/g;s/;/-/g'
A-a-a-a
B-b-b-b
# d删除,删除第一行
[root@VM-12-4-centos my]# cat my1.txt | sed '1d'
B b b b
C c c c
D d d d
E e d d
# 删除1到3行
[root@VM-12-4-centos my]# cat my1.txt | sed '1,3d'
D d d d
E e d d
# 删除第一行到D所在的行
[root@VM-12-4-centos my]# cat my1.txt | sed '1,/D/d'
E e d d
# 写入一个空行
[root@VM-12-4-centos my]# cat my1.txt
A a a a
B b b b
C c c c
D d d d
E e d d
# /^$/d删除空行 并将结果写入my2.txt
[root@VM-12-4-centos my]# cat my1.txt | sed '/^$/d' > my2.txt
[root@VM-12-4-centos my]# cat my2.txt
A a a a
B b b b
C c c c
D d d d
E e d d
[root@VM-12-4-centos my]# cat my1.txt | head -2
A a a a
B b b b
# 1i 在第一行前面加入行
[root@VM-12-4-centos my]# sed -e 1i\hahahaha my1.txt | head -3
hahahaha
A a a a
B b b b
# 在第一行的后面加入行
[root@VM-12-4-centos my]# sed -e 1a\hahahaha my1.txt | head -3
A a a a
hahahaha
B b b b
# 将第一行替换为
[root@VM-12-4-centos my]# sed '1c hahahaha' my1.txt | head -2
hahahaha
B b b b
# -n 仅结果 1p 打印第一行
[root@VM-12-4-centos my]# sed -n '1p' my1.txt
A a a a
# 没加-n 会先打印第一行在打印全部
[root@VM-12-4-centos my]# sed '1p' my1.txt | head -2
A a a a
A a a a
# 将含有a或b的行打印
[root@VM-12-4-centos my]# sed -nr '/a|b/p' my1.txt
A a a a
B b b b
# -i 直接修改源文件内容
[root@VM-12-4-centos my]# sed -i 's/a/b/g' my1.txt
[root@VM-12-4-centos my]# cat my1.txt
A b b b
B b b b
# print 逐行打印
[root@VM-12-4-centos ~]# who | awk '{print}'
root pts/0 2022-04-14 23:25 (106.122.243.166)
# $1 打印第一个字段
[root@VM-12-4-centos ~]# who | awk '{print $1}'
root
#
[root@VM-12-4-centos ~]# who | awk '{print "login user is " $1}'
login user is root
# -F: 用:分割字段 最后删除1-25行
[root@VM-12-4-centos ~]# cat /etc/passwd | awk -F: '{print $1}' | sed '1,25d'
lighthouse
fox
# NR NF $NF $0的使用
[root@VM-12-4-centos ~]# awk -F: '{print "行号=" NR " 字段数=" NF " 最后一个字段=" $NF "行内容=" $0}' /etc/passwd | sed '1,25d'
行号=26 字段数=7 最后一个字段=/bin/bash行内容=lighthouse:x:1000:1000::/home/lighthouse:/bin/bash
行号=27 字段数=7 最后一个字段=/bin/bash行内容=fox:x:1001:1001::/home/fox:/bin/bash
# 打印文件大小 totalSize定义变量 END 表示最后执行
[root@VM-12-4-centos ~]# ls -l | awk '{totalSize+=$5} END {print totalSize}'
19865
# 将命令写到文件中作为脚本
# <
[root@VM-12-4-centos ~]# cat < totalSize
> {totalSize+=$5} END {print "totalSize is "totalSize" and file number is "NR}
> EOF
[root@VM-12-4-centos ~]# cat totalSize
{totalSize+=$5} END {print "totalSize is "totalSize" and file number is "NR}
[root@VM-12-4-centos ~]# ls -l | awk -f totalSize
totalSize is 19941 and file number is 7
# 将脚本写入bootSize文件
[root@VM-12-4-centos ~]# echo "ls -l | awk '{totalSize+=\$5} END {print totalSize}'" > bootSize
[root@VM-12-4-centos ~]# cat bootSize
ls -l | awk '{totalSize+=$5} END {print totalSize}'
# bash 执行
[root@VM-12-4-centos ~]# bash bootSize
19993
# if(length($1)==3) 判断第一个字段的字符长度==3
[root@VM-12-4-centos ~]# cat /etc/passwd | awk -F: '{if(length($1)==3)print$1}' | head -2
bin
adm
# for语句使用
[root@VM-12-4-centos ~]# cat /etc/passwd | awk -F: '{arr[length($1)]++}END{for(i=0;i<9;i++) print "length "i" user number is " arr[i]}'
length 0 user number is
length 1 user number is
length 2 user number is 1
length 3 user number is 6
length 4 user number is 7
length 5 user number is 1
length 6 user number is 4
length 7 user number is 3
length 8 user number is 2
# 恢复之前奔溃未保存的文件
vi -r
# 只读方式打开文件
vi -R
# 操作模式包括: 命令行模式 编辑模式 扩展模式
# 使用箭头上下左右
# $移动到行尾
# ^和0移动到行头
# nG 移动到第n行
# G 移动到文件末尾
# gg 移动到文件头
# ctrl+d 向前移动半个屏幕
# ctrl+u 向后移动半个屏幕
# H 光标移动到屏幕第一行
# M 光标移动到屏幕中间行
# L 光标移动到屏幕最后行
# z+Enter 光标所在行作为屏幕第一行(滚动屏幕)
# 修改操作 cc cl cw 修改当前行 修改当前字母 修改当前单词
# 删除操作 dd dl dw
# 复制操作 yy yl yw
# c( c) c{ c} 向前修改句子 向后修改句子 向前修改段落 向后修改段落
# d( d) d{ d}
# y( y) y{ y}
# nc nd ny n表示数字 向后修改N行 删除N行 复制N行
# p 粘贴到当前行之后 P粘贴到当前行之前
# u 复原 ctrl+r 重做 U 复原当前行
# v 光标所在字符进入可视模式 V整行进入可视模式 ,进入可视模式移动光标进行选择,可以对选择的进行修改删除复制等操作 如下图所示。
# 搜索 /向前搜索 ?向后搜索 n下一个 N上一个
# ~ 转换当前光标字符的大小写
# x 删除当前光标的字符 nx 删除N个字符
# J 合并下一行到当前行
# R 替换当前字符
# .重复之前的操作
#扩展模式 在命令模式下按:即可进入扩展模式
# w 写入 q 退出 !强制
# 过滤功能
# 命令模式按!! 最后面会展示:.! 输入命令 命令的执行结果会输入到当前文件中
:.!cat /etc/passwd
# 返回命令模式 按!} .,$表示将当前光标行到最后行的内容作为命令的输入
# 执行如下命令后(按第一个字段排序)再将结果输回文件
:.,$!sort -nr -t: -k1
# 当前会话有效
# set nu 显示行号
# set nonu 隐藏行号
# set list 显示不可见字符
# set nolist 隐藏不可见字符
# 如果要设置全局会话有效,则需要在家目录下创建.exrc或.vimrc文件,输入set命令,这样每次打开时就展示行号和不可见字符
[root@VM-12-4-centos ~]# ls -la | grep .vimrc
-rw-r--r-- 1 root root 16 Apr 17 15:03 .vimrc
[root@VM-12-4-centos ~]# cat .vimrc
set nu
set list
[root@VM-12-4-centos my]# vi default.conf
1 server {$
2 listen 80;$
3 listen [::]:80;$
# 格式:[范围]s/要替换的字符/替换的字符/[g全局]
# 搜寻范围
# 无 表示当前行
# n1,n2 从n1行到n2行
# 1,$ 1,% 整个文件
# .,.+n 当前行到当前加N行
# .,.-n 当前行到当前减N行
# 表示将当前行的第一个a替换为A
:s/a/A/
# 将当前行的所有a替换为A
:s/a/A/g
# :r dog 读取dog文件的内容到当前
# :n1,n2w cat 将当前n1到n2行 写入cat
# :n1,n2w >>cat 将当前n1到n2行 写入cat尾部
# 多个文件同时vi
[root@VM-12-4-centos my]# vi my1.txt my2.txt my5.txt
# :n 下一个文件
# :rew 回到第一个文件
# :n# 上一次编辑的文件
# 查看所有变量 包括环境变量和局部变量
[root@VM-12-4-centos my]# set | more
ABRT_DEBUG_LOG=/dev/null
BASH=/bin/bash
...
# 显示环境变量
[root@VM-12-4-centos my]# env | more
XDG_SESSION_ID=462901
HOSTNAME=VM-12-4-centos
TERM=vt100
SHELL=/bin/bash
...
# 设置局部变量 局部变量只在当前的会话中有效,子shell无效
[root@VM-12-4-centos my]# DOG1=HAHA
[root@VM-12-4-centos my]# DOG2=HEHE
[root@VM-12-4-centos my]# set | grep DOG
DOG1=HAHA
DOG2=HEHE
[root@VM-12-4-centos my]# echo DOG1
DOG1
# 设置环境变量 环境变量在当前会话的子shell中也有效
[root@VM-12-4-centos ~]# export DOG=HAHA
[root@VM-12-4-centos ~]# env | grep DOG
DOG=HAHA
# 取消环境变量
[root@VM-12-4-centos ~]# unset DOG
# 如果一个变量在set中有env中没有,那么这个就是局部变量
[root@VM-12-4-centos my]# env | grep DOG
[root@VM-12-4-centos my]#
# 终端窗口高度 宽度 命令历史记录长度
[root@VM-12-4-centos my]# set | grep LINES
LINES=7
[root@VM-12-4-centos my]# set | grep COLUMNS
COLUMNS=80
[root@VM-12-4-centos my]# set | grep HISTFI
HISTFILE=/root/.bash_history
HISTFILESIZE=3000
# 用来设置提示符所显示的信息
[root@VM-12-4-centos ~]# echo $PS1
[\u@\h \W]\$
# \u 当前用户名
# \h 主机名
# \W 当前的工作目录
# \d 日期
# \t 时间
# \! 当前命令的历史编号
# \$ 普通用户显示$ root用户显示#
# \l 显示终端设备的基本名
# 设置所有
[root@VM-12-4-centos ~]# PS1='[\u@\h \w TTY\l \d \t \!]\$'
[root@VM-12-4-centos ~ TTY2 Mon Apr 18 01:02:41 2186]#
# 显示系统中的所有别名
[root@VM-12-4-centos ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
# alias 别名=命令字符
[root@VM-12-4-centos ~]# alias dir='ls -laF'
[root@VM-12-4-centos ~]# alias dir
alias dir='ls -laF'
[root@VM-12-4-centos ~]# dir
total 164
dr-xr-x---. 9 root root 4096 Apr 18 01:13 ./
dr-xr-xr-x. 19 root root 4096 Apr 18 01:15 ../
-rw------- 1 root root 59960 Apr 18 01:15 .bash_history
...
# 取消别名 unalias 别名
[root@VM-12-4-centos ~]# unalias dir
[root@VM-12-4-centos ~]# alias dir
-bash: alias: dir: not found
# 多个命令的别名中间用;隔开
[root@VM-12-4-centos ~]# alias info='whoami;hostname;date'
[root@VM-12-4-centos ~]# info
root
VM-12-4-centos
Mon Apr 18 01:20:23 CST 2022
# 别名只在当前会话有效,如果要永久保存,需要在家目录下的.bashrc文件中添加
[root@VM-12-4-centos my]# set -o | more
allexport off
braceexpand on
emacs on
...
[root@VM-12-4-centos my]# set -o | egrep 'emacs|vi'
emacs on
privileged off
vi off
[root@VM-12-4-centos my]# set -o vi
[root@VM-12-4-centos my]# set -o | egrep 'emacs|vi'
emacs off
privileged off
vi on
# shell启动脚本:
# 登录shell:用户登录后使用的shell 或者使用su - 切换登录
# 启动脚本顺序:
# 执行/etc/profile -> /etc/profile.d目录下的所有启动脚本
# 执行~/.bash_profile -> ~/.bashrc -> /etc/bashrc
# 非登录shell: 打开的新shell 或者使用su
# 启动脚本顺序:不执行/etc/profile和~/.bash_profile
# 执行~/.bashrc -> /etc/bashrc
# 执行/etc/profile.d目录下的所有启动脚本
# 其他的一些系统文件
# .bash_history 历史命令
# .bash_logout 退出登录时可以执行的操作
# 计算机的BIOS(Basic Input/Ouput System基本输入输出系统)执行POST
# BIOS为引导加载程序bootloader读取MBR(Master Boot Record主引导记录)
# GRUB bootloader加载Linux内核
# Linux 内核初始化和配置硬件
# 系统运行级别 run level
# 0=关机 1=单用户 2=多用户没启动网络 3=多用户启动网络文字页面 4=用户自定义默认=3 5=3但是是图形化页面 6=重启
# 查看level
[root@localhost rc5.d]# runlevel
N 5
[root@localhost rc5.d]# who -r
run-level 5 2022-04-22 21:46
# 设置level init (0,6)
[root@localhost rc5.d]# init 3
[root@localhost rc5.d]# init 5
# 查看运行级别
[root@localhost rc5.d]# runlevel
3 5
# 查看系统启动级别
[root@localhost etc]# systemctl get-default
graphical.target
# 设置系统启动级别 graphical.target|multi-user.target
[root@localhost etc]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
# init执行/etc/rc.d目录中的rc.sysinit程序初始化系统
[root@localhost rc.d]# ll
drwxr-xr-x. 2 root root 83 Apr 2 01:17 init.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc0.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc1.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc2.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc3.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc4.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc5.d
drwxr-xr-x. 2 root root 61 Nov 11 2020 rc6.d
# -t tcp -u udp -n 不解析名字 -l 监听端口 -p 显示程序
[root@VM-12-4-centos etc]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1384/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1497/master
# ntsysv --level 35 没有加--level 则设置当前运行级别的启动服务
[root@VM-12-4-centos init.d]# ntsysv
│
What services should be automatically started? │
│
↑ │
[ ] netconsole ▮ │
[*] network ▒ │
[ ] opensmd ▒ │
▒ │
[ ] NetworkManager-dispatcher.service ▒ │
[ ] NetworkManager-wait-online.service▒ │
[ ] NetworkManager.service ▒ │
[ ] abrt-ccpp.service ▒ │
[ ] abrt-oops.service ▒ │
[ ] abrt-pstoreoops.service ▒ │
[ ] abrt-vmcore.service ▒ │
[ ] abrt-xorg.service ▒ │
[ ] abrtd.service ▒ │
[*] acpid.service ▒ │
[ ] arp-ethers.service ▒ │
[*] atd.service ▒ │
[*] auditd.service ▒ │
[ ] blk-availability.service ▒ │
[ ] chrony-wait.service ▒ │
[ ] chronyd.service ▒ │
[*] cloud-config.service ▒ │
[*] cloud-final.service ▒ │
[*] cloud-init-local.service ▒ │
[*] cloud-init.service ▒ │
[ ] console-getty.service ▒ │
[ ] console-shell.service ↓ │
│
┌────┐ ┌────────┐ │
│ Ok │ │ Cancel │ │
└────┘ └────────┘ │
│
│ │
└────────────────────────────────────────────────┘
#
[root@VM-12-4-centos ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
opensmd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# 开启服务
[root@VM-12-4-centos ~]# chkconfig network --level 1 on
[root@VM-12-4-centos ~]# chkconfig --list
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:on 2:on 3:on 4:on 5:on 6:off
opensmd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# 关闭服务
[root@VM-12-4-centos ~]# chkconfig network --level 01 off
# 查看服务状态
[root@VM-12-4-centos ~]# service sshd status
Redirecting to /bin/systemctl status sshd.service
● sshd.service - OpenSSH server daemon
...
[root@VM-12-4-centos ~]# systemctl status sshd
● sshd.service - OpenSSH server daemon
...
# 停止服务
service sshd stop
# 启动服务
service sshd start
# 重启服务
service sshd restart
# shutdow -h now 关机参数较多
# halt 挂起
# poweroff 关闭电源
# init 0 推荐这个比较高大上
# -k 参数不真正关闭 可以用来发送消息给其他终端用户
[root@localhost etc]# shutdown -k 23:55 "系统将在23:55关闭"
Shutdown scheduled for Fri 2022-04-22 23:55:00 CST, use 'shutdown -c' to cancel.
# -c 取消关机
[root@localhost etc]# shutdown -c
# 其他终端可以看到如下信息
[dongshan@localhost ~]$
Broadcast message from root@localhost.localdomain (Fri 2022-04-22 23:25:00 CST):
系统将在23:55关闭
The system is going down for power-off at Fri 2022-04-22 23:55:00 CST!
Broadcast message from root@localhost.localdomain (Fri 2022-04-22 23:26:08 CST):
The system shutdown has been cancelled at Fri 2022-04-22 23:27:08 CST!
# shutdown -r now 重启
# reboot 重启
# init 6 重启
# 第一个进程
[root@VM-8-12-centos system]# ps -p 1
PID TTY TIME CMD
1 ? 00:04:15 systemd
# 查看初始内存文件系统内容
[root@VM-8-12-centos system]# lsinitrd | more
# /usr/lib/systemd/system 安装rpm软件包发行
# /run/systemd/system 运行期间创建
# /etc/systemd/system 系统管理员创建
# 列出所有安装的单元 服务.service 目标.target 设备.device 加载点.amount 套接字.socket
[root@VM-8-12-centos ~]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
# 列出所有服务单元
[root@VM-8-12-centos ~]# systemctl list-units --type service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
acpid.service loaded active running ACPI Event Daemon
● apt-daily.service not-found inactive dead apt-daily.service
atd.service loaded active running Job spooling tools
# 查看runlevel
[root@VM-8-12-centos ~]# find / -name "runlevel?.target"
/usr/lib/systemd/system/runlevel1.target
/usr/lib/systemd/system/runlevel6.target
/usr/lib/systemd/system/runlevel2.target
/usr/lib/systemd/system/runlevel3.target
/usr/lib/systemd/system/runlevel5.target
/usr/lib/systemd/system/runlevel0.target
/usr/lib/systemd/system/runlevel4.target
[root@VM-8-12-centos ~]# cd /usr/lib/systemd/system
You have new mail in /var/spool/mail/root
[root@VM-8-12-centos system]# ls -l runlevel?.target
lrwxrwxrwx 1 root root 15 Jan 8 2021 runlevel0.target -> poweroff.target
lrwxrwxrwx 1 root root 13 Jan 8 2021 runlevel1.target -> rescue.target
lrwxrwxrwx 1 root root 17 Jan 8 2021 runlevel2.target -> multi-user.target
lrwxrwxrwx 1 root root 17 Jan 8 2021 runlevel3.target -> multi-user.target
lrwxrwxrwx 1 root root 17 Jan 8 2021 runlevel4.target -> multi-user.target
lrwxrwxrwx 1 root root 16 Jan 8 2021 runlevel5.target -> graphical.target
lrwxrwxrwx 1 root root 13 Jan 8 2021 runlevel6.target -> reboot.target
# 获取当前运行级别
[root@VM-8-12-centos system]# runlevel
N 3
[root@VM-8-12-centos system]# systemctl get-default
multi-user.target
# 默认目标单元
[root@VM-8-12-centos system]# ll /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37 Mar 7 2019 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
# 设置默认目标单元
[root@VM-8-12-centos system]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
# 激活当前系统的目标单元
systemctl isolate multi-user.target