Linux目录结构和文件操作基础
文件的元数据有文件的类型、大小、节点号inode,权限,所有者,所属组,时间等
[root@centos7 data]# stat data1.txt
File: ‘data1.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 104 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-12-06 22:40:26.300255829 +0800
Modify: 2020-12-06 22:40:26.300255829 +0800
Change: 2020-12-06 22:40:26.300255829 +0800
Birth: -
/ 根目录
/bin -> usr/bin 存放二进制程序
/data 安装自定义
/etc 配置文件
/lib -> usr/lib 软件库
/media 外围设备挂载点
/misc 挂载访问光盘 访问时才启用
/opt 第三方软件
/root root用户家目录
/sbin -> usr/sbin 管理员运行的可执行程序
/sys 硬件相关
/usr 相当于Windows的program file
/boot 存放启动文件
/dev 硬件设备
/home 用户家目录
/lib64 -> usr/lib64 软件库
/mnt 外围设备挂载点
/proc 虚拟文件夹 对应内存数据
/run 正在运行中的相关文件
/srv 系统上运行的服务用到的数据
/tmp 临时文件存储
/var 变化数据 类似日志
范例:
[root@centos7 data]# ls -l /dev/
total 0
crw-rw----. 1 root video 10, 175 Dec 3 10:01 agpgart 字符设备
crw-------. 1 root root 10, 235 Dec 3 10:01 autofs
drwxr-xr-x. 2 root root 180 Dec 3 10:01 block 目录文件
drwxr-xr-x. 2 root root 80 Dec 3 10:01 bsg
crw-------. 1 root root 10, 234 Dec 3 10:01 btrfs-control
drwxr-xr-x. 3 root root 60 Dec 3 10:01 bus
lrwxrwxrwx. 1 root root 3 Dec 6 20:00 cdrom -> sr0 符号链接文件
drwxr-xr-x. 2 root root 3140 Dec 9 21:04 cha
srw-rw-rw-. 1 root root 0 Dec 3 10:01 log 套接字文件
pwd
绝对路径和相对路径
绝对路径:以正斜杠开始,完整的文件的路径位置,可用于任何想制定一个文件名的时候
相对路径:不以斜线开始,制定相对于当前工作目录活某目录的位置,可以作为一个简短的星矢制定一个文件名
基名:basename,只取文件名而 不要路径
目录名:dirname,只取路径,不要文件名
[root@centos7 ~]# basename /etc/sysconfig/network-scripts/ifcfg-ens33
ifcfg-ens33
[root@centos7 ~]# dirname /etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts
cd返回家目录
[root@centos7 sysconfig]# cd
[root@centos7 ~]#
cd ~用户名 切换至用户家目录
[root@centos7 ~]# cd ~canyon
[root@centos7 canyon]#
cd -快速切换至前一个目录
[root@centos7 canyon]# cd - /root
[root@centos7 ~]#
ls命令列出当前目录的内容或者指定目录的内容
用法为:
[root@centos7 ~]# ls --help Usage: ls [OPTION]... [FILE]...
常见选项:
查看隐藏文件
[root@centos7 ~]# ls -a
. ..
[root@centos7 ~]# ls -l
total 4 -rw-------. 1 root root 1667 Dec 3 09:59 anaconda.cfg
[root@centos7 ~]# ls -R /data/ 通过-R选项可查看目录下面所有包含目录递归信息
/data/:
data1.txt file1.txt file6.txt filed.txt filei.txt filen.txt files.txt filex.txt test2.txt
dir1 file2.txt file7.txt filee.txt filej.txt fileo.txt filet.txt filey.txt test3.txt
dir2 file3.txt file8.txt filef.txt filek.txt filep.txt fileu.txt filez.txt window1.txt
file0.txt file4.txt file9.txt fileg.txt filel.txt fileq.txt filev.txt linux.txt windows.txt
file10.txt file5.txt filec.txt fileh.txt filem.txt filer.txt filew.txt test1.txt windows.txt.link
/data/dir1:
/data/dir2:
filec.txt filex.txt filex.txt.link
[root@centos7 ~]# ls -ld
dr-xr-x---. 3 root root 144 Dec 3 21:26 .
[root@centos7 dir2]# ls -1
filec.txt
filex.txt
filex.txt.link
[root@centos7 dir2]# ls -1lS
total 4
-rw-r--r--. 1 root root 22 Dec 6 14:17 filex.txt
lrwxrwxrwx. 1 root root 9 Dec 6 14:18 filex.txt.link -> filex.txt
-rw-r--r--. 2 root root 0 Dec 6 13:09 filec.txt
[root@centos7 dir2]# ls -1lt
total 4
lrwxrwxrwx. 1 root root 9 Dec 6 14:18 filex.txt.link -> filex.txt
-rw-r--r--. 1 root root 22 Dec 6 14:17 filex.txt
-rw-r--r--. 2 root root 0 Dec 6 13:09 filec.txt
[root@centos7 dir2]# ls -lut
total 4
lrwxrwxrwx. 1 root root 9 Dec 9 22:00 filex.txt.link -> filex.txt
-rw-r--r--. 2 root root 0 Dec 8 20:52 filec.txt
-rw-r--r--. 1 root root 22 Dec 6 14:22 filex.txt
ls查看不同后缀文件时显示的不同颜色是由/etc/DIR_COLORS和@LS_COLORS变量定义
[root@centos7 data]# cat /etc/DIR_COLORS
# Configuration file for the color ls utility
# Synchronized with coreutils 8.5 dircolors
# This file goes in the /etc directory, and must be world readable.
# You can copy this file to .dir_colors in your $HOME directory to override
# the system defaults.
# COLOR needs one of these arguments: 'tty' colorizes output to ttys, but not
# pipes. 'all' adds color characters to all output. 'none' shuts colorization
# off.
COLOR tty
.....
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# executables (bright green)
#.cmd 01;32
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
#.sh 01;32
#.csh 01;32
# archives or compressed (bright red)
.tar 01;31
.tgz 01;31
.arc 01;31
.arj 01;31
.taz 01;31
.lha 01;31
.lz4 01;31
.lzh 01;31
.lzma 01;31
.tlz 01;31
.txz 01;31
.tzo 01;31
.t7z 01;31
.zip 01;31
.....
Mtime modify time修改时间,是指文件在被修改后的时间
Ctime change time变化时间,是指文件属性(例如用户、大小、权限等)变化后的时间
Atime access time访问时间,是指访问文件时的时间
[root@centos7 ~]# stat anaconda.cfg
File: ‘anaconda.cfg’
Size: 1667 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 201326659 Links: 1
Access: (0600/-rw-------) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:admin_home_t:s0
Access: 2020-12-03 10:46:22.225037212 +0800
Modify: 2020-12-03 09:59:05.258007487 +0800
Change: 2020-12-03 21:26:22.989049456 +0800
Birth: -
目前文件主要分为两类:文本与非文本,通过file命令来查看文件类型,然后确定适当的打开命令或应用程序
格式:
[root@centos7 ~]# file --help
Usage: file [OPTION...] [FILE...]
常用选项:
-b, --brief 不显示文件名 do not prepend filenames to output lines
-f, --files-from FILE 列出文件filelist中文件名的文件类型 read the filenames to be examined from FILE
-F, --separator STRING 使用指定分隔符号替换输出文件名后默认的“:”分隔符 use string as separator instead of `:'
-L, --dereference 查看对应软链接对应文件的文件类型 follow symlinks (default)
[root@centos7 ~]# file /boot/
/boot/: directory
[root@centos7 ~]# file /boot/vmlinuz-3.10.0-957.el7.x86_64
/boot/vmlinuz-3.10.0-957.el7.x86_64: Linux kernel x86 boot executable bzImage, version 3.10.0-957.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) #1 S, RO-rootFS, swap_dev 0x6, Normal VGA
分别在window系统和linux系统中分别创建一个文本文件,内容为abc
查看两个文件的相关信息,发现windows文件比linux文件大1个字节
[root@centos7 data]# ll -i linux.txt
93 -rw-r--r--. 1 root root 6 Dec 6 20:46 linux.txt
[root@centos7 data]# ll -i windows.txt
69 -rw-r--r--. 2 root root 5 Dec 6 21:16 windows.txt
通过file查看文件类型,windows文件会有CRLF的标识
[root@centos7 data]# file windows.txt
windows.txt: ASCII text, with CRLF line terminators
[root@centos7 data]# file linux.txt
linux.txt: ASCII text
通过hexdump命令来查看二进制文件的16进制编码,发现windows文件的多了1个0d
[root@centos7 data]# hexdump -C windows.txt
00000000 61 0d 0a 62 0d 0a 63 |a..b..c|
00000007
[root@centos7 data]# hexdump -C linux.txt
00000000 61 0a 62 0a 63 0a |a.b.c.|
00000006
通过查看ascii表,0d代表为回车
Oct Dec Hex Char Oct Dec Hex Cha
────────────────────────────────────────────────────────────────────────
000 0 00 NUL '\0' 100 64 40 @
001 1 01 SOH (start of heading) 101 65 41 A
002 2 02 STX (start of text) 102 66 42 B
003 3 03 ETX (end of text) 103 67 43 C
004 4 04 EOT (end of transmission) 104 68 44 D
005 5 05 ENQ (enquiry) 105 69 45 E
006 6 06 ACK (acknowledge) 106 70 46 F
007 7 07 BEL '\a' (bell) 107 71 47 G
010 8 08 BS '\b' (backspace) 110 72 48 H
011 9 09 HT '\t' (horizontal tab) 111 73 49 I
012 10 0A LF '\n' (new line) 112 74 4A J
013 11 0B VT '\v' (vertical tab) 113 75 4B K
014 12 0C FF '\f' (form feed) 114 76 4C L
015 13 0D CR '\r' (carriage ret) 115 77 4D M
所以通过对比发现windows文件默认为回车换行标识一行的结束,而linux中只有换行符来标识。
在linux中提供可以将windows文件转换成linux文件的工具,dos2unix,该工具需要安装
[root@centos7 data]# yum search dos2unix
Loaded plugins: fastestmirro
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
============================================ N/S matched: dos2unix ============================================
dos2unix.x86_64 : Text file format converters
Name and summary matches only, use "search all" for everything.
通过yum安装下dos2unix工具
[root@centos7 ~]# yum -y install dos2unix
使用转换工具进行文件格式转换
[root@centos7 data]# dos2unix windows.txt
dos2unix: converting file windows.txt to Unix format ...
转换后发现原先windows文件字节已经变小
[root@centos7 data]# ll -i
windows.txt 69 -rw-r--r--. 1 root root 5 Dec 6 21:16 windows.txt
windows文件的CRLF的标识已经没有了
[root@centos7 data]# file windows.txt windows.txt: ASCII text
文件通配符可以用来匹配复合条件的多个文件,方便批处理管理文件
通配符才有特定的符号,表示特定的含义,此符号成为元字符
* 表示匹配零个或多个字符,但不匹配.开头的文件即隐藏文件
[root@centos7 data]# ll file*
-rw-r--r--. 1 root root 0 Dec 6 21:29 file0.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file10.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file1.txt
?表示匹配任何单个字符
[root@centos7 data]# ll file?.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file0.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file1.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file2.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file3.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file4.txt
~当前用户家目录
[root@centos7 data]# cd ~
[root@centos7 ~]#
~mage用户mage家目录
[root@centos7 ~]# cd ~canyon
[root@centos7 canyon]#
~+和.当前工作目录
~-前一个工作目录
[root@centos7 data]# cd ~-
[root@centos7 ~]#
[0-9]匹配数字范围
[root@centos7 data]# ll file[0-9].txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file0.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file1.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file2.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file3.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file4.txt
-rw-r--r--. 1 root root 0 Dec 6 21:29 file5.txt
[a-z]字母
[root@centos7 ~]# ll /data/file[a-d].txt
-rw-r--r--. 2 root root 0 Dec 6 13:09 /data/filec.txt
-rw-r--r--. 1 root root 0 Dec 3 21:29 /data/filed.txt
[A-Z]字母
[root@centos7 ~]# ll /data/file[A-D].txt
-rw-r--r--. 1 root root 0 Dec 9 22:29 /data/fileA.txt
-rw-r--r--. 1 root root 0 Dec 9 22:29 /data/fileB.txt
-rw-r--r--. 2 root root 0 Dec 6 13:09 /data/filec.txt
-rw-r--r--. 1 root root 0 Dec 9 22:29 /data/fileC.txt
-rw-r--r--. 1 root root 0 Dec 3 21:29 /data/filed.txt
-rw-r--r--. 1 root root 0 Dec 9 22:29 /data/fileD.txt
[canyon]匹配列表中的任何的一个字符
[root@centos7 ~]# ls /data/file[canyon].txt
/data/filec.txt /data/filen.txt /data/fileo.txt /data/filey.txt
[^canyon]匹配列表中的所有字符以外的字符
[root@centos7 ~]# ls /data/file[^canyon].txt
/data/file0.txt /data/file7.txt /data/fileD.txt /data/fileh.txt
只显示文件夹
[root@centos7 ~]# ls -d /data/*/
/data/dir1/ /data/dir2/
[root@centos7 data]# touch --help
Usage: touch [OPTION]... FILE...
-a 仅改变atime和ctime
-m 仅改变mtime和ctime
-t 指定atime和mtime的时间戳
使用touch命令来创建文件时指定时间
[root@centos7 data]# touch -t202012062255.30 test3.txt
[root@centos7 data]# stat file3.txt
File: ‘file3.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 96 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-12-06 21:29:49.190197855 +0800
Modify: 2020-12-06 21:29:49.190197855 +0800
Change: 2020-12-06 21:29:49.190197855 +0800
Birth: -
-c 如果文件不存在,则不予创建
利用cp命令可以实现文件或目录的复制
cp --help
常用选项
mode权限 ownership属主属组 timestamp时间戳
ownership属主属组
timestamp
[root@centos7 data]# cp /etc/issue /tmp/
[root@centos7 data]# ls /tmp/issue /tmp/issue
ll !*将上一个命令的所有参数进行调用
mv可以实现文件或目录的移动和改名
同一分区移动数据,速度很快:数据位置没有变化
不同分区移动数据,速度相对慢:数据位置发生了变化
格式:
[root@centos7 ~]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST or: mv [OPTION]... SOURCE... DIRECTORY or: mv [OPTION]... -t DIRECTORY SOURCE...
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-f, --force 强制移动 do not prompt before overwriting
-i, --interactive 交互式 prompt before overwrite
-n, --no-clobber do not overwrite an existing file
[root@centos7 ~]# mv anaconda-ks.cfg /opt/
[root@centos7 ~]# ls /opt/ anaconda-ks.cfg
rename批量实现修改文件名:
[root@centos7 ~]# rename --help
Usage: rename [options] expression replacement file...
示例:
先批量创建文件
[root@centos7 data]# touch file{a..z}.txt
[root@centos7 data]# ls file[a-z].txt
fileA.txt filed.txt filef.txt fileh.txt filel.txt filep.txt filet.txt filex.txt
fileB.txt fileD.txt fileF.txt filei.txt filem.txt fileq.txt fileu.txt filey.txt
filec.txt filee.txt fileg.txt filej.txt filen.txt filer.txt filev.txt filez.txt
fileC.txt fileE.txt fileG.txt filek.txt fileo.txt files.txt filew.txt
对log文件进行重命名
[root@centos7 data]# rename txt log *
[root@centos7 data]# ls file[a-z].log
fileA.log filed.log filef.log fileh.log filel.log filep.log filet.log filex.log
fileB.log fileD.log fileF.log filei.log filem.log fileq.log fileu.log filey.log
filec.log filee.log fileg.log filej.log filen.log filer.log filev.log filez.
通过rm命令可以实现文件及文件夹的删除
[root@centos7 data]# rm --help
Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s).
常用选项:
[root@centos7 data]# rm -rf fileb.txt
默认rm命令是别名,删除文件时会进行交互确认
[root@centos7 data]# rm filea.txt
rm: remove regular empty file ‘filea.txt’? y
[root@centos7 data]# alias rm alias rm='rm -i'
删除文件夹
[root@centos7 data]# mkdir test
[root@centos7 data]# \rm -r test/
生产环境中尽量不要使用rm,养成将rm重命名避免使用rm导致误操作,通过使用mv进行文件的整理
[root@centos7 ~]# which rm alias rm='rm -i' /usr/bin/rm
[root@centos7 ~]# mv /usr/bin/rm /usr/bin/rm.bak
[root@centos7 ~]# rm -f /
-bash: /usr/bin/rm: No such file or directory
[root@centos7 ~]# rm -f /data/filec.txt
-bash: /usr/bin/rm: No such file or directory
常见选项
[root@centos7 ~]# mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
常见选项:
[root@centos7 ~]# rmdir --help
Usage: rmdir [OPTION]... DIRECTORY...
常见选项
-p, --parents 递归删除父空目录remove DIRECTORY and its ancestors; e.g., 'rmdir -p a/b/c' is
similar to 'rmdir a/b/c a/b a'
-v, --verbose 显示详细信息output a diagnostic for every directory processed
[root@centos7 data]# rmdir dir1
[root@centos7 data]# ls dir1
ls: cannot access dir1: No such file or directory
每个文件的属性信息,例如文件的大小,时间,类型等,称为文件的元数据(meta data)。这些元数据是存放在node(index node)表中。node表中有很多条记录组成,第一条记录对应的存放了一个文件的元数据信息。
第一个node表记录对应保存以下信息:
[root@centos7 data]# ll -i file6.txt
99 -rw-r--r--. 1 root root 0 Dec 6 21:29 file6.txt
[root@centos7 data]# ll -i file6.log
99 -rw-r--r--. 1 root root 0 Dec 6 21:29 file6.log
[root@centos7 data]# stat file6.log
File: ‘file6.log’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 99 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-12-06 21:29:49.190197855 +0800
Modify: 2020-12-06 21:29:49.190197855 +0800
Change: 2020-12-09 22:37:01.830087733 +0800
Birth:
目录
目录是一个特殊文件,目录文件的内容保存了目录文件的表及inode number对应关系
[root@centos7 data]# stat dir1
File: ‘dir1’
Size: 6 Blocks: 0 IO Block: 4096 directory
Device: 803h/2051d Inode: 100663360 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-12-09 22:42:12.300091981 +0800
Modify: 2020-12-09 22:42:12.300091981 +0800
Change: 2020-12-09 22:42:12.300091981 +0800
Birth: -
节点编号是有限的是可以用尽的
[root@centos7 data]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda2 52428800 37912 52390888 1% /
devtmpfs 230184 399 229785 1% /dev
tmpfs 232906 1 232905 1% /dev/shm
tmpfs 232906 735 232171 1% /run
tmpfs 232906 16 232890 1% /sys/fs/cgroup
/dev/sda3 26214400 57 26214343 1% /data
/dev/sda1 524288 332 523956 1% /boot
tmpfs 232906 1 232905 1% /run/user/0
/dev/sr0 0 0 0 - /mnt
Inodes指总共的节点数,IUsed指已使用的inode节点数,IFree指剩余可用inode节点数。
案例:提示空间满No Space left on device ,但是df可以看到空间很多,为什么?
通过dh -h查看空间很多,也有可能是节点数inode用光导致系统提示空间已满可以通过df -i来查看inode节点数使用情况。
如果有大文件需要清除如何来做?
先测试创建大文件命令
[root@centos7 test]# dd if=/dev/zero of=/boot/big bs=1M count=700
700+0 records in 700+0 records out 734003200 bytes (734 MB) copied, 5.51636 s, 133 MB/s
[root@centos7 data]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 1.6G 99G 2% /
devtmpfs 900M 0 900M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda3 50G 33M 50G 1% /data
/dev/sda1 1014M 838M 177M 83% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/sr0 56M 56M 0 100% /mnt
查看创建的文件big
[root@centos7 test]# ll /boot/
total 815776 -rw-r--r--. 1 root root 734003200 Dec 8 22:05 big
如果将big大文件清空如何来做?可以通过重定向来实现
通过使用cat命令将/dev/null(null类似回收站、黑洞)重定向至/boot/big文件中
[root@centos7 test]# cat /dev/null > /boot/big
再次查看磁盘空间使用情况
[root@centos7 data]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 100G 1.6G 99G 2% /
devtmpfs 900M 0 900M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda3 50G 33M 50G 1% /data
/dev/sda1 1014M 138M 877M 14% /boot
tmpfs 182M 0 182M 0% /run/user/0
/dev/sr0 56M 56M 0 100% /mnt
boot下的big文件已经清0,这个是将文件内容清除但是文件没有被删除,应用是可以继续使用的。
[root@centos7 test]# ll /boot/
total 98976 -rw-r--r--. 1 root root 0 Dec 8 22:08 big
硬链接本质上就给一个文件起一个新的名称,实质是同一个文件
[root@centos7 data]# ln filef.log dir1/filef.log
[root@centos7 data]# ll -i !*
ll -i filef.log dir1/filef.log
72 -rw-r--r--. 2 root root 0 Dec 3 21:29 dir1/filef.log
72 -rw-r--r--. 2 root root 0 Dec 3 21:29 filef.log
[root@centos7 data]# stat filef.log
File: ‘filef.log’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 72 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-12-03 21:29:01.725051628 +0800
Modify: 2020-12-03 21:29:01.725051628 +0800
Change: 2020-12-09 22:49:03.074097601 +0800
Birth: -
[root@centos7 data]# rm.bak -f dir1/filef.log
[root@centos7 data]# stat filef.log
File: ‘filef.log’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 803h/2051d Inode: 72 Links: 1 文件删除一个链接数变为1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:etc_runtime_t:s0
Access: 2020-12-03 21:29:01.725051628 +0800
Modify: 2020-12-03 21:29:01.725051628 +0800
Change: 2020-12-09 22:52:08.873100143 +0800
Birth: -
[root@centos7 data]# ln filed.txt /opt/filed.txt
ln: failed to create hard link ‘/opt/filed.txt’ => ‘filed.txt’: Invalid cross-device link
用法格式:
[root@centos7 dir2]# ln --help
Usage: ln [OPTION]... [-T] TARGET LINK_NAME
(1st form) or: ln [OPTION]... TARGET
(2nd form) or: ln [OPTION]... TARGET... DIRECTORY
(3rd form) or: ln [OPTION]... -t DIRECTORY TARGET...
一个符号链接指向另一个文件,就像windows中快捷方式,软链接文件和原文件本质上是不同一个文件
使用格式
-s, --symbolic make symbolic links instead of hard links
[root@centos7 dir2]# ln -s filex.txt filex.txt.link
查看创建的filex软链接和原文件的inode节点编号等信息是不一样的,也就是软链接不是同一文件
[root@centos7 dir2]# ll -i
total 4
67 -rw-r--r--. 2 root root 0 Dec 6 13:09 filec.txt
67160131 -rw-r--r--. 1 root root 22 Dec 6 14:17 filex.txt
67160129 lrwxrwxrwx. 1 root root 9 Dec 6 14:18 filex.txt.link -> filex.txt
查看软链接内容与真实文件内容一致
[root@centos7 dir2]# cat filex.txt.link adf adf adf afd asdf
[root@centos7 dir2]# cat filex.txt adf adf adf afd asdf
[root@centos7 data]# ln windows.txt window1.txt
[root@centos7 data]# ll -i window*.txt
69 -rw-r--r--. 2 root root 5 Dec 6 21:16 window1.txt
69 -rw-r--r--. 2 root root 5 Dec 6 21:16 windows.txt
[root@centos7 data]# cat window1.txt a b c
[root@centos7 data]# cat windows.txt a b c
[root@centos7 data]#
[root@centos7 data]# ln -s windows.txt windows.txt.link
[root@centos7 data]# ll -i | grep wind
69 -rw-r--r--. 2 root root 5 Dec 6 21:16 window1.txt
69 -rw-r--r--. 2 root root 5 Dec 6 21:16 windows.txt
107 lrwxrwxrwx. 1 root root 11 Dec 9 21:09 windows.txt.link -> windows.txt
[root@centos7 data]# ln windows.txt /opt/windows.txt
ln: failed to create hard link ‘/opt/windows.txt’ => ‘windows.txt’: Invalid cross-device link
[root@centos7 data]# ln -s windows.txt /opt/windows.txt
[root@centos7 data]# ll -i /opt/
total 0
135007746 lrwxrwxrwx. 1 root root 11 Dec 9 22:59 windows.txt -> windows.txt
[root@centos7 data]# ln dir1 dir1test
ln: ‘dir1’: hard link not allowed for directory
[root@centos7 data]# ll -i
total 16
100663360 drwxr-xr-x. 2 root root 6 Dec 9 22:52 dir1
117 lrwxrwxrwx. 1 root root 4 Dec 9 23:03 dir1test -> dir1
[root@centos7 data]# rm.bak -f windows.txt
[root@centos7 data]# catm windows.test.txt
-bash: catm: command not found
[root@centos7 data]# cat windows.test.txt
a
b
c
[root@centos7 data]# ll -i windows.txt.link
107 lrwxrwxrwx. 1 root root 11 Dec 9 21:09 windows.txt.link -> windows.txt
[root@centos7 data]# cat windows.txt.link
cat: windows.txt.link: No such file or directory
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。