2025年Linux常用文件管理命令(下)

Linux常用文件管理命令(下)11 head 与 tail 命令 head 命令 用来显示文件开头部分内容 默认显示文件开头 10 行内容 命令格式 head 选项 文件 常用操作 行数 指定显示的行数 root localhost head etc passwd root localhost 行数

大家好,我是讯享网,很高兴认识大家。

11. head与tail命令

head命令:用来显示文件开头部分内容,默认显示文件开头10行内容

  • 命令格式:head [选项] 文件
  • 常用操作:
    • -<行数> 指定显示的行数
[root@localhost ~]# head /etc/passwd [root@localhost ~]# head /etc/fstab [root@localhost ~]# head /etc/group [root@localhost ~]# head /etc/hostname [root@localhost ~]# head /etc/hosts [root@localhost ~]# head /etc/sysconfig/network-scripts/ifcfg-ens32 ​ #查看存放DNS配置文件信息 [root@localhost ~]# head /etc/resolv.conf ​ #使用-n指定显示文件前多少行内容 [root@localhost ~]# head -n 5 /etc/passwd [root@localhost ~]# head -n 6 /etc/passwd [root@localhost ~]# head -n 15 /etc/passwd [root@localhost ~]# head -n 20 /etc/passwd

讯享网

  • tail命令:用来显示文件末尾部分内容,默认显示文件末尾10行内容
  • 命令格式:tail [选项] 参数
  • 常用操作:
  • -<行数> 显示指定的行数
  • 常用选项:
    • -f 动态显示
讯享网[root@localhost ~]# tail /etc/passwd ​ #使用“-n”指定显示文件末尾多少行内容 [root@localhost ~]# tail -n 5 /etc/passwd [root@localhost ~]# tail -n 5 /etc/sysconfig/network-scripts/ifcfg-ens32 IPADDR="192.168.0.50" PREFIX="24" GATEWAY="192.168.0.254" DNS1="114.114.114.114" IPV6_PRIVACY="no" ​ #动态查看文件内容 [root@localhost ~]# touch t1 root@localhost ~]# tail -f t1 ​ #另开一个终端向文件写入内容 [root@localhost ~]# echo 123 > t1

12. rm删除命令

rm(英文全拼:remove)命令用于删除文件或者目录。


讯享网

  • 命令格式:rm [-选项…] 目录或文件…
  • 常用选项
    • -f 强制删除
    • -r 删除目录
    • *特殊字符:系统常用符号,用来代表任意所有字符
[root@localhost ~]# ls /opt abc abc1 abc2 abc3 anaconda-ks.cfg hello.txt home rh student t1 t2 t3 t4 xx xxoo ​ [root@localhost ~]# ls /mnt hello home oooo student1 t1 t2 t3 t4 test test1 test2 test3 ​ #删除指定目录下文件 [root@localhost ~]# rm /opt/anaconda-ks.cfg rm:是否删除普通文件 "/opt/anaconda-ks.cfg"?y  #默认需要确认(y|n) ​ #查看文件是否被成功删除 [root@localhost ~]# ls /opt abc abc1 abc2 abc3 hello.txt home rh student t1 t2 t3 t4 xx xxoo ​ [root@localhost ~]# rm /opt/hello.txt rm:是否删除普通空文件 "/opt/hello.txt"?y ​ #同时删除目录下指定文件 [root@localhost ~]# rm /opt/t1 /opt/t2 /opt/t3 /opt/t4 rm:是否删除普通空文件 "/opt/t1"?y rm:是否删除普通空文件 "/opt/t2"?y rm:是否删除普通空文件 "/opt/t3"?y rm:是否删除普通空文件 "/opt/t4"?y ​ #查看文件是否被成功删除 [root@localhost ~]# ls /opt abc abc1 abc2 abc3 home rh student xx xxoo ​ #使用“-f”强制删除文件(无需确认,直接删除) [root@localhost ~]# rm -f /mnt/hello [root@localhost ~]# ls /mnt home oooo student1 t1 t2 t3 t4 test test1 test2 test3 ​ #同时强制删除多个文件 [root@localhost ~]# rm -f /mnt/t1 /mnt/t2 /mnt/t3 /mnt/t4 [root@localhost ~]# ls /mnt ​ #删除目录 [root@localhost ~]# rm -r /opt/abc rm:是否删除目录 "/opt/abc"?y ​ [root@localhost ~]# ls /opt abc1 abc2 abc3 home rh student xx xxoo ​ #同时删除多个目录 [root@localhost ~]# rm -r /opt/abc1 /opt/abc2 /opt/abc3 rm:是否删除目录 "/opt/abc1"?y rm:是否删除目录 "/opt/abc2"?y rm:是否删除目录 "/opt/abc3"?y ​ [root@localhost ~]# ls /opt home rh student xx xxoo ​ #同时强制删除多个目录 [root@localhost ~]# rm -rf /opt/home /opt/student /opt/xx /opt/xxoo [root@localhost ~]# ls /opt rh ​ #创建目录与文件 [root@localhost ~]# touch /opt/t1 [root@localhost ~]# mkdir /opt/test [root@localhost ~]# ls /opt rh t1 test ​ #rm命令在删除目录时,包含改目录及目录下所有数据全部删除 [root@localhost ~]# rm -rf /opt/ [root@localhost ~]# ls / ​ [root@localhost ~]# ls /mnt home oooo student1 test test1 test2 test3 ​ #使用“*”通配任意所有字符,删除/mnt目录下所有数据 [root@localhost ~]# rm -rf /mnt/* [root@localhost ~]# ls /mnt

13. 软连接与硬连接

Linux中的链接文件类似于windows中的快捷方式

软链接特点:可以跨分区,可以对目录进行链接,源文件删除后,链接文件不可用

  • 软链接命令格式:ln -s 源文件路径 目标路径

注意:创建链接文件时一定要写绝对路径,哪怕是在当前路径下,也要写绝对路径·

讯享网[root@localhost ~]# touch hello.soft [root@localhost ~]# ls ​ #创建软连接(必须要绝对路径创建) [root@localhost ~]# ln -s /root/hello.soft /opt [root@localhost ~]# ls /opt ​ #查看连接文件详细属性 [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月  21 14:28 /opt/hello.soft -> /root/hello.soft #提示:链接文件的权限最终取决于源文件的权限 ​ #普通用户验证 [lisi@localhost ~]$ ls /opt hello.soft [lisi@localhost ~]$ ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月  21 14:28 /opt/hello.soft -> /root/hello.soft [lisi@localhost ~]$ cat /opt/hello.soft cat: /opt/hello.soft: 权限不够 #提示:由于源文件存放于/root目录下,而普通用户对/root目录没有任何权限,所以普通用户无法查看 ​ #删除源文件 [root@localhost ~]# rm -f /root/hello.soft [root@localhost ~]# ls ​ #山粗源文件后,软链接文件不可用 [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月  21 14:28 /opt/hello.soft -> /root/hello.soft ​ #创建文件并创建软连接 [root@localhost ~]# touch hello.soft [root@localhost ~]# ln -s /root/hello.soft /opt ​ [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 16 3月  21 14:39 /opt/hello.soft -> /root/hello.soft ​ #删除链接文件后,源文件仍然可用 [root@localhost ~]# rm -f /opt/hello.soft [root@localhost ~]# ls [root@localhost ~]# cat hello.soft ​ #对目录创建软连接 [root@localhost ~]# ln -s /root/test1 /opt/ ​ [root@localhost ~]# ls -ld /opt/test1 lrwxrwxrwx. 1 root root 11 3月  21 14:44 /opt/test1 -> /root/test1 ​ 3创建链接时一定要写目录或文件的绝对路径,哪怕是在当前路径下,也要写绝对路径 [root@localhost ~]# ln -s hello.soft /opt [root@localhost ~]# ls /opt hello.soft test1 [root@localhost ~]# ls -l /opt/hello.soft lrwxrwxrwx. 1 root root 10 3月  21 14:47 /opt/hello.soft -> hello.soft

硬链接特点:硬连接不可以跨分区,不可以对目录进行链接,源文件删除后,链接文件仍然可用(类似与对文件做了一个备份)

  • 硬链接命令格式:ln 源文件路径 目标路径
#创建文件,并创建硬连接 [root@localhost ~]# touch hello.hard [root@localhost ~]# ln /root/hello.hard /opt/ [root@localhost ~]# ls /opt hello.hard hello.soft test1 ​ #向硬连接的源文件写入内容 root@localhost ~]# echo 123 > /root/hello.hard ​ #查看源文件内容 [root@localhost ~]# cat /root/hello.hard 123 ​ #查看链接文件内容,以同步更新 [root@localhost ~]# cat /opt/hello.hard 123 ​ #向链接文件写入内容,查看源文件以同步更新 [root@localhost ~]# echo xx >> /opt/hello.hard ​ #擦看源文件,以同步更新 [root@localhost ~]# cat /root/hello.hard 123 xx ​ #硬连接文件的特点可以保持文件属性不发生改变 [root@localhost ~]# ls -l /root/hello.hard -rw-r--r--. 2 root root 7 3月  21 14:55 /root/hello.hard [root@localhost ~]# ls -l /opt/hello.hard -rw-r--r--. 2 root root 7 3月  21 14:55 /opt/hello.hard ​ #并且硬连接文件的i节点号相同 [root@localhost ~]# ls -i /root/hello.hard  /root/hello.hard [root@localhost ~]# ls -i /opt/hello.hard  /opt/hello.hard ​ #硬连接不允许对目录进行连接 root@localhost ~]# ln /root/test1 /opt ln: "/root/test1": 不允许将硬链接指向目录 ​ #硬连接源文件删除后,链接文件仍然可用 [root@localhost ~]# rm -f /root/hello.hard [root@localhost ~]# cat /opt/hello.hard 123 xx ​ #向硬连接文件写入内容 [root@localhost ~]# echo abc >> /opt/hello.hard [root@localhost ~]# cat /opt/hello.hard 123 xx abc ​ #硬连接不允许跨分区 [root@localhost ~]# lsblk NAME           MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda               8:0    0   20G  0 disk ├─sda1            8:1    0   1G  0 part /boot └─sda2            8:2    0   19G  0 part ├─centos-root 253:0    0   17G  0 lvm / └─centos-swap 253:1    0   2G  0 lvm [SWAP] sr0              11:0    1  4.3G  0 rom   [root@localhost ~]# ln /root/hello.soft /boot ln: 无法创建硬链接"/boot/hello.soft" => "/root/hello.soft": 无效的跨设备连接

小讯
上一篇 2025-03-25 13:57
下一篇 2025-03-04 13:38

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/28603.html