【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

有查看全部文件,就会有只查看部分文件,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等著
相关推荐
刚入门的大一新生6 小时前
Linux-Linux第一个系统程序-进度条
linux·运维·服务器
wuminyu6 小时前
JDK21解决虚拟线程IO阻塞原理剖析
java·linux·c语言·jvm·c++
MC皮蛋侠客6 小时前
TDengine C++ 系列(4):时序查询——窗口聚合、插值、连接与时序函数
大数据·c++·tdengine
junuo6 小时前
10_C++内存分区与动态内存管理
c++·编程语言
茫忙然6 小时前
Linux提权辅助查询工具——BinLens的安装和使用
linux·提权·binlens
FlightYe6 小时前
linux系统编程(八):阻塞与非阻塞IO对比
android·linux·运维·服务器·github·vim
欧特克_Glodon6 小时前
OpenCV计算机视觉开发入门与实践<二>:图像噪声与图像处理
c++·图像处理·opencv·计算机视觉
键盘会跳舞6 小时前
C++:函数对象与 std::function 源码级深度拆解——泛型算法的策略内核与可调用对象统一封装
c++·算法·仿函数
Augustzero7 小时前
为什么高性能调度器都在“偷任务”?从无锁队列看懂工作窃取
c++·后端
(Charon)7 小时前
【C++】线程安全队列(三):MPSC无锁队列、atomic与链表节点实现
c++·安全·链表