蓝色 目录
白色 文件
绿色 可执行文件(脚本,命令程序文件)-->类似于windows的桌面的快捷方式
红色 可压缩文件
黄色 设备文件
红色闪动 ----> 表示连接文件不可用
ls 常用选项:
-l 以长格式显示目录下的内容及其详细属性
-h 人性化显示目录下内容大小
-d 仅显示目录本身而不显示目录下的内容
$:打头的表示这不是在root用户(管理员用户)下执行的命令。
#:打头的表示这是在root用户下执行的命令。要使$变为#,可以使用su命令切换到root用户
date "+%Y/%m/%d %H:%M:%S" 注意双引号
date +%j 该年已过多少天
cal命令可以显示日历,默认显示当前月的日历。加上参数可以显示其他月份的日历。
timedatectl status
timedatectl set-ntp no
mkdir -p 递归创建多个目录
directory 文件
[root@localhost ~]# cd /kkk/text1/text2/text3
[root@localhost text3]# cd ..
[root@localhost text2]# cd ..
[root@localhost text1]# cd ..
[root@localhost kkk]# cd ..
[root@localhost /]# cd ..
.. 返回到上一级
rm
-f 强制删除
-r 删除目录
which:Linux系统中用于查找文件路径
链接文件:快捷方式
mv命令在Linux系统中被用于移动文件或改变文件的名称。以下是mv命令的几种常用用法:
--文件改名:
mv oldname.txt newname.txt
这条命令会将名为oldname.txt的文件改名为newname.txt。
------------------------------------------------------------------------------------
[root@localhost ~]# cd /opt
[root@localhost opt]# touch hello_soft
[root@localhost opt]# ls
hello_soft
[root@localhost opt]# cd /
[root@localhost /]# ls /media
hello_soft
[root@localhost /]# ls -l /media/hello_soft
lrwxrwxrwx. 1 root root 10 10月 17 19:40 /media/hello_soft -> hello_soft(闪动的红色)
-- 注意:创建链接时一定要写目录或文件的绝对路径
[root@localhost /]# ls /media
[root@localhost /]# ln -s /opt/hello_soft /media/
[root@localhost /]# ls /media
hello_soft (浅蓝色 表示可用的链接文件)
[root@localhost /]#
[root@localhost opt]# ll hello_soft
-rw-r--r--. 1 root root 0 10月 17 19:40 hello_soft
[root@localhost opt]# ls -l /media/hello_soft
lrwxrwxrwx. 1 root root 15 10月 17 19:43 /media/hello_soft -> /opt/hello_soft (参考源文件)
-- 可以看到,链接文件的权限比源文件的大
--软连接特点1:链接文件和源文件同步
[root@localhost opt]# cp /etc/fstab hello_soft
cp:是否覆盖'hello_soft'? y
[root@localhost opt]# cat /media/hello_soft
--软连接特点2:源文件被删除,链接文件不可用
链接文件被删除,源文件没有影响
rm -rf hello_soft
[root@localhost opt]# ls -l /media
总用量 0
lrwxrwxrwx. 1 root root 15 10月 17 19:43 hello_soft -> /opt/hello_soft
[root@localhost opt]# ls /media
hello_soft
[root@localhost opt]# ls -l /media/hello_soft
lrwxrwxrwx. 1 root root 15 10月 17 19:43 /media/hello_soft -> /opt/hello_soft
[root@localhost opt]#
-- 软链接的特点3:可以跨分区、
[root@localhost opt]# ln -s /opt/hello_soft /boot
软链接: ln -s 硬链接:ln
------------------------------------------------------------
硬链接特点1:不可以跨分区
[root@localhost opt]# ln /opt/hello_hard /boot
ln: 无法创建硬链接 '/boot/hello_hard' => '/opt/hello_hard': 无效的跨设备链接
硬链接特点2:源文件被删除,链接文件依然可用
[root@localhost opt]# ln /opt/hello_hard /media
[root@localhost opt]# ls /media
hello_hard hello_soft test 硬链接文件颜色跟普通文件颜色一样(硬链接文件的颜色不是浅蓝色)
[root@localhost opt]# rm -rf hello_hard
[root@localhost opt]# ls
1a 1b 1c 2a 2b 2c 3a 3b 3c hello_soft test
[root@localhost opt]# ls /media
hello_hard hello_soft test
[root@localhost opt]# cat /media/hello_hard
Linux 的帮助命令主要包含三个:
1.help 命令与 --help 参数 help命令只能用于内建命令
2.man 命令 想比help,man 命令的信息更全
3.info 命令
查看/etc/passwd文件第一行内容
[root@localhost /]# head -1 /etc/passwd
或
[root@localhost /]# head -n 1 /etc/passwd
查看内核
[root@localhost /]# uname
Linux
[root@localhost /]# uname -r
5.14.0-162.6.1.el9_1.x86_64
[root@localhost /]# uname -sr
Linux 5.14.0-162.6.1.el9_1.x86_64
查看cpu
[root@localhost /]# cat /proc/cpuinfo
[root@localhost ~]# cat -n /etc/passwd | head -10 | tail -5
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
| 管道符
> 覆盖
>> 追加
2> 只收集错误的输出结果 (覆盖)
2>> 只收集错误的输出结果 (追加)
&> 错误正确的都收集 (覆盖)
&>> 错误正确的都收集(追加)
[root@localhost opt]# cat -n /etc/passwd | head -n 10 | tail -n 5
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost opt]# cat test2.txt
nihaowoshiwenjiatest.txt
[root@localhost opt]# cat -n /etc/passwd | head -n 10 | tail -n 5 >> test2.txt 实现内容的追加
[root@localhost opt]# cat test2.txt
nihaowoshiwenjiatest.txt
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost opt]# cat test3.txt
cat: /opt/oocc: 没有那个文件或目录
[root@localhost opt]# cat /opt/oocc 2> /opt/test3.txt
[root@localhost opt]# cat test3.txt
cat: /opt/oocc: 没有那个文件或目录
[root@localhost opt]# ping www.kkk.com
ping: www.kkk.com: 未知的名称或服务
[root@localhost opt]# ping www.kkk.com 2>> test3.txt
[root@localhost opt]# cat test3.txt
cat: /opt/oocc: 没有那个文件或目录
ping: www.kkk.com: 未知的名称或服务
遇到END结束
[root@localhost ~]# cat > /opt/111.txt << END
> nihao
> haode
> enen
> xixi
> END
[root@localhost ~]# cat /opt/111.txt
nihao
haode
enen
xixi
: set nu ---显示行号
: set nonu ---取消行号显示
: %s/所需修改的内容/想要修改的内容
stat命令是Linux系统中用于显示文件或文件系统详细信息的工具。它提供了比ls命令更为详尽的数据,包括文件大小、iNode节点、块数量、访问权限、访问时间、修改时间等。这个命令对于分析文件属性非常有用。
-type 类型
f 文件
d 目录
l 链接文件
[root@localhost ~]# find /var/log -type f
- (not)name “文件名”
[root@localhost ~]# find /var/log -name "*.log
- iname 按文件名查找忽略大小写
-size
[root@localhost ~]# find /var/log -size -5k
寻找比5k小的文件
--------------------------------------------
grep和find
grep ---> 文件的内容(文件内部)
find ---> 文件本身(文件外部)
find和ls
find 会使用递归,ls不会。find所呈现的比ls的多
ls /etc/*.conf ls只在etc这一层找,其下面的目录不找
-----------------------------------------------
- mtime 按日期查找 +多少天之前 -多少天之内
0 24小时之内
-a 两个条件同时满足
-o 两个条件满足一个
-user 用户名
[root@localhost ~]# find /opt -mtime -3 -type f -exec cp {} /var \;
-- 将在/opt及其下面的目录中查找在三天以内创建的文件复制到/var目录中
[root@localhost ~]# ls /var
adm db games local mail preserve test2.txt tmp
cache empty kerberos lock nis run test3.txt yp
crash ftp lib log opt spool test.txt
-exec 可接额外的命令来处理查找到的结果
{} 将find查找到的内容放置{}中
\; 代表额外处理命令结束
使用 head 和 tail 命令的组合:
head -n 23 文件名 | tail -n 1
head -n 23 会输出文件的前 23 行,然后通过管道 | 将这些行传递给 tail -n 1,后者会输出最后 1 行,也就是第 23 行。
压缩--
gzip bzip2 xz ,其中xz压缩效果最好
[root@localhost ~]# cp /etc/services /opt
[root@localhost ~]# ls /opt
services
[root@localhost ~]# cd /opt
[root@localhost opt]# du -h services
680K services
压缩后:
root@localhost opt]# gzip services
[root@localhost opt]# du -h services.gz
140K services.gz
查看压缩文件内容:
zcat services.gz
解压缩 -d
[root@localhost opt]# gzip -d services.gz
gzip bzip2 xz不支持对目录进行压缩
--->tar
先打包再压缩
[root@localhost ~]# tar -cf log.tar /var/log
tar: 从成员名中删除开头的“/”
[root@localhost ~]# ls
anaconda-ks.cfg files hello_soft helloworld.c log.tar
[root@localhost ~]# du -h log.tar
12M log.tar
[root@localhost ~]# gzip log.tar
[root@localhost ~]# du -h log.tar.gz
980K log.tar.gz
一步到位
[root@localhost ~]# tar -czf log.tar.gz /var/log 注意参数的顺序
列出打包文档内容
[root@localhost ~]# tar -tf log.tar.gz
解压
tar -xf log.tar.gz
解压
tar -xf log.tar.gz -C /opt -C解压到指定目录
[root@localhost ~]# tar -xf log.tar.gz -C /opt
tar: var/log/rhsm/rhsmcertd.log-20251011:时间戳 2025-10-11 01:35:30 是未来的 30518691.697754056 秒之后
tar: var/log/messages-20251011:时间戳 2025-10-11 01:35:30 是未来的 30518691.601237282 秒之后
tar: var/log/maillog-20241022:时间戳 2025-10-11 01:35:30 是未来的 30518691.59952303 秒之后
tar: var/log/vmware-network.9.log:时间戳 2025-10-11 01:47:38 是未来的 30519419.581619879 秒之后
tar: var/log/spooler-20241022:时间戳 2025-10-11 01:35:30 是未来的 30518691.577483277 秒之后
[root@localhost ~]# ls /opt
tmp var
解释:
tar: 从成员名中删除开头的“/”
var/log/
var/log/README
var/log/tallylog
var/log/private/
var/log/wtmp
可以看出var前面没有/
查看别名 alias
删除别名unalias
[root@localhost ~]# 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 xzegrep='xzegrep --color=auto'
alias xzfgrep='xzfgrep --color=auto'
alias xzgrep='xzgrep --color=auto'
alias zegrep='zegrep --color=auto'
alias zfgrep='zfgrep --color=auto'
alias zgrep='zgrep --color=auto'
[root@localhost ~]# alias ping='ping -c 4'
/etc/passwd 用户信息文件
UID:0 超级用户
UID: 1-999 系统伪用户,不能登录系统
UID: 1000-65535 普通用户,管理员创建的用户
账户和组的管理
adduser 用户名字 id 用户名 查看用户的UID GID 组
-u 指定用户UID
-d 指定用户家目录 (一般不修改,默认/home)
-c 用户描述信息
-g 指定用户基本组 (了解)
-s 修改用户shell /sbin/nologin /bin/bash
-l 改名
usermod 修改用户
usermod -c 我是user1 user1
[root@localhost ~]# ls /home
ldp user1 user2
[root@localhost ~]# user -r user2
-r 可以删除用户及其家目录
/etc/group 组信息文件
/etc/gshadow 组密码文件
groupmod 修改组属性
-g GID 修改组的GID
-n 新名字 新组名
gpasswd [-选项] 用户名 组名
-a 将用户添加到工作组
-d 将用户从工作组中删除
--改组名
[root@localhost ~]# groupmod -n user1g user1
--将user1用户放入user1g组中
[root@localhost ~]# gpasswd -a user1 user1g
正在将用户“user1”加入到“user1g”组中
chmod:change mode 设置用户对文件的权限
r--4 w--2 x--1
u g o
+ -
对于超级用户,权限对他的几乎没有影响 可以在命令模式 :wq! 修改并保存
但是对于普通用户具有权限约束
chown: 归属关系管理
chown 所有者:所属组 文档 同时修改所有者和所属组的身份
chown 所有者 只修改所有者
chown :所属组 只修改所属组
[root@localhost ~]# groupadd yunwei
[root@localhost ~]# useradd user2
正在创建信箱文件: 文件已存在
[root@localhost ~]# gpasswd -a user2 yunwei
正在将用户“user2”加入到“yunwei”组中
[root@localhost ~]#cat /etc/passwd | tail -1
user2:x:1003:1004::/home/user2:/bin/bash
[root@localhost ~]# mkdir /opt/test
[root@localhost ~]# touch test1.txt
[root@localhost ~]# chown root:yunwei test1.txt
[root@localhost ~]# ll test1.txt
-rw-r--r--. 1 root yunwei 0 10月 25 20:47 test1.txt
[root@localhost ~]# chmod u+x test1.txt
[root@localhost ~]# ll test1.txt
-rwxr--r--. 1 root yunwei 0 10月 25 20:47 test1.txt
[root@localhost ~]# chmod u-x test1.txt
[root@localhost ~]# ll test1.txt
-rw-r--r--. 1 root yunwei 0 10月 25 20:47 test1.txt
[root@localhost ~]# chmod g+x test1.txt 可以注意到,test.txt变成了绿色(可执行文件)
[root@localhost ~]# chmod g-x test1.txt
[root@localhost ~]# ll test1.txt
-rw-r-xr--. 1 root yunwei 0 10月 25 20:47 test1.txt
(user2用户的密码是 liudepustc)
who
whoami命令来自于英文词组”Who am i“的拼写,中文译为”我是谁“,其功能是用于显示当前用户名。whoami命令执行后会输出当前登录的用户身份名称,相当于执行了”id -un“命令。
总结:用户对文件拥有rwx权限(针对的是文件的内容)
r : 查看文件内容
w : 对文件内容拥有增删改权限,并不能删除文件
[root@localhost test2]# ll -d /test2
drwxr-xr-x. 2 root root 19 10月 27 10:16 /test2
我们要求 root 是 /project 目录的属主,权限是 rwx;tgroup 是此目录的属组,tgroup 组中拥有班级学员 zhangsan 和 lisi,权限是 rwx;其他人的权限是 0。这时,试听学员 st 来了,她的权限是 r-x。。
[root@localhost ~]# useradd zhangsan
[root@localhost ~]# useradd lisi
[root@localhost ~]# useradd st
[root@localhost ~]# groupadd tgroup
#添加需要试验的用户和用户组,省略设定密码的过程 echo"1234567" passwd | --stdin st
[root@localhost ~]# mkdir /project #建立需要分配权限的目录
[root@localhost ~]# chown root:tgroup /project/
#改变/project目录的属主和属组
[root@localhost ~]# chmod 770 /project/
#指定/project目录的权限
[root@localhost ~]# ll -d /project/
drwxrwx--- 2 root tgroup 6 10月 27 11:35 /project/
#查看一下权限,已经符合要求了
#这时st学员来试听了,如何给她分配权限r-x
[root@localhost ~]# setfacl -m u(用户名):st:rx /project/
#给用户st赋予r-x权限,使用"u:用户名:权限" 格式
[root@localhost /]# cd /
[root@localhost /]# ll -d project/
drwxrwx---+ 3 root tgroup 6 10月 27 11:35 project/
#使用ls-l査询时会发现,在权限位后面多了一个"+",表示此目录拥有ACL权限
[root@localhost /]# getfacl project
#查看/prpject目录的ACL权限
# file: project
# owner: root
# group: tgroup
user::rwx user::rwx <-用户名栏是空的,说明是属主的权限
user:st:r-x
group::rwx
mask::rwx
other::---
st 用户既不是 /prpject 目录的属主、属组,也不是其他人,我们单独给 st 用户分配了 r-x 权限.
---------------------------------
find /var/log -name '*.log' -size +10M cp /dev/null {} \;
所查的文件被覆盖
---------------------------------
---------------------------------------------------------
CPU 内存 硬盘
硬盘是一种磁盘
查看系统中有多少硬盘,每个硬盘的分区数量以及分区的具体信息 lsblk
查看具体分区使用情况 df -h
统计磁盘下目录或文件大小 du -h
du -hs 总计总数只统计每个参数的总数
1B(字节)=8b(位)
1 KB = 1024 B
1 MB = 1024 KB
1 GB = 1024 MB
1TB = 1024GB