【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: 向下翻动一页

  • pageup: 向上翻动一页

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

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

tail 和 head

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

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

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

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

参考资料

  • 《Linux命令行与shell脚本编程大全》(第四版) Richaed Blum等著
相关推荐
悠仁さん2 小时前
【C++】AVL 树
c++
wuminyu2 小时前
JVM规范学习总结:解释器与JIT编译器如何处理JVM字节码
java·linux·c语言·jvm·c++
小徐不徐说3 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang3 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
IMPYLH3 小时前
Linux 的时间为什么会被拨回去
linux·运维·服务器
充值内卷4 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
叼烟扛炮5 小时前
[Git 实战] 学习笔记:从生成 SSH 密钥到解决 TortoiseGit 推送报错全指南
linux·运维·笔记·git·学习
xbzb5 小时前
OpenStack 网络管理(理论):Linux 网络虚拟化是 Neutron 的地基
linux·服务器·网络·openstack·network·neutron
Android系统攻城狮7 小时前
Linux FFmpeg深度解析之avcodec_flush_buffers调用流程与实战(十九)
linux·运维·服务器·ffmpeg·车载音视频·ffmpeg音视频进阶
樱花树下的数学喵7 小时前
手写简易 STL string|C++ 字符串底层实现
开发语言·c++