IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> 第六章、Linux文件与目录管理 -> 正文阅读

[系统运维]第六章、Linux文件与目录管理

6.1 目录与路径
6.1.1 相对路径与绝对路径

  1. 绝对路径:【cd /urs/bin】
  2. 相对路径:【cd …/bin】

6.1.2目录的相关操作

.  代表此层目录
..  代表代表上一层目录
.  代表前一个工作目录
~  代表家目录
~account 代表account的家目录

注意:在所有的目录下都存在【.】【…】
在这里插入图片描述

  1. cd : 变换目录
  2. pwd : 显示当前目录
  3. mkdir : 建立一个新目录
  4. rmdir : 删除一个新目录
.cd(变换目录)
[li@192 ~]$ su -
Password: 
Last login: Tue Jul 20 21:53:04 PDT 2021 on pts/1
ABRT has detected 1 problem(s). For more info run: abrt-cli list --since 1626843184
[root@192 ~]# cd ~li
[root@192 li]# cd ~
[root@192 ~]# cd ..
[root@192 /]# cd .
[root@192 /]# cd /usr/bin    // 绝对路径
[root@192 bin]# cd ../bin		//相路径
[root@192 bin]# 

.pwd(显示当前所在目录)
**pwd [-P]**
-P:显示绝对路径
[root@192 ~]# pwd
/root
[root@192 ~]# cd /usr/bin
**[root@192 bin]# pwd
/usr/bin
[root@192 bin]# pwd -P			//此处路径不明显
/usr/bin**
[root@192 bin]# ls -ld /usr/bin
dr-xr-xr-x. 2 root root 49152 Jul 20 21:56 /usr/bin

在这里插入图片描述

.mkdir(创建新目录)
mkdir [-mp]
-m:配置文件的权限,直接设定。
-p:将所需目录(包含上层目录)递归建立起来
[root@192 ~]# cd /li
[root@192 li]# mkdir test
[root@192 li]# mkdir -p tet1/tet2/tet3/tet4
[root@192 li]# mkdir -m 722 tet2
[root@192 li]# ls -ld tet*
drwxr-xr-x. 3 root root 18 Jul 21 00:51 tet1
drwx-w--w-. 2 root root  6 Jul 21 00:51 tet2

rmdir(删除【空】的目录)
rmdir [-p] 目录名称
-p:连同【上层】【空的】目录也一起删除
[root@192 li]# ls -ld tet*
drwxr-xr-x. 3 root root 18 Jul 21 00:51 tet1
drwx-w--w-. 2 root root  6 Jul 21 00:51 tet2
[root@192 li]# rmdir test
[root@192 li]# rmdir tet1
rmdir: failed to remove ‘tet1’: Directory not empty  //因为有内容所以无法删除,
[root@192 li]# rmdir -p tet1/tet2/tet3/tet4
[root@192 li]# ls -ld tet*
drwx-w--w-. 2 root root 6 Jul 21 00:51 tet2

6.1.3关于执行文件路径的变量:$PATH
环境变量:为了方便找到系统中软件的位置

[root@192 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@192 ~]# exit
logout
[li@192 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/li/.local/bin:/home/li/bin

在这里插入图片描述
在这里插入图片描述
警告:没有特殊要求尽量不要去做交换实验,以防虚拟机报废,如果实在好奇记得快照。

6.2文件与目录管理
6.2.1文件与目录的检视:ls

ls [-aAdfFhilnrRSt]  文件名或者目录名称
ls [--color={never,auto,always}] 文件名或者目录名称
ls [--full-time] 	文件名或者目录名称
[root@192 ~]# ls -al ~
total 48
dr-xr-x---.  4 root root  262 Jul 21 01:35 .
dr-xr-xr-x. 19 root root  247 Jul 20 21:49 ..
-rw-------.  1 root root 2757 Jul 19 01:55 anaconda-ks.cfg
-rw-------.  1 root root 1790 Jul 21 01:09 .bash_history
-rw-r--r--.  1 root root   18 Dec 28  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 28  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 28  2013 .bashrc
drwx------.  3 root root   18 Jul 19 02:01 .cache
drwxr-xr-x.  3 root root   18 Jul 19 02:01 .config
-rw-r--r--.  1 root root  100 Dec 28  2013 .cshrc
-rw-------.  1 root root 2037 Jul 19 01:55 original-ks.cfg
-rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc
-rw-------.  1 root root  916 Jul 20 21:50 .viminfo
-rw-------.  1 root root   61 Jul 21 01:35 .xauth4c6Ntu
-rw-------.  1 root root   61 Jul 19 02:01 .Xauthority
-rw-------.  1 root root   61 Jul 20 00:11 .xauthPuFmh0
[root@192 ~]# ls -a
.                .bash_history  .bashrc  .cshrc           .viminfo      .xauthPuFmh0
..               .bash_logout   .cache   original-ks.cfg  .xauth4c6Ntu
anaconda-ks.cfg  .bash_profile  .config  .tcshrc          .Xauthority
[root@192 ~]# ls -f
.              .bashrc          anaconda-ks.cfg  .bash_history
..             .cshrc           .cache           .xauthPuFmh0
.bash_logout   .tcshrc          .Xauthority      .viminfo
.bash_profile  original-ks.cfg  .config          .xauth4c6Ntu
[root@192 ~]# ls -alF --color=never
total 48
dr-xr-x---.  4 root root  262 Jul 21 01:35 ./
dr-xr-xr-x. 19 root root  247 Jul 20 21:49 ../
-rw-------.  1 root root 2757 Jul 19 01:55 anaconda-ks.cfg
-rw-------.  1 root root 1790 Jul 21 01:09 .bash_history
-rw-r--r--.  1 root root   18 Dec 28  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 28  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 28  2013 .bashrc
drwx------.  3 root root   18 Jul 19 02:01 .cache/
drwxr-xr-x.  3 root root   18 Jul 19 02:01 .config/
-rw-r--r--.  1 root root  100 Dec 28  2013 .cshrc
-rw-------.  1 root root 2037 Jul 19 01:55 original-ks.cfg
-rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc
-rw-------.  1 root root  916 Jul 20 21:50 .viminfo
-rw-------.  1 root root   61 Jul 21 01:35 .xauth4c6Ntu
-rw-------.  1 root root   61 Jul 19 02:01 .Xauthority
-rw-------.  1 root root   61 Jul 20 00:11 .xauthPuFmh0

在这里插入图片描述
6.2.2复制、删除与移动:cp,rm,mv
cp(复制文件或目录)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
rm(移除文件或目录)
rm [-fir] 文件或目录
-f:忽视不存在的文件,为警告讯息。
-i:互动模式,删除前询问是否继续
-r:递归删除{危险操作}
在这里插入图片描述
在这里插入图片描述
mv(移动文件与目录,或更名)
在这里插入图片描述
6.2.3取得路径的文件名与目录名称
在这里插入图片描述
6.3文件内容查询

  1. cat 由第一行开始显示文件内容
  2. tac 从最后一行开始显示
  3. nl 显示输出行号
  4. more 一页一页显示文件内容
  5. less 相比more可以往前翻页
  6. head 显示头几行
  7. tail 看末尾几行
  8. od 以二进制方式读取文件内容
    在这里插入图片描述
[root@192 ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@192 ~]# cat -n /etc/issue
     1	\S
     2	Kernel \r on an \m
     3	
[root@192 ~]# cat -A /etc/man_db.conf
[root@192 ~]# tac /etc/issue

Kernel \r on an \m
\S

在这里插入图片描述

[root@192 ~]# nl /etc/issue
     1	\S
     2	Kernel \r on an \m
       
[root@192 ~]# nl -b a -n rz /etc/issue
000001	\S
000002	Kernel \r on an \m
000003	
[root@192 ~]# nl -b a -n rz -w 3 /etc/issue
001	\S
002	Kernel \r on an \m
003	

6.3.2可翻动检视
more(一页一页翻动)
[root@192 ~]# more /etc/man_db.conf

在这里插入图片描述

less(一页一页翻动)
[root@192 ~]# less /etc/man_db.conf
在这里插入图片描述
6.3.3资料撷取
head(取出前面几行)

head [-n number] 文件
-n:后面接数字,代表显示几行的意思

在这里插入图片描述
tail(取出后面几行)
tail [-n number] 文件
-n:后面接参数,代表几行的意思
-f:表示持续侦测后面的文档,按下【Ctrl+c】退出

[root@192 ~]# tail -n 2 /etc/man_db.conf
# NOCACHE keeps man from creating cat pages.
#NOCACHE
[root@192 ~]# tail /etc/man_db.conf
//默认情况下,显示末尾10行


6.3.4非纯文本档:od
在这里插入图片描述

[root@192 ~]# od -t c /usr/bin/passwd
//以ASCII码展示
[root@192 ~]# od -t oCc /etc/issue
//将内容以8进制列出储存值与ASCII对照

6.3.5修改文件时间或建置新档:touch
三个时间参数:

  1. mtime:文件内容改变,就会更新时间。
  2. ctime:文件状态改变,更新时间。(包括权限和属性)
  3. atime:文件被取用,就会读取更新时间。
[root@192 ~]# date; ls -l /etc/man_db.conf ; ls -l --time=atime /etc/man_db.conf ; \ls -l --time=ctime /etc/man_db.conf
Thu Jul 22 05:10:53 PDT 2021
-rw-r--r--. 1 root root 5171 Jun  9  2014 /etc/man_db.conf
-rw-r--r--. 1 root root 5171 Jul 21 19:48 /etc/man_db.conf
-rw-r--r--. 1 root root 5171 Jul 19 01:42 /etc/man_db.conf

在这里插入图片描述

6.4文件与目录的默认权限与隐藏权限
6.4.1文件预设权限:umask
在这里插入图片描述
umask分数值指:该默认值需要减掉的权限

[root@192 ~]# umask
0022
[root@192 ~]# touch test1
[root@192 ~]# mkdir test2
[root@192 ~]# ll -d test*
-rw-r--r--. 1 root root 0 Jul 22 00:10 test1
drwxr-xr-x. 2 root root 6 Jul 22 00:10 test2

umask的利用与重要性:
#如果umask定为022,那新建数据只有用户自己具有w的权限,同群组的人只有r这个可读的权限,并无法修改。

[root@192 ~]# umask 002
[root@192 ~]# touch test3
[root@192 ~]# mkdir test4
[root@192 ~]# ll -d test[34]
-rw-rw-r--. 1 root root 0 Jul 22 00:19 test3
drwxrwxr-x. 2 root root 6 Jul 22 00:19 test4

例题:假设你的umask为003,请问该umask情况下,建立的文件与目录权限为?
答:
umask为003,所以拿掉的权限为-------wx,因此:

[root@192 ~]# umask 003
[root@192 ~]# touch test5
[root@192 ~]# mkdir test6
[root@192 ~]# ll -d test[56]
-rw-rw-r--. 1 root root 0 Jul 22 00:20 test5
drwxrwxr--. 2 root root 6 Jul 22 00:20 test6

在预设情况下,root的umask默认是022,普通用户为002.
6.4.2文件隐藏属性
chattr(配置文件案隐藏属性)
在这里插入图片描述

[root@192 ~]# cd /tmp
[root@192 tmp]# touch attrtest
[root@192 tmp]# chattr +i attrtest   //给予i属性
[root@192 tmp]# rm attrtest 
rm: remove regular empty file ‘attrtest’? y
rm: cannot remove ‘attrtest’: Operation not permitted
[root@192 tmp]# chattr -i attrtest 			//解除文件i属性
[root@192 tmp]# rm attrtest 
rm: remove regular empty file ‘attrtest’? y
[root@192 tmp]# 

lsattr(显示文件隐藏属性)
在这里插入图片描述

[root@192 tmp]# touch attrtest
[root@192 tmp]# chattr +aiS attrtest
[root@192 tmp]# lsattr attrtest
--S-ia---------- attrtest

6.4.3文件特殊权限:SUID,SCID,SBIT

[root@192 ~]# ls -ld /tmp ; ls -l /usr/bin/passwd
drwxrwxrwt. 37 root root 4096 Jul 22 00:34 /tmp
-rw**s**r-xr-x. 1 root root 27832 Jun  9  2014 /usr/bin/passwd

SUID

  1. SUID权限仅对于二进制程序有效
  2. 执行者对于该程序需要具有x的可执行权限
  3. 本权限仅在执行该程序的过程中有效
  4. 执行者将具有该程序拥有者的权限
    注意:SUID对于目录也是无效的
    SGIU
[root@192 ~]# ls -l /usr/bin/locate
-rwx--*s*--x. 1 root slocate 40520 Apr 10  2018 /usr/bin/locate

  1. SGIU对二进制程序有用
  2. 程序执行者对于该程序需具有x的权限
  3. 执行者在执行的过程中获得该程序群组的支持
[root@192 ~]# ll /usr/bin/locate /var/lib/mlocate/mlocate.db
-rwx--s--x. 1 root slocate   40520 Apr 10  2018 /usr/bin/locate
-rw-r-----. 1 root slocate 3316977 Jul 21 19:49 /var/lib/mlocate/mlocate.db

当一个目录设定SGID权限后:

  1. 当用户对此目录有r与x权限后,该用户能够进入此目录

  2. 用户在此目录下的有效群组将会变成该目录的群组

  3. 用途:若用户在此目录下具有w的权限,则使用者所建立的新文件与此目录的群组相同。
    SBIT

  4. 当用户对此目录具有w,x的权限,亦具有写入的权限。

  5. 当用户在该目录下建立文件或目录时,仅自己与root才有权利删除该文件。

SUID/SGID/SBIT权限的设定
4为SUID
2为SGID
1为SBIT

[root@192 ~]# cd /tmp
[root@192 tmp]# chmod 4755 test; ls -l test
-rwsr-xr-x. 1 root root 0 Jul 22 00:55 test
[root@192 tmp]# chmod 6755 test; ls -l test
-rwsr-sr-x. 1 root root 0 Jul 22 00:55 test
[root@192 tmp]# chmod 1755 test; ls -l test
-rwxr-xr-t. 1 root root 0 Jul 22 00:55 test
[root@192 tmp]# chmod 7666 test; ls -l test
-rwSrwSrwT. 1 root root 0 Jul 22 00:55 test
//u,g,o都没有x这个可执行标志,所以,这个S,T代表就是空
[root@192 tmp]# chmod u=rwx,go=x test; ls -l test
-rwx--x--x. 1 root root 0 Jul 22 00:55 test
//设定权限为-rws--x--x
[root@192 tmp]# chmod g+s,o+t test; ls -l test
-rwx--s--t. 1 root root 0 Jul 22 00:55 test
//在上述文件加上SGID与SBIT

6.4.4观察文件类型:file
查询某个文件属于ASCLL或者data文件,或者是binary。

[root@192 ~]# file ~/.bashrc
/root/.bashrc: ASCII text
[root@192 ~]# file /usr/bin/passwd
/usr/bin/passwd: setuid ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1e5735bf7b317e60bcb907f1989951f6abd50e8d, stripped
[root@192 ~]# file /var/lib/mlocate/mlocate.db
/var/lib/mlocate/mlocate.db: data

6.5指令与文件的搜寻
6.5.1脚本文件名的搜寻
which(寻找【执行档】)
#which [-a] command
-a:将所有由PATH目录中可以找到的指令均列出。

[root@192 ~]# which ifconfig
/sbin/ifconfig
[root@192 ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
	/bin/alias
	/usr/bin/which
[root@192 ~]# which history
/usr/bin/which: no history in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

这个指令根据【PATH】这个环境变量所规范的路径,去搜索。
6.5.2文件档名的搜寻
whereis(由一些特定的目录寻找文件及文件名)
在这里插入图片描述

[root@192 ~]# whereis ifconfig
ifconfig: /usr/sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz
[root@192 ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz
[root@192 ~]# whereis -m passwd
passwd: /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz

locate/updatedb
在这里插入图片描述

[root@192 ~]# locate -l 4 passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
[root@192 ~]# locate -S
Database /var/lib/mlocate/mlocate.db:
	13,212 directories
	144,803 files
	7,239,376 bytes in file names
	3,316,977 bytes used to store database

updatedb:根据 /etc/updatedb.conf的设定去搜寻系统盘内的文件夹,并更新 /var/lib/mlocate内的数据库文件。
locate:依据 /var/lib/mlocate内数据库记载,找出用户输入的关键词文件名。

find
在这里插入图片描述

[root@192 ~]# find / -mtime 0
[root@192 ~]# find /etc -newer /etc/passwd

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@192 ~]# find / -name passwd
find: ‘/run/user/1000/gvfs’: Permission denied
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
[root@192 ~]# find / -name "*passwd*"
find: ‘/run/user/1000/gvfs’: Permission denied
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
[root@192 ~]# find /run -type s
/run/NetworkManager/private-dhcp
/run/gssproxy.sock
/run/chrony/chronyd.sock
/run/vmware/guestServicePipe
/run/abrt/abrt.socket
/run/avahi-daemon/socket
/run/rpcbind.sock
/run/libvirt/libvirt-admin-sock
/run/libvirt/libvirt-sock-ro
/run/libvirt/libvirt-sock
/run/libvirt/virtlogd-sock
/run/libvirt/virtlockd-sock
/run/dbus/system_bus_socket
/run/setroubleshoot/setroubleshoot_server
/run/user/1000/pulse/native
find: ‘/run/user/1000/gvfs’: Permission denied
/run/user/1000/keyring/pkcs11
/run/user/1000/keyring/ssh
/run/user/1000/keyring/control
/run/lsm/ipc/simc
/run/lsm/ipc/sim
/run/cups/cups.sock
/run/lvm/lvmpolld.socket
/run/lvm/lvmetad.socket
/run/udev/control
/run/systemd/shutdownd
/run/systemd/private
/run/systemd/journal/socket
/run/systemd/journal/stdout
/run/systemd/cgroups-agent
/run/systemd/notify
[root@192 ~]# find / -perm /7000
/dev/mqueue
/dev/shm
[root@192 ~]# find /usr/bin /usr/sbin -perm /6000
[root@192 ~]# find /usr/bin /usr/sbin -perm /7000 -exec ls -l {} \;
//将上个文件使用ls - l 列出来
[root@192 ~]#find / -size +1M
//找出系统中,大于1MB的文件。

在这里插入图片描述

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 11:14:16  更:2021-07-23 11:16:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/29 2:08:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码