【Linux运维基础】(一)硬链接与软连接+一些实用的文件操作命令

前言

笔者日常的工作比较琐碎,接下来估计是偏测试和运维方向的,所以借写这个系列的文章夯实一下Linux基础。

硬链接与软链接

是什么

ln命令用于链接操作,其帮助说明节选如下:

shell 复制代码
Create hard links by default, symbolic links with --symbolic.
By default, each destination (name of new link) should not already exist.
When creating hard links, each TARGET must exist.  Symbolic links
can hold arbitrary text; if later resolved, a relative link is
interpreted in relation to its parent directory.

Mandatory arguments to long options are mandatory for short options too.
      --backup[=CONTROL]      make a backup of each existing destination file
  -b                          like --backup but does not accept an argument
  -d, -F, --directory         allow the superuser to attempt to hard link
                                directories (note: will probably fail due to
                                system restrictions, even for the superuser)
  -f, --force                 remove existing destination files
  -i, --interactive           prompt whether to remove destinations
  -L, --logical               dereference TARGETs that are symbolic links
  -n, --no-dereference        treat LINK_NAME as a normal file if
                                it is a symbolic link to a directory
  -P, --physical              make hard links directly to symbolic links
  -r, --relative              create symbolic links relative to link location
  -s, --symbolic              make symbolic links instead of hard links
  -S, --suffix=SUFFIX         override the usual backup suffix
  -t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create
                                the links
  -T, --no-target-directory   treat LINK_NAME as a normal file always
  -v, --verbose               print name of each linked file
      --help     display this help and exit
      --version  output version information and exit

默认创建的是硬链接,相连的文件实际是同一个文件,它们的大小、inode编号都一模一样:

shell 复制代码
//cmd
ln hello.txt helloplus.txt
//result
ls -l hello*
-rw-rw-r-- 2 julia julia 13 Nov 23 11:18 helloplus.txt
-rw-rw-r-- 2 julia julia 13 Nov 23 11:18 hello.txt

//cmd
ls -i hello*
//result
786909 helloplus.txt  786909 hello.txt

如果加上-s参数(symbolic),就是符号链接(软链接),链接起来的两个文件是不同的,笔者试了一下,改了源文件hello.txt之后,软链接的文件内容也对应更新,但是文件大小和编号是独立的:

shell 复制代码
//cmd
ls -i hello*
//result
786838 helloplus_soft.txt  786909 helloplus.txt  786909 hello.txt

//cmd
ls -l hello*
//result
lrwxrwxrwx 1 julia julia  9 Nov 23 11:20 helloplus_soft.txt -> hello.txt
-rw-rw-r-- 2 julia julia 17 Nov 23 11:24 helloplus.txt
-rw-rw-r-- 2 julia julia 17 Nov 23 11:24 hello.txt

该软链接文件大小为9个字节,这也许是一个放之四海而皆准的数值。

怎么用

首先要明确一个注意事项:

硬链接不能跨文件系统(存储设备),软链接则可以。

  • 硬链接像是给文件取个别名、在不同文件目录都方便地访问该文件

适用于普通文件,经常直接改动

  • 软链接像是给文件创建一个快捷方式。

适用于希望保留源文件属性的情况(较重要,不希望随意就修改)

file命令

arduino 复制代码
//cmd
file hello*
//result
helloplus_soft.txt: symbolic link to hello.txt
helloplus.txt:      ASCII text
hello.txt:          ASCII text

如上所示,这个命令可以深入文件,判断它是什么类型的,链接到哪里,采用何种方式等等。

如果是未知的二进制文件,也可以用它来辅助判断一下属性。这里以bin目录下的zip工具为例:

bash 复制代码
//cmd
file zip
//result
zip: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=xxxxxxx, stripped

more和less

相信大家对cat 这个命令不陌生了,但是如果要读取的文本过长,用cat 难免不方便。而more 可以以分页的方式辅助阅读: 按空格键翻页,回车键下一行,q键退出。


less 则有了更强大的功能(因为less is more)。 它的首要特点是 ,对于大文件的读取也许比vi/vim还快,因为它是边读取边显示的。

vbnet 复制代码
//man less节选
Less  is  a program similar to more (1), but it has many more features.  Less
       does not have to read the entire input file before starting,  so  with  large
       input  files  it  starts  up faster than text editors like vi (1).

它还支持上下翻页、文本搜索等:

  • pagedown\]: 向下翻动一页

  • /字符串:向下搜索"字符串"的功能

  • ?字符串:向上搜索"字符串"的功能

tail 和 head

有查看全部文件,就会有只查看部分文件,tailhead就是这样的例子。

顾名思义,tail是指读取文件末尾部分的文件,而head则是指读取文件开头部分的文件。可通过-n参数指定行数,也可以省略"n",直接用如下表示:

bash 复制代码
//cmd
tail -3 hello.txt
//result
hello world!
hello world!
hello world!

既然能一次性查看完全部文件,为什么会有只查看部分文件的需求呢?这就要提到tail的重要特性:在文件被其他进程使用时,tail -f依然能查看该文件,并且同步输出文件的最新内容。一个经典的应用场景是实时输出监控日志

参考资料

  • 《Linux命令行与shell脚本编程大全》(第四版) Richaed Blum等著
相关推荐
肥大毛15 小时前
C++入门学习---结构体
开发语言·c++·学习
qq_4017004116 小时前
Linux文件锁解决多进程并发
linux·服务器·算法
南棱笑笑生16 小时前
20251213给飞凌OK3588-C开发板适配Rockchip原厂的Buildroot【linux-6.1】系统时适配type-C0
linux·c语言·开发语言·rockchip
小猪猪屁16 小时前
顺序表与链表:头插法与尾插法详解
c语言·数据结构·c++
历程里程碑17 小时前
C++ 5:模板初阶
c语言·开发语言·数据结构·c++·算法
RisunJan17 小时前
Linux命令-gpasswd命令(管理用户组的重要工具)
linux·运维·服务器
where happens17 小时前
centos创建目录并授予权限
linux·运维·服务器·centos
dllmayday17 小时前
Qt/QML + C++ 双向数据绑定(MVVM 模式的几种常用方法(ChatGPT)
开发语言·c++·qt
好好学操作系统17 小时前
autodl 保存 数据 跨区
linux·运维·服务器
dbitc18 小时前
WIN11把WSL2移动安装目录
linux·运维·ubuntu·wsl