温故而知新——Linux 文件与目录管理(一)

Linux 文件与目录管理(一)

1、目录与路径

1.1、目录的相关操作

变换目录的指令是 cd,特殊的目录表示,需要记住:

.代表此层目录
代表上一层目录
-代表前一个工作目录
~代表 “目前用户身份” 所在的家目录
~account代表 account 这个用户的家目录(account 是账号名称)

需要注意的是:在所有目录底下都会存在的两个目录,分别是 “.” 和 “…” 分别代表此层和上层目录的意思。那根目录的上层目录是什么?

[root@MiWiFi-R4A-srv ~]# cd /
[root@MiWiFi-R4A-srv /]# ls -al
total 28
dr-xr-xr-x.  17 root root  224 Aug 10 16:25 .
dr-xr-xr-x.  17 root root  224 Aug 10 16:25 ..
......

常见的处理目录的指令:

  • cd :变换目录

  • pwd :显示当前目录

  • mkdir : 新建一个目录

  • rmdir : 删除一个目录

  • cd(change directory,变换目录)

    说明一下前面说到的特殊目录的意思:

    [root@MiWiFi-R4A-srv /]# cd [相对路径或绝对路径]
    # 最重要的就是一些 相对路径或绝对路径,还有一些特殊的目录符号!
    
    [root@MiWiFi-R4A-srv /]# cd ~li
    # 这个代表去 li 这个用户的家目录,即 /home/li
    
    [root@MiWiFi-R4A-srv li]# cd ~
    # 这个代表的是回到自己的家目录,亦即 /root 这个目录
    
    [root@MiWiFi-R4A-srv ~]# cd
    # 没有加上任何路径,也还是代表回到自己的家目录
    
    [root@MiWiFi-R4A-srv ~]# cd ..
    # 表示去到目前的上层目录,亦即是 /root 的上层目录
    
    [root@MiWiFi-R4A-srv /]# cd -
    /root
    # 表示回到刚刚的那个目录,也就是 /root
    
    [root@MiWiFi-R4A-srv ~]# cd /var/spool/mail/
    # 绝对路径的写法
    
    [root@MiWiFi-R4A-srv mail]# cd ../postfix/
    # 相对路径的写法,即由 /var/spool/mail/ 去到 /var/spool/postfix/
    
  • pwd(显示当前所在的目录)

    [root@MiWiFi-R4A-srv /]# pwd [-P]
    选项与参数:
    -P :显示出确定的路径,而非使用链接(link)路径
    
    范例:单纯显示出目前的工作目录:
    [root@MiWiFi-R4A-srv ~]# pwd
    /root
    
    范例:显示出实际的工作目录,而非链接文件本身的目录名而已
    [root@MiWiFi-R4A-srv ~]# cd /var/mail	# 注意,/var/mail 是一个链接文件
    [root@MiWiFi-R4A-srv mail]# pwd
    /var/mail
    [root@MiWiFi-R4A-srv mail]# pwd -P
    /var/spool/mail
    [root@MiWiFi-R4A-srv mail]# ls -ld /var/mail
    lrwxrwxrwx. 1 root root 10 Nov  3  2020 /var/mail -> spool/mail
    
  • mkdir(建立新目录)

    [root@MiWiFi-R4A-srv /]# mkdir [-mp] 目录名称
    选项与参数:
    -m :配置文件的权限!直接设定,不需要看预设权限(umask)
    -p :帮助你直接将所需要的目录(包含上层目录)递归地建立起来
    
    范例:请到 /tmp 底下尝试建立个新目录看看:
    [root@MiWiFi-R4A-srv mail]# cd /tmp
    [root@MiWiFi-R4A-srv tmp]# mkdir test
    [root@MiWiFi-R4A-srv tmp]# mkdir test1/test2/test3/test4
    mkdir: cannot create directory ‘test1/test2/test3/test4’: No such file or directory
    [root@MiWiFi-R4A-srv tmp]# mkdir -p test1/test2/test3/test4
    # 原来是要建 test4 上层没先建 test3 之故!加了 -p 这个选项,可以自行帮你建立多层目录!
    
    范例:建立权限 rwx--x--x 的目录:
    [root@MiWiFi-R4A-srv tmp]# mkdir -m 711 test2
    [root@MiWiFi-R4A-srv tmp]# ls -ld test*
    drwxr-xr-x. 2 root root  6 Aug 22 15:28 test
    drwxr-xr-x. 3 root root 19 Aug 22 15:33 test1
    drwx--x--x. 2 root root  6 Aug 22 15:30 test2
    drwxr--r--. 2 li   root  6 Aug 20 19:05 testing
    

    新建目录的权限与 umask 有关,后边具体说。

  • rmdir(删除 “空” 的目录)

    [root@MiWiFi-R4A-srv /]# rmdir [-p] 目录名称
    选项与参数:
    -p :连同 “上层”“空的” 目录也一起删除
    
    范例:将于 mkdir 范例中建立的目录(/tmp 底下)删除掉
    [root@MiWiFi-R4A-srv tmp]# ls -ld test*
    drwxr-xr-x. 2 root root  6 Aug 22 15:28 test
    drwxr-xr-x. 3 root root 19 Aug 22 15:33 test1
    drwx--x--x. 2 root root  6 Aug 22 15:30 test2
    drwxr--r--. 2 li   root  6 Aug 20 19:05 testing
    [root@MiWiFi-R4A-srv tmp]# rmdir test	# 可直接删除掉
    [root@MiWiFi-R4A-srv tmp]# rmdir test1	# 因为尚有内容,无法删除
    rmdir: failed to remove 'test1': Directory not empty
    [root@MiWiFi-R4A-srv tmp]# rmdir -p test1/test2/test3/test4/
    [root@MiWiFi-R4A-srv tmp]# ls -ld test*
    drwx--x--x. 2 root root 6 Aug 22 15:30 test2
    drwxr--r--. 2 li   root 6 Aug 20 19:05 testing
    

1.2、关于执行文件路径的变量:$PATH

我们知道查阅文件属性的指令 ls 完整文件名为:/bin/ls(这是绝对路径),那你会不会感觉奇怪:为什么我可以在任何地方执行 /bin/ls 这个指令呢?为什么我在任何目录下输入 ls 就一定可以显示出一些信息而不会说找不到该 /bin/ls 指令呢?这是因为环境变量 PATH 的帮忙。

当我们执行一个指令的时候,举例来说 “ls”,系统会依照 PATH 的设定去每个 PATH 定义的目录下搜索文件名为 ls 的可执行文件,如果在 PATH 定义的目录中含有多个文件名为 ls 的可执行文件,那么先搜寻到的同名指令先被执行!

[root@MiWiFi-R4A-srv tmp]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

范例:用 li 的身份列出搜索到的路径
[li@MiWiFi-R4A-srv ~]$ echo $PATH
/home/li/.local/bin:/home/li/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

PATH 这个变量的内容是由一堆目录所组成的,每个目录中间用冒号(:)来隔开。你可以发现,无论是 root 还是 li 都有 /usr/bin(/bin 链接到 /usr/bin 了),所以无论在任何目录都可以执行 ls 来找到 /bin/ls 执行文件。

总结如下:

  • 不同身份使用者预设的 PATH 不同,默认能够随意执行的指令也不同(如 root 与 li);
  • PATH 是可以修改的;
  • 使用绝对路径或相对路径直接指定某个指令的文件名来执行,会比搜索 PATH 来的正确;
  • 指令应该放在正确的目录下,执行会比较方便;
  • 本目录(.)最好不要放到 PATH 中。

2、文件与目录管理

2.1、查看文件和目录命令 ls

[root@MiWiFi-R4A-srv ~]# ls [-aAdfFhinrRSt] 文件名或目录名称..
[root@MiWiFi-R4A-srv ~]# ls [--color={never,auto,always}] 文件名或目录名称..
[root@MiWiFi-R4A-srv ~]# ls [--full-time] 文件名或目录名称..
选项与参数的功能:
-a :全部的文件,连同隐藏文件( 开头为 . 的文件) 一起列出来(常用)
-A :全部的文件,连同隐藏文件,但不包括 ... 这两个目录
-d :仅列出目录本身,而不是列出目录内的文件数据(常用)
-f :直接列出结果,而不进行排序 (ls 默认会以文件名排序!)
-F :根据文件、目录等信息,给予附加数据结构,例如:
	 *:代表可可执行文件; /:代表目录; =:代表 socket 文件; |:代表 FIFO 文件;
-h :将文件大小以易读的方式(例如 GB, KB 等等)列出来;
-i :列出 inode 号码,inode 的意义下一章将会介绍;
-l :长数据串行出,包含文件的属性与权限等等数据;(常用)
-n :列出 UID 与 GID 而非使用者与群组的名称 (UID与GID会在帐号管理提到!)
-r :将排序结果反向输出,例如:原本文件名由小到大,反向则为由大到小;
-R :连同子目录内容一起列出来,等于该目录下的所有文件都会显示出来;
-S :以文件大小大小排序,而不是用文件名排序;
-t :依时间排序,而不是用文件名。
--color=never :不要依据文件特性给予颜色显示;
--color=always :显示颜色
--color=auto :让系统自行依据设置来判断是否给予颜色
--full-time :以完整时间模式 (包含年、月、日、时、分) 输出
--time={atime,ctime} :输出 access 时间或改变权限属性时间 (ctime)而非内容变更时间 (modification time)
范例一:将家目录下的所有文件列出来(含属性和隐藏文件)
[root@MiWiFi-R4A-srv ~]# ls -al ~
total 56
dr-xr-x---. 15 root root 4096 Aug 22 14:54 .
dr-xr-xr-x. 18 root root  237 Aug 22 15:29 ..
-rw-------.  1 root root 2723 Aug 10 16:32 anaconda-ks.cfg
-rw-------.  1 root root  927 Aug 21 15:45 .bash_history
-rw-r--r--.  1 root root   18 May 11  2019 .bash_logout
-rw-r--r--.  1 root root  176 May 11  2019 .bash_profile
-rw-r--r--.  1 root root  176 May 11  2019 .bashrc
-rw-r--r--.  1 root root  176 Aug 20 17:59 .bashrc_test
drwx------. 10 root root  235 Aug 14 11:29 .cache
drwx------. 12 root root  227 Aug 14 11:30 .config
-rw-r--r--.  1 root root  100 May 11  2019 .cshrc
drwx------.  3 root root   25 Aug 10 16:43 .dbus
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Desktop
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Documents
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Downloads
-rw-------.  1 root root   16 Aug 14 11:29 .esd_auth
-rw-------.  1 root root  314 Aug 14 11:29 .ICEauthority
-rw-r--r--.  1 root root    0 Aug 20 17:42 initial-setup-ks.cfg
drwx------.  3 root root   19 Aug 14 11:29 .local
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Music
-rw-------.  1 root root 2049 Aug 10 16:32 original-ks.cfg
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Pictures
drwxr-----.  3 root root   19 Aug 14 11:29 .pki
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Public
-rw-------.  1 root root   59 Aug 14 11:29 .serverauth.53398
-rw-r--r--.  1 root root  129 May 11  2019 .tcshrc
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Templates
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Videos
-rw-------.  1 root root  278 Aug 22 14:54 .Xauthority

范例二:承上题,不显示颜色,但在文件名末尾显示出该文件名代表的类型(type)
[root@MiWiFi-R4A-srv ~]# ls -alF --color=never ~
total 56
dr-xr-x---. 15 root root 4096 Aug 22 14:54 ./
dr-xr-xr-x. 18 root root  237 Aug 22 15:29 ../
-rw-------.  1 root root 2723 Aug 10 16:32 anaconda-ks.cfg
-rw-------.  1 root root  927 Aug 21 15:45 .bash_history
-rw-r--r--.  1 root root   18 May 11  2019 .bash_logout
-rw-r--r--.  1 root root  176 May 11  2019 .bash_profile
-rw-r--r--.  1 root root  176 May 11  2019 .bashrc
-rw-r--r--.  1 root root  176 Aug 20 17:59 .bashrc_test
drwx------. 10 root root  235 Aug 14 11:29 .cache/
drwx------. 12 root root  227 Aug 14 11:30 .config/
-rw-r--r--.  1 root root  100 May 11  2019 .cshrc
drwx------.  3 root root   25 Aug 10 16:43 .dbus/
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Desktop/
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Documents/
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Downloads/
-rw-------.  1 root root   16 Aug 14 11:29 .esd_auth
-rw-------.  1 root root  314 Aug 14 11:29 .ICEauthority
-rw-r--r--.  1 root root    0 Aug 20 17:42 initial-setup-ks.cfg
drwx------.  3 root root   19 Aug 14 11:29 .local/
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Music/
-rw-------.  1 root root 2049 Aug 10 16:32 original-ks.cfg
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Pictures/
drwxr-----.  3 root root   19 Aug 14 11:29 .pki/
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Public/
-rw-------.  1 root root   59 Aug 14 11:29 .serverauth.53398
-rw-r--r--.  1 root root  129 May 11  2019 .tcshrc
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Templates/
drwxr-xr-x.  2 root root    6 Aug 14 11:29 Videos/
-rw-------.  1 root root  278 Aug 22 14:54 .Xauthority

范例三:完整地呈现文件的修改时间(modification time)
[root@MiWiFi-R4A-srv ~]# ls -al --full-time ~
total 56
dr-xr-x---. 15 root root 4096 2021-08-22 14:54:57.073052611 +0800 .
dr-xr-xr-x. 18 root root  237 2021-08-22 15:29:18.743216058 +0800 ..
-rw-------.  1 root root 2723 2021-08-10 16:32:15.533180425 +0800 anaconda-ks.cfg
-rw-------.  1 root root  927 2021-08-21 15:45:59.843968619 +0800 .bash_history
-rw-r--r--.  1 root root   18 2019-05-11 23:33:50.000000000 +0800 .bash_logout
-rw-r--r--.  1 root root  176 2019-05-11 23:33:50.000000000 +0800 .bash_profile
-rw-r--r--.  1 root root  176 2019-05-11 23:33:50.000000000 +0800 .bashrc
-rw-r--r--.  1 root root  176 2021-08-20 17:59:57.999673487 +0800 .bashrc_test
drwx------. 10 root root  235 2021-08-14 11:29:55.706357165 +0800 .cache
drwx------. 12 root root  227 2021-08-14 11:30:05.824394644 +0800 .config
-rw-r--r--.  1 root root  100 2019-05-11 23:33:50.000000000 +0800 .cshrc
drwx------.  3 root root   25 2021-08-10 16:43:50.340853846 +0800 .dbus
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Desktop
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Documents
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Downloads
-rw-------.  1 root root   16 2021-08-14 11:29:40.259299938 +0800 .esd_auth
-rw-------.  1 root root  314 2021-08-14 11:29:24.571241840 +0800 .ICEauthority
-rw-r--r--.  1 root root    0 2021-08-20 17:42:39.763269860 +0800 initial-setup-ks.cfg
drwx------.  3 root root   19 2021-08-14 11:29:25.968247013 +0800 .local
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Music
-rw-------.  1 root root 2049 2021-08-10 16:32:15.432179770 +0800 original-ks.cfg
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Pictures
drwxr-----.  3 root root   19 2021-08-14 11:29:36.389285611 +0800 .pki
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Public
-rw-------.  1 root root   59 2021-08-14 11:29:20.039225053 +0800 .serverauth.53398
-rw-r--r--.  1 root root  129 2019-05-11 23:33:50.000000000 +0800 .tcshrc
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Templates
drwxr-xr-x.  2 root root    6 2021-08-14 11:29:35.064280707 +0800 Videos
-rw-------.  1 root root  278 2021-08-22 14:54:57.073052611 +0800 .Xauthority

2.2、复制、删除和移动:cp,rm,mv

2.2.1、cp(复制文件或目录)
[root@MiWiFi-R4A-srv ~]# cp [-adfilprsu] 来源文件(source) 目标文件(destination)
[root@MiWiFi-R4A-srv ~]# cp [options] source2 source3 ... directory
选项与参数:
-a :相当于 -dr --preserve=all 的意思(常用)
-d :若来源文件为链接文件的属性(link file),则复制链接文件属性而非文件本身
-f :为强制(force)的意思,若目标文件已经存在且无法开启,则移除后再尝试一次
-i :若目标文件(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
-l :进行硬链接(hard link)的链接文件的建立,而非复制文件本身
-p :连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性(备份常用)
-r :递归持续复制,用于目录的复制(常用)
-s :复制成为符号链接文件(sysbolic link),亦即 “快捷方式” 文件
-u :destination 比 source 旧才更新 destination,或 destination 不存在的情况下才复制
--preserve=all :除了 -p 的权限相关参数外,还加入 SELinux 的属性,links,xattr 等也复制
需要注意的是,如果来源文件有两个以上,则最后一个目的文件一定要是 “目录” 才行!

案例:

范例一:用 root 身份将家目录下的 .bashrc 复制到 /tmp 下,并更名为 bashrc:
[root@MiWiFi-R4A-srv ~]# cp ~/.bashrc /tmp/bashrc
[root@MiWiFi-R4A-srv ~]# cp -i ~/.bashrc /tmp/bashrc
cp: overwrite '/tmp/bashrc'? n
# 重复两次动作,由于 /tmp 底下已经存在 bashrc,加上 -i 选项后则会覆盖前会询问使用者是否确定

范例二:变换目录到 /tmp,并将 /var/log/wtmp 复制到 /tmp 且观察属性:
[root@MiWiFi-R4A-srv ~]# cd /tmp
[root@MiWiFi-R4A-srv tmp]# cp /var/log/wtmp .	# 复制到当前目录,不要忘了最后的 .
[root@MiWiFi-R4A-srv tmp]# ls -l /var/log/wtmp wtmp
-rw-rw-r--. 1 root utmp 23808 Aug 22 16:59 /var/log/wtmp
-rw-r--r--. 1 root root 23808 Aug 22 17:44 wtmp
# 注意上面的属性,在不加任何选项的情况下,文件的某些属性/权限会改变
# 很重要的一点,连文件的建立时间都不一样了!!!

# 那如果想将文件的所有特性都一起复制过来该怎么办?加上 -a
[root@MiWiFi-R4A-srv tmp]# cp -a /var/log/wtmp wtmp2
[root@MiWiFi-R4A-srv tmp]# ls -l /var/log/wtmp wtmp wtmp2
-rw-rw-r--. 1 root utmp 23808 Aug 22 16:59 /var/log/wtmp
-rw-r--r--. 1 root root 23808 Aug 22 17:44 wtmp
-rw-rw-r--. 1 root utmp 23808 Aug 22 16:59 wtmp2	# 这样,整个文件才一模一样

一般来说,我们如果去复制别人的文件(当然,该文件你必须要有 read 的权限才行)时,总是希望复制到的数据最后是我们自己的,所以,在预设的条件中,cp 的来源文件与目的文件的权限是不同的,目的文件的拥有者通常会是执行指令的操作者本身。举例来说,上面的案例中,由于我是用 root 身份,因此复制过来的文件拥有者与群组就改变成了 root 所有的了。

因此,当我们备份的时候,某些需要特别注意的特殊权限文件,例如密码(/etc/shadow)以及一些配置文件,就不能直接以 cp 来复制,而必须加上 -a 或者是 -p 等等可以完整复制文件权限的选项才行!另外,如果你想要复制文件给其他的使用者,也必须要注意到文件的权限(包含读、写、执行以及文件拥有者等等),否则,其他人还是无法针对你给予的文件进行修订的动作。

范例三:复制 /etc/ 这个目录下的所有文件到 /tmp 底下
[root@MiWiFi-R4A-srv tmp]# cp /etc/ /tmp
cp: -r not specified; omitting directory '/etc/'
[root@MiWiFi-R4A-srv tmp]# cp -r /etc/ /tmp
# 再次强调,-r 是可以复制目录,但是,文件与目录的权限可能会被改变

范例四:将范例一复制的 bashrc 建立一个链接文件(symbolic link)
[root@MiWiFi-R4A-srv tmp]# ls -l bashrc
-rw-r--r--. 1 root root 176 Aug 22 17:42 bashrc
[root@MiWiFi-R4A-srv tmp]# cp -s bashrc bashrc_slink
[root@MiWiFi-R4A-srv tmp]# cp -l bashrc bashrc_hlink
[root@MiWiFi-R4A-srv tmp]# ls -l bashrc*
-rw-r--r--. 2 root root 176 Aug 22 17:42 bashrc
-rw-r--r--. 2 root root 176 Aug 22 17:42 bashrc_hlink
lrwxrwxrwx. 1 root root   6 Aug 22 20:35 bashrc_slink -> bashrc

使用 -l 及 -s 都会建立所谓的链接文件(link file),但是这两个链接文件却有不一样的情况。这是怎么一回事?那个 -l 就是所谓的实体链接(hard link),至于 -s 则是符号链接(symbolic link)。简单来说,bashrc_slink 是一个 “快捷方式”,会有一个 “–>” 的符号!

至于 bashrc_hlink 文件与 bashrc 的属性与权限完全一模一样,与尚未进行链接前的差异则是第二栏的 link 数由 1 变成了 2。

范例五:若 ~/.bashrc 比 /tmp/bashrc 新才复制过来
[root@MiWiFi-R4A-srv tmp]# cp -u ~/.bashrc /tmp/bashrc
# 这个 -u 的特性,是在目标文件与来源文件有差异时,才复制过来,所以,常用于 “备份” 的工作中

范例六:将范例四造成的 bashrc_slink 复制成 bashrc_slink_1 与 bashrc_slink_2
[root@MiWiFi-R4A-srv tmp]# cp bashrc_slink bashrc_slink_1
[root@MiWiFi-R4A-srv tmp]# cp -d bashrc_slink bashrc_slink_2
[root@MiWiFi-R4A-srv tmp]# ls -l bashrc bashrc_slink*
-rw-r--r--. 2 root root 176 Aug 22 17:42 bashrc
lrwxrwxrwx. 1 root root   6 Aug 22 20:35 bashrc_slink -> bashrc
-rw-r--r--. 1 root root 176 Aug 22 20:42 bashrc_slink_1		# 与源文件相同
lrwxrwxrwx. 1 root root   6 Aug 22 20:42 bashrc_slink_2 -> bashrc	# 是链接文件
# 如果没有加上任何选项时,cp 复制的是源文件,而非链接文件的属性!若要复制链接文件的属性,就得要使用 -d 的选项

范例七:将家目录的 .bashrc 及 .bash_history 复制到 /tmp 底下
[root@MiWiFi-R4A-srv tmp]# cp ~/.bashrc ~/.bash_history /tmp
# 可以将多个数据一次复制到同一个目录,最后面一定是目录!

总之,由于 cp 有种种的文件属性与权限特性,所以,在复制的时候,你必须要清除的了解到:

  • 是否需要完整的保留来源文件的信息?
  • 来源文件是否为链接文件(symbolic link file)?
  • 来源文件是否为特殊文件,例如 FIFO,socket?
  • 来源文件是否为目录?
2.2.2、rm(移除文件或目录)
[root@MiWiFi-R4A-srv ~]# rm [-fir] 文件或目录
选项与参数:
-f	:	就是 force 的意思,忽略不存在的文件,不会出现警告信息
-i	:	互动模式,在删除前会询问使用者是否动作
-r	:	递归删除!最常用在目录的删除!这是非常危险的选项!!!
范例一:将刚刚在 cp 的范例中建立的 bashrc 删除掉
[root@MiWiFi-R4A-srv ~]# cd /tmp
[root@MiWiFi-R4A-srv tmp]# rm -i bashrc
rm: remove regular file 'bashrc'? y
# 加上 -i 的选项之后就会主动询问,避免删除掉错误的文件

范例二:通过通配符帮忙,将 /tmp 底下开头为 bashrc 的文件名通通删除
[root@MiWiFi-R4A-srv tmp]# rm -i bashrc*
rm: remove regular file 'bashrc_hlink'? y
rm: remove symbolic link 'bashrc_slink'? y
rm: remove regular file 'bashrc_slink_1'? y
rm: remove symbolic link 'bashrc_slink_2'? y

范例三:将 cp 范例中所建立的 /tmp/etc/ 这个目录删除掉
[root@MiWiFi-R4A-srv tmp]# rmdir /tmp/etc/
rmdir: failed to remove '/tmp/etc/': Directory not empty	# 因为不是空目录
[root@MiWiFi-R4A-srv tmp]# rm -r /tmp/etc/
rm: descend into directory '/tmp/etc/'? y
rm: descend into directory '/tmp/etc/java'? y
rm: descend into directory '/tmp/etc/java/java-1.8.0-openjdk'? y
rm: descend into directory '/tmp/etc/java/java-1.8.0-openjdk/java-1.8.0-openjdk-1.8.0.282.b08-4.el8.x86_64'? ^C		# 按下 [Ctrl]+c 中断
[root@MiWiFi-R4A-srv tmp]# \rm -r /tmp/etc/
# 在指令面前加上反斜杠,可以忽略掉 alias 的指定选项
2.2.3、mv(移动文件与目录,或更名)
[root@MiWiFi-R4A-srv ~]# mv [-fiu] source destination
[root@MiWiFi-R4A-srv ~]# mv [options] source1 source2 source3 ... directory
选项与参数:
-f	:	就是 force 的意思,如果目标文件已经存在,不会询问而直接覆盖
-i	:	若目标文件(destination)已经存在时,就会询问是否覆盖!
-u	:	若目标文件已经存在,且 source 比较新,才会更新(update)
范例一:复制一文件,建立一目录,将文件移动到目录中
[root@MiWiFi-R4A-srv tmp]# cp ~/.bashrc bashrc
[root@MiWiFi-R4A-srv tmp]# mkdir mvtest
[root@MiWiFi-R4A-srv tmp]# mv bashrc mvtest
# 若将某个文件移动到某个目录去,就这样做!

范例二:将刚刚的目录名称更名为 mvtest2
[root@MiWiFi-R4A-srv tmp]# mv mvtest mvtest2

范例三:再建立两个文件,再全部移动到 /tmp/mvtest2 当中
[root@MiWiFi-R4A-srv tmp]# cp ~/.bashrc bashrc1
[root@MiWiFi-R4A-srv tmp]# cp ~/.bashrc bashrc2
[root@MiWiFi-R4A-srv tmp]# mv bashrc1 bashrc2 mvtest2
# 如果有多个来源文件或目录,则最后一个目标文件一定是 “目录”

2.3、取得路径的文件名和目录名称

每个文件的完整文件名包含了前面的目录与最终的文件名,而每个文件名的长度可以达到 255 个字符。那么你怎么知道哪个是文件名?哪个是目录名?就是利用斜线(/)来分辨!其实,取得文件名或者是目录名称,一般的用途应该是在写程序的时候用来判断之用的。指令 basename 和 dirname。

[root@MiWiFi-R4A-srv tmp]# basename /etc/sysconfig/network
network
[root@MiWiFi-R4A-srv tmp]# dirname /etc/sysconfig/network
/etc/sysconfig

3、文件内容查阅

相关指令:

  • cat 由第一行开始显示文件内容
  • tac 从最后一行开始显示,可以看出 tac 是 cat 的倒着写!
  • nl 显示的时候,顺道输出行号
  • more 一页一页地显示文件内容
  • less 与 more 类似,但是比 more 更好的是,它可以往前翻页
  • head 只看头几行
  • tail 只看尾几行
  • od 以二进制的方式读取文件内容

3.1、直接检视文件内容

直接查阅一个文件的内容可以使用 cat/tac/nl 这几个指令。

  • cat(concatenate)

    [root@MiWiFi-R4A-srv ~]# cat [-AbEnTv]
    选项与参数:
    -A	:	相当于 -vET 的整合选项,可列出一些特殊字符而不是空白而已
    -b	:	列出行号,仅针对非空白行做行号显示,空白行不标行号
    -E	:	将结尾的断行字符 $ 显示出来
    -n	:	打印出行号,连同空白行也会有行号,与 -b 的选项不同
    -T	:	将 [tab] 按键以 ^I 显示出来
    -v	:	列出一些看不出来的特殊字符
    
    范例一:检阅 /etc/issue 这个文件的内容
    [root@MiWiFi-R4A-srv ~]# cat /etc/issue
    \S
    Kernel \r on an \m
    
    范例二:承上题,如果还要打印行号呢?
    [root@MiWiFi-R4A-srv ~]# cat -n /etc/issue
         1  \S
         2  Kernel \r on an \m
         3
    
    范例三:将 /etc/man_db.conf 的内容完整地显示出来(包含特殊字符)
    [root@MiWiFi-R4A-srv ~]# cat -A /etc/man_db.conf
    # $
    #$
    # This file is used by the man-db package to configure the man and cat paths.$
    # It is also used to provide a manpath for those without one by examining$
    # their PATH environment variable. For details see the manpath(5) man page.$
    #$
    # Lines beginning with `#' are comments and are ignored. Any combination of$
    # tabs or spaces may be used as `whitespace' separators.$
    #$
    # There are three mappings allowed in this file:$
    # --------------------------------------------------------$
    # MANDATORY_MANPATH^I^I^Imanpath_element$
    # MANPATH_MAP^I^Ipath_element^Imanpath_element$
    # MANDB_MAP^I^Iglobal_manpath^I[relative_catpath]$
    #---------------------------------------------------------$
    # every automatically generated MANPATH includes these fields$
    #$
    #MANDATORY_MANPATH ^I^I^I/usr/src/pvm3/man$
    #$
    MANDATORY_MANPATH^I^I^I/usr/man$
    MANDATORY_MANPATH^I^I^I/usr/share/man$
    MANDATORY_MANPATH^I^I^I/usr/local/share/man$
    ......
    
    # 基本上,在一般环境中,使用 [tab] 与空格键的效果差不多,都是一堆空白。我们无法知道两者的差别。此时使用 cat -A 就能发现那些空白是什么东西了![tab] 会以 ^I 表示,断行符在是以 $ 表示,不过断行符在 Windows/Linux 中不太一样,Windows 中的是 ^M$。
    

    比较少用 cat,因为如果文件行数超过 40 行,根本来不及在屏幕上显示出来!

  • tac(反向列示)

    [root@MiWiFi-R4A-srv ~]# tac /etc/issue
    
    Kernel \r on an \m
    \S
    
  • nl(添加行号打印)

    [root@MiWiFi-R4A-srv ~]# nl [-bnw] 文件
    选项与参数:
    -p	:	指定行号指定的方式,主要有两种:
    		-b a	:	表示不论是否空行,也同样列出行号(类似 cat -n);
    		-b t	:	如果有空行,空的那一行不要列出行号(默认值);
    -n	:	列出行号表示的方法,主要有三种:
    		-n ln	:	行号在屏幕左边显示
    		-n rn	:	行号在自己字段的最右方显示,且不加 0;
    		-n rz	:	行号在自己字段的最右方显示,且加 0;
    -w	:	行号字段的占用字符数
    
    范例一:用 nl 列出 /etc/issue 的内容
    [root@MiWiFi-R4A-srv ~]# nl /etc/issue
         1  \S
         2  Kernel \r on an \m
    
    # 第三行是空白行,所以 nl 不会加上行号。如果要加上行号,可以:
    
    [root@MiWiFi-R4A-srv ~]# nl -b a /etc/issue
         1  \S
         2  Kernel \r on an \m
         3
    # 加上了行号,那么如果想要在行号前面自动补上 0呢?
    
    [root@MiWiFi-R4A-srv ~]# nl -b a -n rz /etc/issue
    000001  \S
    000002  Kernel \r on an \m
    000003
    #默认是六位数,如果想改成 3 位数呢?
    
    [root@MiWiFi-R4A-srv ~]# nl -b a -n rz -w 3 /etc/issue
    001     \S
    002     Kernel \r on an \m
    003
    

3.2、可翻页检视

前面提到的 nl 与 cat,tac 等等,都是一次性地将数据一口气显示到屏幕上,那么有没有可以进行一页一页翻动的?

  • more(一页一页翻动)

    [root@MiWiFi-R4A-srv ~]# more /etc/man_db.conf
    #
    #
    # This file is used by the man-db package to configure the man and cat paths.
    # It is also used to provide a manpath for those without one by examining
    # their PATH environment variable. For details see the manpath(5) man page.
    #
    # Lines beginning with `#' are comments and are ignored. Any combination of
    # tabs or spaces may be used as `whitespace' separators.
    #
    # There are three mappings allowed in this file:
    # --------------------------------------------------------
    # MANDATORY_MANPATH                     manpath_element
    # MANPATH_MAP           path_element    manpath_element
    # MANDB_MAP             global_manpath  [relative_catpath]
    #---------------------------------------------------------
    # every automatically generated MANPATH includes these fields
    #
    #MANDATORY_MANPATH                      /usr/src/pvm3/man
    #
    MANDATORY_MANPATH                       /usr/man
    MANDATORY_MANPATH                       /usr/share/man
    MANDATORY_MANPATH                       /usr/local/share/man
    #---------------------------------------------------------
    # set up PATH to MANPATH mapping
    # ie. what man tree holds man pages for what binary directory.
    #
    #               *PATH*        ->        *MANPATH*
    #
    MANPATH_MAP     /bin                    /usr/share/man
    --More--(21%)	<== 重点在这一行,你的光标会在这里等待你的指令
    

    仔细查看上面的案例,如果 more 后面接的内容行数大于屏幕输出的行数,就会出现类似上面的图示。重点在最后一行,最后一行会显示目前显示的百分比,而且还可以在最后一行输入一些有用的指令。

    • 空格键 : 代表向下翻一页
    • Enter : 代表向下翻 “一行”
    • /字符串 : 代表在这个显示的内容当中,向下搜索 “字符串” 这个关键词
    • :f : 立刻显示出文件名以及目前显示的行数
    • q : 代表立刻离开 more,不再显示该文件内容
    • b 或 [ctrl]-b : 代表往回翻页,不过这动作只对文件有用,对管线无用
    MANPATH_MAP     /bin                    /usr/share/man
    MANPATH_MAP     /usr/bin                /usr/share/man
    MANPATH_MAP     /sbin                   /usr/share/man
    MANPATH_MAP     /usr/sbin               /usr/share/man
    MANPATH_MAP     /usr/local/bin          /usr/local/man
    MANPATH_MAP     /usr/local/bin          /usr/local/share/man
    MANPATH_MAP     /usr/local/sbin         /usr/local/man
    MANPATH_MAP     /usr/local/sbin         /usr/local/share/man
    MANPATH_MAP     /usr/X11R6/bin          /usr/X11R6/man
    MANPATH_MAP     /usr/bin/X11            /usr/X11R6/man
    MANPATH_MAP     /usr/games              /usr/share/man
    MANPATH_MAP     /opt/bin                /opt/man
    MANPATH_MAP     /opt/sbin               /opt/man
    #---------------------------------------------------------
    # For a manpath element to be treated as a system manpath (as most of those
    # above should normally be), it must be mentioned below. Each line may have
    # an optional extra string indicating the catpath associated with the
    # manpath. If no catpath string is used, the catpath will default to the
    # given manpath.
    #
    # You *must* provide all system manpaths, including manpaths for alternate
    # operating systems, locale specific manpaths, and combinations of both, if
    # they exist, otherwise the permissions of the user running man/mandb will
    # be used to manipulate the manual pages. Also, mandb will not initialise
    # the database cache for any manpaths not mentioned below unless explicitly
    # requested to do so.
    #
    # In a per-user configuration file, this directive only controls the
    # location of catpaths and the creation of database caches; it has no effect
    /MANPATH	<== 输入 / 之后,光标会自动跑到最底下一行等待输入!
    
  • less(一页一页翻动)

    [root@MiWiFi-R4A-srv ~]# less /etc/man_db.conf
    #
    #
    # This file is used by the man-db package to configure the man and cat paths.
    # It is also used to provide a manpath for those without one by examining
    # their PATH environment variable. For details see the manpath(5) man page.
    #
    # Lines beginning with `#' are comments and are ignored. Any combination of
    # tabs or spaces may be used as `whitespace' separators.
    #
    # There are three mappings allowed in this file:
    # --------------------------------------------------------
    # MANDATORY_MANPATH                     manpath_element
    # MANPATH_MAP           path_element    manpath_element
    # MANDB_MAP             global_manpath  [relative_catpath]
    #---------------------------------------------------------
    # every automatically generated MANPATH includes these fields
    #
    #MANDATORY_MANPATH                      /usr/src/pvm3/man
    #
    MANDATORY_MANPATH                       /usr/man
    MANDATORY_MANPATH                       /usr/share/man
    MANDATORY_MANPATH                       /usr/local/share/man
    #---------------------------------------------------------
    # set up PATH to MANPATH mapping
    # ie. what man tree holds man pages for what binary directory.
    #
    #               *PATH*        ->        *MANPATH*
    #
    MANPATH_MAP     /bin                    /usr/share/man
    /etc/man_db.conf
    

    可以输入的指令:

    • 空格键 : 向下翻一页
    • [pagedown] : 向下翻一页
    • [pageup] : 向上翻一页
    • /字符串 : 向下搜索 “字符串” 的功能
    • ?字符串 : 向上搜索 “字符串” 的功能
    • n : 重复前一个搜索(与 / 或 ? 有关)
    • N : 反向的重复前一个搜索(与 / 或 ? 有关)
    • g : 前进到这个资料的第一行去
    • G : 前进到这个数据的最后一行(注意大小写)
    • q : 离开 less 这个程序

3.3、资料截取

head 和 tail 都是以 “行” 为单位进行截取的。

  • head(取出前几行)

    [root@MiWiFi-R4A-srv ~]# head [-n number] 文件
    选项与参数:
    -n	:	后面接数字,代表显示几行的意思
    
    [root@MiWiFi-R4A-srv ~]# head /etc/man_db.conf	# 默认只显示前 10 行
    #
    #
    # This file is used by the man-db package to configure the man and cat paths.
    # It is also used to provide a manpath for those without one by examining
    # their PATH environment variable. For details see the manpath(5) man page.
    #
    # Lines beginning with `#' are comments and are ignored. Any combination of
    # tabs or spaces may be used as `whitespace' separators.
    #
    # There are three mappings allowed in this file:
    
    [root@MiWiFi-R4A-srv ~]# head -n 20 /etc/man_db.conf
    #
    #
    # This file is used by the man-db package to configure the man and cat paths.
    # It is also used to provide a manpath for those without one by examining
    # their PATH environment variable. For details see the manpath(5) man page.
    #
    # Lines beginning with `#' are comments and are ignored. Any combination of
    # tabs or spaces may be used as `whitespace' separators.
    #
    # There are three mappings allowed in this file:
    # --------------------------------------------------------
    # MANDATORY_MANPATH                     manpath_element
    # MANPATH_MAP           path_element    manpath_element
    # MANDB_MAP             global_manpath  [relative_catpath]
    #---------------------------------------------------------
    # every automatically generated MANPATH includes these fields
    #
    #MANDATORY_MANPATH                      /usr/src/pvm3/man
    #
    MANDATORY_MANPATH                       /usr/man
    
  • tail(取出后面几行)

    [root@MiWiFi-R4A-srv ~]# tail [-n number] 文件
    选项与参数:
    -n	:	后面接数字,代表显示几行的意思
    -f	:	表示持续侦测后面所接的文件名,要等到按下 [ctrl]-c 才会结束
    
    [root@MiWiFi-R4A-srv ~]# tail /etc/man_db.conf	# 默认情况下,显示最后 10 行
    # formatted for a terminal of the given width, regardless of the width of
    # the terminal actually being used. This should generally be within the
    # range set by MINCATWIDTH and MAXCATWIDTH.
    #
    #CATWIDTH       0
    #
    #---------------------------------------------------------
    # Flags.
    # NOCACHE keeps man from creating cat pages.
    #NOCACHE
    
    [root@MiWiFi-R4A-srv ~]# tail -n 20 /etc/man_db.conf
    #
    #---------------------------------------------------------
    # Range of terminal widths permitted when displaying cat pages. If the
    # terminal falls outside this range, cat pages will not be created (if
    # missing) or displayed.
    #
    #MINCATWIDTH    80
    #MAXCATWIDTH    80
    #
    # If CATWIDTH is set to a non-zero number, cat pages will always be
    # formatted for a terminal of the given width, regardless of the width of
    # the terminal actually being used. This should generally be within the
    # range set by MINCATWIDTH and MAXCATWIDTH.
    #
    #CATWIDTH       0
    #
    #---------------------------------------------------------
    # Flags.
    # NOCACHE keeps man from creating cat pages.
    #NOCACHE
    
    范例一:你不知道 /etc/man_db.conf 有多少行,但只想列出 100 行以后的数据
    [root@MiWiFi-R4A-srv ~]# tail -n +100 /etc/man_db.conf
    #---------------------------------------------------------
    # Section names. Manual sections will be searched in the order listed here;
    # the default is 1, n, l, 8, 3, 0, 2, 5, 4, 9, 6, 7. Multiple SECTION
    # directives may be given for clarity, and will be concatenated together in
    # the expected way.
    # If a particular extension is not in this list (say, 1mh), it will be
    # displayed with the rest of the section it belongs to. The effect of this
    # is that you only need to explicitly list extensions if you want to force a
    # particular order. Sections with extensions should usually be adjacent to
    # their main section (e.g. "1 1mh 8 ...").
    #
    SECTION         1 1p 8 2 3 3p 4 5 6 7 9 0p n l p o 1x 2x 3x 4x 5x 6x 7x 8x
    #
    #---------------------------------------------------------
    # Range of terminal widths permitted when displaying cat pages. If the
    # terminal falls outside this range, cat pages will not be created (if
    # missing) or displayed.
    #
    #MINCATWIDTH    80
    #MAXCATWIDTH    80
    #
    # If CATWIDTH is set to a non-zero number, cat pages will always be
    # formatted for a terminal of the given width, regardless of the width of
    # the terminal actually being used. This should generally be within the
    # range set by MINCATWIDTH and MAXCATWIDTH.
    #
    #CATWIDTH       0
    #
    #---------------------------------------------------------
    # Flags.
    # NOCACHE keeps man from creating cat pages.
    #NOCACHE
    
    范例二:持续侦测 /etc/man_db.conf 的内容
    [root@MiWiFi-R4A-srv ~]# tail -n +100 /etc/man_db.conf
    #---------------------------------------------------------
    # Section names. Manual sections will be searched in the order listed here;
    # the default is 1, n, l, 8, 3, 0, 2, 5, 4, 9, 6, 7. Multiple SECTION
    # directives may be given for clarity, and will be concatenated together in
    # the expected way.
    # If a particular extension is not in this list (say, 1mh), it will be
    # displayed with the rest of the section it belongs to. The effect of this
    # is that you only need to explicitly list extensions if you want to force a
    # particular order. Sections with extensions should usually be adjacent to
    # their main section (e.g. "1 1mh 8 ...").
    #
    SECTION         1 1p 8 2 3 3p 4 5 6 7 9 0p n l p o 1x 2x 3x 4x 5x 6x 7x 8x
    #
    #---------------------------------------------------------
    # Range of terminal widths permitted when displaying cat pages. If the
    # terminal falls outside this range, cat pages will not be created (if
    # missing) or displayed.
    #
    #MINCATWIDTH    80
    #MAXCATWIDTH    80
    #
    # If CATWIDTH is set to a non-zero number, cat pages will always be
    # formatted for a terminal of the given width, regardless of the width of
    # the terminal actually being used. This should generally be within the
    # range set by MINCATWIDTH and MAXCATWIDTH.
    #
    #CATWIDTH       0
    #
    #---------------------------------------------------------
    # Flags.
    # NOCACHE keeps man from creating cat pages.
    #NOCACHE
    
    [root@MiWiFi-R4A-srv ~]# tail -f /etc/man_db.conf
    # formatted for a terminal of the given width, regardless of the width of
    # the terminal actually being used. This should generally be within the
    # range set by MINCATWIDTH and MAXCATWIDTH.
    #
    #CATWIDTH       0
    #
    #---------------------------------------------------------
    # Flags.
    # NOCACHE keeps man from creating cat pages.
    #NOCACHE
    ^C
    [root@MiWiFi-R4A-srv ~]# ^C
    [root@MiWiFi-R4A-srv ~]# tail -f /var/log/messages
    Aug 25 10:32:38 MiWiFi-R4A-srv systemd[1]: Starting Cleanup of Temporary Directories...
    Aug 25 10:32:38 MiWiFi-R4A-srv systemd[1]: systemd-tmpfiles-clean.service: Succeeded.
    Aug 25 10:32:38 MiWiFi-R4A-srv systemd[1]: Started Cleanup of Temporary Directories.
    Aug 25 10:37:38 MiWiFi-R4A-srv systemd[1]: Starting dnf makecache...
    Aug 25 10:37:41 MiWiFi-R4A-srv dnf[9030]: CentOS Linux 8 - AppStream                      7.2 kB/s | 4.3 kB     00:00
    Aug 25 10:37:42 MiWiFi-R4A-srv dnf[9030]: CentOS Linux 8 - BaseOS                         8.6 kB/s | 3.9 kB     00:00
    Aug 25 10:37:43 MiWiFi-R4A-srv dnf[9030]: CentOS Linux 8 - Extras                         2.8 kB/s | 1.5 kB     00:00
    Aug 25 10:37:44 MiWiFi-R4A-srv dnf[9030]: Metadata cache created.
    Aug 25 10:37:44 MiWiFi-R4A-srv systemd[1]: dnf-makecache.service: Succeeded.
    Aug 25 10:37:44 MiWiFi-R4A-srv systemd[1]: Started dnf makecache.
    ^C		<== 按下 [ctrl]-c 之后才会离开
    

3.4、非纯文本文件:od

上面的都是查阅纯文本文件的内容,那么万一我们想要查阅非文本文件,举例来说,例如 /usr/bin/passwd 这个执行文件的内容,又该如何读取呢?事实上,由于执行文件通常都是 binary file,使用上面的指令来读取的时候,确实会产生乱码的情况。

[root@MiWiFi-R4A-srv ~]# od [-t TYPE] 文件
选项与参数:
-t	:	后面可以接各种 “类型(TYPE)” 的输出,例如:
		 a	:	利用默认的字符来输出
		 c	:	使用 ASCII 字符来输出
		 d[size] : 利用十进制(decimal)来输出数据,每个整数占用 size bytes
		 f[size] : 利用浮点数(float)来输出数据,每个数占用 size bytes
		 o[size] : 利用八进制(octal)来输出数据,每个整数占用 size bytes
		 x[size] : 利用十六进制(hexadecimal)来输出数据,每个整数占用 size bytes
范例一:请将 /etc/bin/passwd 的内容使用 ASCII 方式展示
[root@MiWiFi-R4A-srv ~]# od -t c /usr/bin/passwd
0000000 177   E   L   F 002 001 001  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020 003  \0   >  \0 001  \0  \0  \0   0   5  \0  \0  \0  \0  \0  \0
0000040   @  \0  \0  \0  \0  \0  \0  \0 200   {  \0  \0  \0  \0  \0  \0
0000060  \0  \0  \0  \0   @  \0   8  \0  \n  \0   @  \0 037  \0 036  \0
0000100 006  \0  \0  \0 004  \0  \0  \0   @  \0  \0  \0  \0  \0  \0  \0
......

3.5、修改文件时间或新建文件:touch

每个文件在 Linux 底下都会记录许多的时间参数,其实有三个主要的变动时间,那么三个时间的意义是什么?

  • modification time(mtime)

    当文件的 “内容数据” 变更时,就会更新这个时间!

  • status time(ctime)

    当该文件的 “状态” 变更时,就会更新这个时间!

  • access time(atime)

    当 “该文件的内容被取用” 时,就会变更这个读取时间!

[root@MiWiFi-R4A-srv ~]# date; ls -l /etc/man_db.conf ; ls -l --time=atime /etc/man_db.conf ; \
> ls -l --time=ctime /etc/man_db.conf
Wed Aug 25 14:52:06 CST 2021
-rw-r--r--. 1 root root 5165 May 11  2019 /etc/man_db.conf	<==2019/5/11 建立的内容(mtime)
-rw-r--r--. 1 root root 5165 Aug 25 10:28 /etc/man_db.conf	<==2021/8/25 读取过内容(atime)
-rw-r--r--. 1 root root 5165 Aug 10 16:28 /etc/man_db.conf	<==2021/8/10 更新过状态(ctime)

在默认情况下,ls 显示出来的是该文件的 mtime,也就是这个文件的内容上次被更改的时间

文件的时间是很重要的,因为,如果文件的时间误判的话,可能会造成某些程序无法顺利运行。那么万一发现一个文件来自未来,该如何让文件的时间变成 “现在” 的时间呢?

[root@MiWiFi-R4A-srv ~]# touch [-acdmt] 文件
选项与参数:
-a	:	仅修订 access time
-c	:	仅修订文件的时间,若该文件不存在则不建立新文件
-d	:	后面可以接欲修订的日期而不用目前的日期,也可以使用 --date="日期或时间"
-m	:	仅修订 mtime
-t	:	后面可以接欲修订的时间而不用目前的时间,格式为 "YYYYMMDDhhmm"
范例一:建立一个空文件并观察时间
[root@MiWiFi-R4A-srv ~]# cd /tmp
[root@MiWiFi-R4A-srv tmp]# touch testtouch
[root@MiWiFi-R4A-srv tmp]# ls -l testtouch
-rw-r--r--. 1 root root 0 Aug 25 14:59 testtouch
# 在默认情况下,如果 touch 后面接有文件,则该文件的三个时间(mtime、ctime、atime)都会更新为目前的时间。若文件不存在,则会主动建立一个新的空文件。

范例二:将 ~/.bashrc 复制成为 bashrc,假设复制完全的属性,检查日期
[root@MiWiFi-R4A-srv tmp]# cp -a ~/.bashrc bashrc
[root@MiWiFi-R4A-srv tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Aug 25 15:02:31 CST 2021
-rw-r--r--. 1 root root 176 May 11  2019 bashrc		<== mtime
-rw-r--r--. 1 root root 176 Aug 24 14:52 bashrc		<== atime
-rw-r--r--. 1 root root 176 Aug 25 15:01 bashrc		<== ctime

可以看到,数据的内容和属性是被复制过来的,因此文件内容时间(mtime)与原本文件相同。但是由于这个文件是刚刚被建立的,因此状态(ctime)就变成了现在的时间。那如果你想要变更这个文件的时间呢?

范例三:修订案例二的 bashrc 文件,将日期调整为两天前
[root@MiWiFi-R4A-srv tmp]# touch -d "2 days ago" bashrc
[root@MiWiFi-R4A-srv tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Aug 25 15:06:03 CST 2021
-rw-r--r--. 1 root root 176 Aug 23 15:06 bashrc		<== mtime 变成了 23
-rw-r--r--. 1 root root 176 Aug 23 15:06 bashrc		<== atime 变成了 23
-rw-r--r--. 1 root root 176 Aug 25 15:06 bashrc		<== ctime 没有改变

范例四:将上个示例的 bashrc 日期改为 2014/06/15 2:02
[root@MiWiFi-R4A-srv tmp]# touch -t 201406150202 bashrc
[root@MiWiFi-R4A-srv tmp]# date; ll bashrc; ll --time=atime bashrc; ll --time=ctime bashrc
Wed Aug 25 15:09:02 CST 2021
-rw-r--r--. 1 root root 176 Jun 15  2014 bashrc		<== mtime 变成了 2014/06/15 2:02
-rw-r--r--. 1 root root 176 Jun 15  2014 bashrc		<== atime 变成了 2014/06/15 2:02
-rw-r--r--. 1 root root 176 Aug 25 15:09 bashrc		<== ctime 变成了 现在的时间

从上面可以看出,即使我们复制一个文件,复制其所有的属性,也没办法复制 ctime 这个属性

touch 这个指令最常被使用的情况是:

  • 建立一个新的空文件;
  • 将某个文件日期修订为目前(mtime 和 atime)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值