第三次作业

一、文件系统

1.1 文件目录系统

  • /boot:引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader,grub)都存放于此目录
  • /bin: 所有用户使用的基本命令,不能关联至独立分区,OS启动即会用到的程序
  • /sbin: 管理类的基本命令;不能关联至独立分区,OS启动即会用到的程序
  • /lib: 启动时程序依赖的基本共享文件以及内核模块文件(/lib/modules)
  • /lib64:专用于x86_64系统上的辅助共享库文件存放位置
  • /etc: 配置文件目录
  • /home:普通用户家目录
  • /root: 管理员的家目录
  • /media:便携式移动设备挂载点
  • /mnt: 临时文件系统挂载点
  • /dev: 设备文件及特殊文件存储位置
  • /opt: 第三方应用程序安装位置
  • /stv: 系统上运行的服务用到的数据
  • /tmp: 临时文件存储位置
  • /usr: 普通共享的只读数据
  • /var: 可变数据存放位置
  • /proc: 用于输出内核与进程信息相关的虚拟文件系统
  • /sys: 用于输出当前系统上硬件设备相关信息虚拟文件系统
  • /selinux:selinux相关的安全策略等信息的存储位置

1.2 CentOS7目录结构变化

  • /bin和/usr/bin
  • /sbin和/usr/sbin
  • /lib和/urs/lib
  • /lib64和/usr/lib64

1.3 Linux下的文件类型

  • -:普通文件
  • d:目录文件 directory
  • b:块设备 block
  • c:字符设备 character
  • l: 符号链接文件 link
  • p:管道文件 pipe
  • s: 套件字文件 socket

二、元数据

2.1 元数据信息表示的含有

[root@VM-0-6-centos ~]# stat filea
  File: filea
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 405278      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-09-26 10:18:35.273009503 +0800
Modify: 2020-09-26 10:18:35.273009503 +0800
Change: 2020-09-26 10:18:35.273009503 +0800
 Birth: -
  • Size:表示文件大小

  • Blocks:文件占用的磁盘块数

  • IO Block:linux系统默认的IO分块的大小,磁盘的读写是以块为单位的,

  • regular file:表示该文件是一个普通文件,目录的还在此位置是directory,软连接是symbolic link。

  • Inode:index node,文件索引节点的编号

  • links:该索引节点的硬链接数

  • Access:上次访问文件的时间。

  • Modify:上次修改文件内容的时间。

  • Change:上次改变文件属性的时间,这里的文件属性指的是如文件的权限、文件属主之类的。

  • 如果只是读取文件,那么这三个时间里只有Access发生改变;

  • 如果是修改文件内容,那么这三个时间都会改变;

  • 如果是修改文件属性,那么这三个时间只有Change发生改变。

2.2 修改Access时间

[root@VM-0-6-centos ~]# stat filea
  File: filea
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 405278      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-09-26 10:18:35.273009503 +0800
Modify: 2020-09-26 10:18:35.273009503 +0800
Change: 2020-09-26 10:18:35.273009503 +0800
 Birth: -
[root@VM-0-6-centos ~]# cat filea   #读取文件
[root@VM-0-6-centos ~]# stat filea
  File: filea
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 405278      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-23 11:12:50.219157730 +0800   # 发生变化 
Modify: 2020-09-26 10:18:35.273009503 +0800
Change: 2020-09-26 10:18:35.273009503 +0800
 Birth: -

2.3 修改Change时间

[root@VM-0-6-centos ~]# stat filea
  File: filea
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 405278      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-23 11:12:50.219157730 +0800
Modify: 2020-09-26 10:18:35.273009503 +0800
Change: 2020-09-26 10:18:35.273009503 +0800
 Birth: -
[root@VM-0-6-centos ~]# chmod a+x filea  ## 修改权限
[root@VM-0-6-centos ~]# stat filea
  File: filea
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd01h/64769d    Inode: 405278      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-23 11:12:50.219157730 +0800
Modify: 2020-09-26 10:18:35.273009503 +0800
Change: 2020-10-23 11:15:39.096126332 +0800   ## 发生改变
 Birth: -

2.4 修改Modify时间

[root@VM-0-6-centos ~]# nano filea
[root@VM-0-6-centos ~]# stat filea
  File: filea
  Size: 3               Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 405278      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-24 15:20:50.744311325 +0800  # 三个时间都发生改变
Modify: 2020-10-24 15:20:56.547311167 +0800
Change: 2020-10-24 15:20:56.547311167 +0800
 Birth: -

三、软连接和硬链接

3.1软硬链接的区别

  • 本质
    • 硬链接:本质是同一个文件
    • 软链接:本质不是同一个文件
[root@Centos8 data]# ll -i !*
ll -i f1 dir1/f1
136 -rw-r--r--. 2 root root 0 Oct 27 17:19 dir1/f1
136 -rw-r--r--. 2 root root 0 Oct 27 17:19 f1
[root@Centos8 data]# nano f1   #内容sfsdf
[root@Centos8 data]# ll -i f1 dir1/f1
136 -rw-r--r--. 2 root root 7 Oct 27 17:21 dir1/f1
136 -rw-r--r--. 2 root root 7 Oct 27 17:21 f1
# 硬链接是同一文件

[root@Centos8 data]# ln -s f2 f2.link
[root@Centos8 data]# ll -i
total 8
137 drwxr-xr-x. 2 root root   6 Oct 27 17:41 dir1
136 -rw-r--r--. 1 root root   7 Oct 27 17:21 f1
138 -rw-r--r--. 1 root root 158 Oct 27 18:17 f2
139 lrwxrwxrwx. 1 root root   2 Oct 27 18:27 f2.link -> f2
131 drwxr-xr-x. 7 root root  66 Oct 25 20:59 testdir
# 软链接不是同一文件
  • 跨设备
    • 硬链接:不支持
    • 软链接:支持
[root@Centos8 data]# ln f1 /opt/f1.txt
ln: failed to create hard link '/opt/f1.txt' => 'f1': Invalid cross-device link
# 当要在不同分区创建硬链接报错

[root@Centos8 data]# ln -s /data/testdir/ /opt/dir_link
# 在不同分区可以成功创建软链接
  • inode
    • 硬链接:相同
    • 软连接:不同
[root@Centos8 data]# ll -i !*
ll -i f1 dir1/f1
136 -rw-r--r--. 2 root root 0 Oct 27 17:19 dir1/f1
136 -rw-r--r--. 2 root root 0 Oct 27 17:19 f1
# 硬链接inode相同

[root@Centos8 data]# ll -i
total 8
137 drwxr-xr-x. 2 root root   6 Oct 27 17:41 dir1
136 -rw-r--r--. 1 root root   7 Oct 27 17:21 f1
138 -rw-r--r--. 1 root root 158 Oct 27 18:17 f2
139 lrwxrwxrwx. 1 root root   2 Oct 27 18:27 f2.link -> f2
131 drwxr-xr-x. 7 root root  66 Oct 25 20:59 testdir
# 软链接inide不相同
  • 链接数
    • 硬链接:创建新的硬链接,链接数会增加,删除硬链接,链接数减少
    • 软连接:创建或删除,链接数不会变化
[root@Centos8 data]# ll
total 8
drwxr-xr-x. 2 root root  37 Oct 27 18:43 dir1  
-rw-r--r--. 1 root root   7 Oct 27 17:21 f1    # 链接数为1
-rw-r--r--. 1 root root 158 Oct 27 18:17 f2
lrwxrwxrwx. 1 root root   2 Oct 27 18:27 f2.link -> f2
drwxr-xr-x. 7 root root  66 Oct 25 20:59 testdir
[root@Centos8 data]# ln f1 dir1/f1.link
[root@Centos8 data]# ll
total 8
drwxr-xr-x. 2 root root  52 Oct 27 21:14 dir1
-rw-r--r--. 2 root root   7 Oct 27 17:21 f1   链接数为2
-rw-r--r--. 1 root root 158 Oct 27 18:17 f2
lrwxrwxrwx. 1 root root   2 Oct 27 18:27 f2.link -> f2
drwxr-xr-x. 7 root root  66 Oct 25 20:59 testdir
[root@Centos8 data]#
# 链接数发生变化

[root@Centos8 data]# ll
total 8
drwxr-xr-x. 2 root root  37 Oct 27 18:43 dir1  
-rw-r--r--. 1 root root   7 Oct 27 17:21 f1    # 链接数为1
-rw-r--r--. 1 root root 158 Oct 27 18:17 f2    # 链接数为1
lrwxrwxrwx. 1 root root   2 Oct 27 18:27 f2.link -> f2
drwxr-xr-x. 7 root root  66 Oct 25 20:59 testdir
[root@Centos8 data]# ln -s f2 dir1/f2.link
[root@Centos8 data]# ll -i
total 8
137 drwxr-xr-x. 2 root root   6 Oct 27 17:41 dir1
136 -rw-r--r--. 1 root root   7 Oct 27 17:21 f1
138 -rw-r--r--. 1 root root 158 Oct 27 18:17 f2  # 链接数为1
139 lrwxrwxrwx. 1 root root   2 Oct 27 18:27 f2.link -> f2  # 大小为链接文件字符个数
131 drwxr-xr-x. 7 root root  66 Oct 25 20:59 testdir
# 链接数没有变化
  • 文件夹
    • 硬链接:不支持
    • 软链接:支持
[root@Centos8 data]# ln testdir dir1/
ln: testdir: hard link not allowed for directory
# 硬链接不支持文件夹

[root@Centos8 data]# ln -s ../testdir dir1/
[root@Centos8 data]# ll dir1/
total 4
-rw-r--r--. 2 root root  7 Oct 27 17:21 f1.link
lrwxrwxrwx. 1 root root 10 Oct 27 21:17 testdir -> ../testdir
# 软连接支持文件夹
  • 相对路径
    • 硬链接:原始文件相对路径是相对于当前工作目录
    • 软链接:原始文件的相对路径是相对于链接文件的相对路径
ln f1 dir1/f1.link
[root@Centos8 data]# ll f1 dir1/f1.link
-rw-r--r--. 2 root root 7 Oct 27 17:21 dir1/f1.link
-rw-r--r--. 2 root root 7 Oct 27 17:21 f1
# 硬链接原始文件起始位置是/data

[root@Centos8 data]# ln -s f2 dir1/f2.link
[root@Centos8 data]# ll -i f2 dir1/f2.link
140 lrwxrwxrwx. 1 root root   2 Oct 27 18:33 dir1/f2.link -> f2 # 错误
138 -rw-r--r--. 1 root root 158 Oct 27 18:17 f2
[root@Centos8 data]# ln -s ../f2 dir1/f2.link2
# 必须是软连接的相对路径
[root@Centos8 data]# ll -i f2 dir1/f2.link2
141 lrwxrwxrwx. 1 root root   5 Oct 27 18:43 dir1/f2.link2 -> ../f2 #正确
138 -rw-r--r--. 1 root root 158 Oct 27 18:17 f2
# 软连接原始文件起始位置是 /data/dir1
  • 删除源文件
    • 硬链接:只是链接数减一,但链接文件的访问不受影响
    • 软连接:链接文件将无法访问
[root@Centos8 data]# ll -i f1 dir1/f1
136 -rw-r--r--. 2 root root 7 Oct 27 17:21 dir1/f1 # 链接数为2
136 -rw-r--r--. 2 root root 7 Oct 27 17:21 f1
[root@Centos8 data]# rm -f dir1/f1    # 删除硬链接
[root@Centos8 data]# ll -i f1
136 -rw-r--r--. 1 root root 7 Oct 27 17:21 f1   # 链接数为1
  • 文件类型
    • 硬链接:和源文件相同
    • 软链接:链接文件和源文件无关
[root@Centos8 data]# ll
total 0
crw-rw-rw-. 1 root root 1, 3 Oct 27 15:54 null2
[root@Centos8 data]# ln null2 null2.link
[root@Centos8 data]# ll
total 0
crw-rw-rw-. 2 root root 1, 3 Oct 27 15:54 null2
crw-rw-rw-. 2 root root 1, 3 Oct 27 15:54 null2.link
# 硬链接两个文件类型相同

[root@Centos8 data]# ln -s null2 null2.link2
[root@Centos8 data]# ll
total 0
crw-rw-rw-. 2 root root 1, 3 Oct 27 15:54 null2
crw-rw-rw-. 2 root root 1, 3 Oct 27 15:54 null2.link
lrwxrwxrwx. 1 root root    5 Oct 27 21:39 null2.link2 -> null2
# 软连接文件类型发生改变

四、文件操作命令

4.1 显示当前工作目录

  • 当前工作目录CWD:current work directory
  • pwd:显示当前shell CWD的绝对路径
  • pwd:printing working directory
    • -P:显示真实物理路径
    • -L:显示链接路径(默认)
[root@VM-0-6-centos ~]# pwd
/root

4.2 绝对路径和相对路径

  • 绝对路径:以/即根开始,完整的文件的位置路径,可用于任何想指定一个文件名
  • 相对路径:不易斜线开始,一般情况下,是指相对于当前工作目录的路径,特殊场景下,是相对于某目录的位置可以作为一个简短的形式指定一个文件名
  • 基名:basename,只取文件名而不要路径
  • 目录名:dirname,只取路径,不要文件名
[root@VM-0-6-centos ~]# basename /etc/sysconfig/network-scripts/
network-scripts
[root@VM-0-6-centos ~]# dirname /etc/sysconfig/network-scripts/
/etc/sysconfig

4.3 更改目录

  • cd:change directory 改变目录
  • -P:切换值物理路径,而非软链接目录
  • 可以使用绝对和相对路径
    • 切换至父目录:cd …
    • 切换至当前用户主目录:cd
    • 切换至以前的工作目录:cd -
[root@VM-0-6-centos ~]# cd /etc/sysconfig/network-scripts/
# 切换到/etc/sysconfig/network-scripts/
[root@VM-0-6-centos network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@VM-0-6-centos network-scripts]# cd ..
# 切换到上一次目录
[root@VM-0-6-centos sysconfig]# pwd
/etc/sysconfig
[root@VM-0-6-centos sysconfig]# cd
# 切换到当前用户家目录
[root@VM-0-6-centos ~]# pwd
/root
[root@VM-0-6-centos ~]# cd -
/etc/sysconfig
# 切换至以前的工作目录
[root@VM-0-6-centos sysconfig]# pwd
/etc/sysconfig

[root@VM-0-6-centos ~]# echo $OLDPWD
/etc/sysconfig
# 上一次目录路径

4.4 列出目录内容

  • ls 命令可以列出当前目录的内容或指定目录

  • 选项

    • -a:包含隐藏文件
    • -l :显示额外的信息
    • -R:目录递归
    • -ld:目录和符号链接信息
    • -1:文件分行显示
    • -S:按从大到小排序
    • -t:按mtime排序
    • -u:配合-t选项,显示并按atime从新到旧排序
    • -U:按目录存放顺序显示
    • -X:按文件后缀排序
[root@VM-0-6-centos ~]# ls -a
.              .bash_logout   .cache  .lesshst          .ssh        .tcshrc
..             .bash_profile  .cshrc  .pip              ss.sh       .viminfo
.bash_history  .bashrc        filea   .pydistutils.cfg  .ss.sh.swp
[root@VM-0-6-centos ~]# ls -l
total 8
-rwxr-xr-x 1 root root   3 Oct 24 15:20 filea
-rwxr-xr-x 1 root root 132 Oct 23 11:43 ss.sh
[root@VM-0-6-centos ~]# ls -R
.:
filea  ss.sh

4.5 查看文件状态

  • stat
  • 三个时间戳
    • access time :访问时间,atime,读取文件内容
    • modify time:修改时间,mtime,改变文件内容
    • change time:改变时间,ctime,元数据发生变化
[root@VM-0-6-centos ~]# stat ss.sh
  File: ss.sh
  Size: 132             Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 405410      Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-23 11:44:02.652884752 +0800
Modify: 2020-10-23 11:43:59.390884830 +0800
Change: 2020-10-23 11:43:59.398884830 +0800
 Birth: -

4.6 确认文件内容

  • file:检测文件的类型,然后确定适当的打开命令或应用程序使用
  • 选项
    • -b:列出文件识别结果时,不显示文件名称
    • -f:filelist列出文件filelist中文件名的文件类型
    • -F:使用指定分割符号替换输出文件名后默认的“.”分隔符
    • -L:查看对应软连接对应文件的文件类型
[root@VM-0-6-centos ~]# file ss
ss: ASCII text

[root@VM-0-6-centos ~]# cat files
/bin
/etc
/root/ss.sh
[root@VM-0-6-centos ~]# file -f files
/bin:        symbolic link to usr/bin
/etc:        directory
/root/ss.sh: ASCII text
  • 在win10上写的UTF-8的文本格式于linux有区别
[root@VM-0-6-centos ~]# file linux.txt
linux.txt: ASCII text
[root@VM-0-6-centos ~]# file win.txt
win.txt: ASCII text, with CRLF line terminators
[root@VM-0-6-centos ~]# hexdump -C linux.txt
00000000  61 0a 62 0a 63 0a                                 |a.b.c.|
00000006
[root@VM-0-6-centos ~]# hexdump -C win.txt
00000000  61 0d 0a 62 0d 0a 63                              |a..b..c|
00000007
  • 需要进行转码执行dos2unix
[root@VM-0-6-centos ~]# yum install -y dos2unix
[root@VM-0-6-centos ~]# dos2unix win.txt   # win转linux
dos2unix: converting file win.txt to Unix format...
[root@VM-0-6-centos ~]# file win.txt
win.txt: ASCII text
[root@VM-0-6-centos ~]# unix2dos linux.txt # linux转win
unix2dos: converting file linux.txt to DOS format...
[root@VM-0-6-centos ~]# file linux.txt
linux.txt: ASCII text, with CRLF line terminators
  • windows10上的ANSI文本是GB2312而linux是UTF-8,所以要在linux上使用需要编码转换
[root@VM-0-6-centos ~]# iconv -f gb2312 win.txt -o win1.txt
[root@VM-0-6-centos ~]# file win1.txt
win1.txt: ASCII text

4.7 创建空文件和刷新时间

  • touch命令可以创建空文件或刷新文件的时间
  • 选项
    • -a:仅改变atime和ctime
    • -m:仅改变mtime和ctime
    • -t [[CC]YY]MMDDhhmm[.ss]:指定atime和mtime的时间戳
    • -c:如果文件不存在,则不予创建
[root@VM-0-6-centos mh]# touch f1.txt
[root@VM-0-6-centos mh]# ll
total 0
-rw-r--r-- 1 root root 0 Oct 24 19:49 f1.txt
[root@VM-0-6-centos mh]# ll /etc/issue
-rw-r--r--. 1 root root 23 Aug 14  2019 /etc/issue
[root@VM-0-6-centos mh]# touch /etc/issue
[root@VM-0-6-centos mh]# ll /etc/issue
-rw-r--r--. 1 root root 23 Oct 24 19:49 /etc/issue
[root@VM-0-6-centos mh]# stat /etc/issue
  File: /etc/issue
  Size: 23              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 267321      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-10-24 19:49:25.974952360 +0800
Modify: 2020-10-24 19:49:25.974952360 +0800
Change: 2020-10-24 19:49:25.974952360 +0800
 Birth: -
[root@VM-0-6-centos mh]# date
Sat Oct 24 19:51:22 CST 2020
[root@VM-0-6-centos mh]# touch `date -d "-1 day" +%F_%T`.log
[root@VM-0-6-centos mh]# ls
2020-10-23_19:52:02.log  f1.txt
[root@VM-0-6-centos mh]# touch `date -d "-1 day" +%F_%T`.log
[root@VM-0-6-centos mh]# ls
2020-10-23_19:52:02.log  2020-10-23_19:52:13.log  f1.txt

4.8 复制文件和目录

  • cp:文件或目录的复制
  • 直接拷贝会丢失属性
  • 选项
    • -i:如果目标存在,覆盖前提示是否覆盖
    • -n:不覆盖,
    • -r、-R:递归复制目录及内部的所有内容
    • -a:归档,相当于-dR --preserv=all,常用于备份功能
    • -d:–no-dereference --preserv=links:不复制原文件,只复制链接名
    • –preserv[=ATTR_LIST]
      • mode:权限
      • ownership:属主属组
      • timestamp:时间戳
      • links
      • xattr
      • context
      • all
    • -p:–preserv=mode,ownership,timestamp
    • -v:verbose
    • -f:force
    • -u:–update,只复制源比目标更新文件或目标不存在的文件
    • -b:目标存在,覆盖前先备份,形式为filename~,只保留一个备份()
    • –backup=numbered:目标存在,覆盖前先备份加数字后缀,形式为filename.#
  • 在root里cp使用的是重命名为“alias cp=‘cp -i’”,而在普通用户中是原命令,所以在root权限中复制不想提示可以使用“/cp”
# 复制
[root@Centos8 data]# cp /etc/issue ./issue.bak
[root@Centos8 data]# ls
f1.txt  issue.bak
# 保留权限复制
[root@Centos8 data]# cp -p /home/mh/.bashrc ./.bashrc2
[root@Centos8 data]# ll -a /home/mh/.bashrc ./.bashrc2
-rw-r--r--. 1 mh mh 312 Nov  9  2019 ./.bashrc2
-rw-r--r--. 1 mh mh 312 Nov  9  2019 /home/mh/.bashrc
# 复制文件夹
[root@Centos8 data]# cp -r /etc/ etcbak
[root@Centos8 data]# ll
total 16
drwxr-xr-x. 137 root root 8192 Oct 25 16:48 etcbak
-rw-r--r--.   1 root root   52 Oct 25 16:37 issue.bak
# 拷贝链接文件,拷贝链接文件的真实文件
[root@Centos8 data]# ll /etc/redhat-release /etc/centos-release
-rw-r--r--. 1 root root 38 Jun  3 09:02 /etc/centos-release
lrwxrwxrwx. 1 root root 14 Jun  3 09:02 /etc/redhat-release -> centos-release
[root@Centos8 data]# cp /etc/redhat-release .
[root@Centos8 data]# ll
total 20
drwxr-xr-x. 137 root root 8192 Oct 25 16:48 etcbak
-rw-r--r--.   1 root root   52 Oct 25 16:37 issue.bak
-rw-r--r--.   1 root root   38 Oct 25 16:51 redhat-release
# 移动整个包括隐藏文件
[root@Centos8 data]# ls -a
.  ..  .a  a  .b  b  .c  c
[root@Centos8 data]# cp -av . ^C
[root@Centos8 data]# ls /opt/
[root@Centos8 data]# cp -av . /opt/
'./.a' -> '/opt/./.a'
'./.b' -> '/opt/./.b'
'./.c' -> '/opt/./.c'
'./a' -> '/opt/./a'
'./b' -> '/opt/./b'
'./c' -> '/opt/./c'
[root@Centos8 data]# ls -a /opt/
.  ..  .a  a  .b  b  .c  c

4.9 移动和重命名文件

  • mv:文件或目录的移动和改名
  • 同一分区移动数据,速度很快,数据位置没有变化
  • 不同分区移动数据,速度相对慢;数据位置发生变化
  • 选项
    • -i:交互式
    • -f:强制
    • -b:目标存在,覆盖前先备份
  • mv也是别名“alias mv=‘mv -i’”
  • rename:可以批量修改文件名
[root@Centos8 data]# ls
f1~  f1.~1~  f1.~2~  ss
[root@Centos8 data]# ls /home/
f1  mh
[root@Centos8 data]# mv f1~ /home/f2
[root@Centos8 data]# ls
f1.~1~  f1.~2~  ss
[root@Centos8 data]# ls /home/
f1  f2  mh
# 批量改名
[root@Centos8 data]# touch file{1..10}.txt
[root@Centos8 data]# ls
file10.txt  file2.txt  file4.txt  file6.txt  file8.txt
file1.txt   file3.txt  file5.txt  file7.txt  file9.txt
[root@Centos8 data]# rename txt txt.bak *
[root@Centos8 data]# ls
file10.txt.bak  file2.txt.bak  file4.txt.bak  file6.txt.bak  file8.txt.bak
file1.txt.bak   file3.txt.bak  file5.txt.bak  file7.txt.bak  file9.txt.bak

4.10 删除文件

  • rm:删除文件

  • 命令危险,可使用mv代替

  • 选项

    • -i:交互式
    • -f:强制删除
    • -r:递归
    • –no-preserve-root 删除/
  • shred:安全删除

  • 选项

    • -z:最后一次覆盖添加0,以隐藏覆盖操作
    • -v:能够显示操作进度
    • -u:覆盖后截断并删除文件
    • -n # :指定覆盖文件内容的次数(默认值是3次)
[root@Centos8 data]# cp -r /etc/ .
[root@Centos8 data]# ls
etc
[root@Centos8 data]# rm -rf etc/
[root@Centos8 data]# ls
# 删除隐藏文件
[root@Centos8 data]# ls -a
.  ..  .bashrc  .bashrc2
[root@Centos8 data]# rm -rf .*
rm: refusing to remove '.' or '..' directory: skipping '.'
rm: refusing to remove '.' or '..' directory: skipping '..'
[root@Centos8 data]# ls -a
.  ..
# 存在风险
# 使用通配符删除隐藏文件
[root@Centos8 data]# touch .a .b .c .d
[root@Centos8 data]# ls -a
.  ..  .a  .b  .c  .d
[root@Centos8 data]# rm -rf .[^.]*
[root@Centos8 data]# ls -a
.  ..

# 删除特殊文件
[root@Centos8 data]# touch ./-f
[root@Centos8 data]# rm -f -f
[root@Centos8 data]# ls
-f
[root@Centos8 data]# rm /data/-f
rm: remove regular empty file '/data/-f'? y
[root@Centos8 data]#
[root@Centos8 data]# touch ./-f
[root@Centos8 data]# ls
-f
[root@Centos8 data]# rm -f ./-f
[root@Centos8 data]# ls
[root@Centos8 data]# touch ./-f
[root@Centos8 data]# ls
-f
[root@Centos8 data]# rm -- -f   # 只能是-开头的
rm: remove regular empty file '-f'? y
[root@Centos8 data]# ls

4.11 目录操作

4.11.1 显示目录树

  • tree
  • 选项
    • -d:只显示目录
    • -L level:指定显示的层级数目
    • -P pattern:只显示由指定wild-card pattern匹配到的路径
[root@Centos8 data]# tree /etc/sysconfig/rhn/
/etc/sysconfig/rhn/
├── allowed-actions
│   ├── configfiles
│   └── script
├── clientCaps.d
└── up2date

4 directories, 1 file

4.11.2 创建目录

  • mkdir

  • 选项

    • -p:存在于不报错,且可自动创建所需的各目录
    • -v:显示详细信息
    • -m MIDE:创建目录时指定权限
[root@Centos8 data]# mkdir dir1
l[root@Centos8 data]# ls
dir1

4.11.3 删除空目录

  • rmdir
  • 选项
    • -p:递归删除父空目录
    • -v:显示详细信息

rmdor只能删除空目录,如果想删除非空目录,可以使用rm -r命令,递归删除目录树。

[root@Centos8 data]# rmdir dir1/
[root@Centos8 data]# ls

五、其他

5.1复制/etc/profile至/tmp/目录

[root@VM-0-6-centos ~]# cp /etc/profile /tmp/
[root@VM-0-6-centos ~]# ls /tmp/
6cedba805efd07d7c9f32183734eabfb
cvm_init.log
net_affinity.log
nv_driver_install.log
nv_gpu_conf.log
profile    # 复制的文件
setRps.log
stargate.lock
systemd-private-16b702e942f64b45a22ecb3d1807563c-chronyd.service-MGHuil
[root@VM-0-6-centos ~]#

5.2 删除/tmp/profile文件中的行首的空白字符

[root@VM-0-6-centos ~]# tr -d ' ' < /tmp/profile
#/etc/profile

#Systemwideenvironmentandstartupprograms,forloginsetup
#Functionsandaliasesgoin/etc/bashrc

#It'sNOTagoodideatochangethisfileunlessyouknowwhatyou
#aredoing.It'smuchbettertocreateacustom.shshellscriptin
#/etc/profile.d/tomakecustomchangestoyourenvironment,asthis
#willpreventtheneedformerginginfutureupdates.

5.3在vim中设置tab缩进为4个字符

[root@VM-0-6-centos ~]# vim /etc/vimrc
# 在最后添加
set ts=4

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值