前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux文件管理

Linux文件管理

原创
作者头像
鱼丸葱面
修改2020-12-23 11:37:12
3.9K0
修改2020-12-23 11:37:12
举报
文章被收录于专栏:ShadowLinuxer

Linux文件管理

Linux目录结构和文件操作基础

文件系统目录结构

  • 文件和目录被组织成一个单根倒置树结构
  • 文件系统从根目录下开始,用“/”表示
  • 根文件系统:root filesystem
  • 标准linux文件系统ext4
  • 以.开头的文件为隐藏文件
  • 路径分隔的/
  • 文件名最长255字节
  • 蓝色表示目录 绿色表示可行文件 红色表示压缩文件 浅蓝色表示链接文件 灰色表示其他文件
  • 除斜杠和NUL,所有字符都有效,但使用特殊字符的目录名和文件不推荐使用,有些字符需要用引号来引用
  • 每个文件都有两类相关数据:元数据metada(也就是所说的文件属性)和数据data(文件内容)

文件的元数据有文件的类型、大小、节点号inode,权限,所有者,所属组,时间等

代码语言:text
复制
[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 变化数据 类似日志

Linux系统文件类型

  • -普通文件
  • d目录文件
  • c字符设备
  • l符号链接文件
  • p管道文件pipe
  • s套接字文件socket

范例:

代码语言:text
复制
[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,只取路径,不要文件名

代码语言:text
复制
[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返回家目录

代码语言:text
复制
[root@centos7 sysconfig]# cd  
[root@centos7 ~]#  

cd ~用户名 切换至用户家目录

代码语言:text
复制
[root@centos7 ~]# cd ~canyon 
[root@centos7 canyon]#  

cd -快速切换至前一个目录

代码语言:text
复制
[root@centos7 canyon]# cd - /root 
[root@centos7 ~]#  

列出目录内容

ls命令列出当前目录的内容或者指定目录的内容

用法为:

代码语言:text
复制
[root@centos7 ~]# ls --help Usage: ls [OPTION]... [FILE]... 

常见选项:

  • -a包含隐藏文件
代码语言:text
复制
查看隐藏文件
[root@centos7 ~]#  ls -a 
.  ..   
  • -l显示额外信息
代码语言:text
复制
[root@centos7 ~]# ls -l 
total 4 -rw-------. 1 root root 1667 Dec  3 09:59 anaconda.cfg 
  • -R显示目录递归
代码语言:text
复制
[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
  • -ld显示目录和符号链接信息
代码语言:text
复制
[root@centos7 ~]# ls -ld 
dr-xr-x---. 3 root root 144 Dec  3 21:26 . 
  • -1文件分行显示
代码语言:text
复制
[root@centos7 dir2]# ls -1
filec.txt
filex.txt
filex.txt.link
  • -S按从大到小排序
代码语言:text
复制
[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

  • -t按mtime排序
代码语言:text
复制
[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

  • -u配合-t选项,显示按atime从新到旧排序
代码语言:text
复制
[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
  • -U按目录存放顺序显示
  • -X按文件后缀排序

ls查看不同后缀文件时显示的不同颜色是由/etc/DIR_COLORS和@LS_COLORS变量定义

代码语言:text
复制
[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
.....

文件的三个时间TIME

Mtime modify time修改时间,是指文件在被修改后的时间

Ctime change time变化时间,是指文件属性(例如用户、大小、权限等)变化后的时间

Atime access time访问时间,是指访问文件时的时间

代码语言:text
复制
[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命令来查看文件类型,然后确定适当的打开命令或应用程序

格式:

代码语言:text
复制
[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)

代码语言:text
复制
[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

windows文件和linux文件区别

分别在window系统和linux系统中分别创建一个文本文件,内容为abc

查看两个文件的相关信息,发现windows文件比linux文件大1个字节

代码语言:text
复制
[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的标识

代码语言:text
复制
[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

代码语言:text
复制
[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代表为回车

代码语言:text
复制
       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,该工具需要安装

代码语言:text
复制
[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工具

代码语言:text
复制
[root@centos7 ~]# yum -y install dos2unix 

使用转换工具进行文件格式转换

代码语言:text
复制
[root@centos7 data]# dos2unix windows.txt  
dos2unix: converting file windows.txt to Unix format ... 

转换后发现原先windows文件字节已经变小

代码语言:text
复制
[root@centos7 data]# ll -i 
windows.txt  69 -rw-r--r--. 1 root root 5 Dec  6 21:16 windows.txt 

windows文件的CRLF的标识已经没有了

代码语言:text
复制
[root@centos7 data]# file windows.txt  windows.txt: ASCII text 

文件管理基本命令

文件通配符模式

文件通配符可以用来匹配复合条件的多个文件,方便批处理管理文件

通配符才有特定的符号,表示特定的含义,此符号成为元字符

* 表示匹配零个或多个字符,但不匹配.开头的文件即隐藏文件

代码语言: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

?表示匹配任何单个字符

代码语言:text
复制
[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

~当前用户家目录

代码语言:text
复制
[root@centos7 data]# cd ~ 
[root@centos7 ~]#  

~mage用户mage家目录

代码语言:text
复制
[root@centos7 ~]# cd ~canyon 
[root@centos7 canyon]#  

~+和.当前工作目录

~-前一个工作目录

代码语言:text
复制
[root@centos7 data]# cd ~-
[root@centos7 ~]# 

[0-9]匹配数字范围

代码语言:text
复制
[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]字母

代码语言:text
复制
[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]字母

代码语言:javascript
复制
[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]匹配列表中的任何的一个字符

代码语言:text
复制
[root@centos7 ~]# ls /data/file[canyon].txt
/data/filec.txt  /data/filen.txt  /data/fileo.txt  /data/filey.txt

[^canyon]匹配列表中的所有字符以外的字符

代码语言:text
复制
[root@centos7 ~]# ls /data/file[^canyon].txt
/data/file0.txt  /data/file7.txt  /data/fileD.txt  /data/fileh.txt  

只显示文件夹

代码语言:text
复制
[root@centos7 ~]# ls -d /data/*/
/data/dir1/  /data/dir2/

创建空文件和刷新时间

代码语言:text
复制
[root@centos7 data]# touch --help 
Usage: touch [OPTION]... FILE... 

-a 仅改变atime和ctime

-m 仅改变mtime和ctime

-t 指定atime和mtime的时间戳

使用touch命令来创建文件时指定时间

代码语言:text
复制
[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

常用选项

  • -i 覆盖前提示是否覆盖
  • -n 不覆盖,注意两者顺序
  • -r,-R 递归复制目录及内部的所有内容
  • -a 归档,相当于-dR --preserv=all 常用于备份功能
  • -d --no-dereference --preserv=links 不复制原文件,只复制链接名
  • --preserv[=ATTR_LIST]

mode权限 ownership属主属组 timestamp时间戳

ownership属主属组

timestamp

  • -p 保留文件属性
代码语言:text
复制
[root@centos7 data]# cp /etc/issue /tmp/ 
[root@centos7 data]# ls /tmp/issue  /tmp/issue 

ll !*将上一个命令的所有参数进行调用

移动和重命名文件

mv可以实现文件或目录的移动和改名

同一分区移动数据,速度很快:数据位置没有变化

不同分区移动数据,速度相对慢:数据位置发生了变化

格式:

代码语言:text
复制
[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批量实现修改文件名:

代码语言:text
复制
[root@centos7 ~]# rename --help 
Usage:  rename [options] expression replacement file... 

示例:

先批量创建文件

代码语言:text
复制
[root@centos7 data]# touch file{a..z}.txt 
代码语言:text
复制
[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文件进行重命名

代码语言:text
复制
[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命令可以实现文件及文件夹的删除

代码语言:text
复制
[root@centos7 data]# rm --help 
Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s). 

常用选项:

  • -f, --force 强制删除ignore nonexistent files and arguments, never prompt
  • -i 交互式prompt before every removal
  • -r, -R, --recursive 递归remove directories and their contents recursively
  • -d, --dir 删除空目录 remove empty directories
代码语言:text
复制
[root@centos7 data]# rm -rf fileb.txt  

默认rm命令是别名,删除文件时会进行交互确认

代码语言:text
复制
[root@centos7 data]# rm filea.txt  
rm: remove regular empty file ‘filea.txt’? y 
[root@centos7 data]# alias rm alias rm='rm -i' 

删除文件夹

代码语言:text
复制
[root@centos7 data]# mkdir test
[root@centos7 data]# \rm -r test/

生产环境中尽量不要使用rm,养成将rm重命名避免使用rm导致误操作,通过使用mv进行文件的整理

代码语言:text
复制
[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 

目录操作

显示目录树tree

常见选项

  • -d只显示目录List directories only.
  • -L level指定显示的层级数目Follow symbolic links like directories.
  • -P pattern只显示由指定wild-card pattern匹配到的路径List only those files that match the pattern given.

创建目录mkdir

代码语言:text
复制
[root@centos7 ~]# mkdir --help 
Usage: mkdir [OPTION]... DIRECTORY... 

常见选项:

  • -m, --mode=MODE 创建目录时指定权限set file mode (as in chmod), not a=rwx - umask
  • -p, --parents 不报错切自动创建所需目录no error if existing, make parent directories as needed
  • -v, --verbose 显示详细信息print a message for each created directory

删除空目录

代码语言:text
复制
[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

代码语言:text
复制
[root@centos7 data]# rmdir dir1
[root@centos7 data]# ls dir1
ls: cannot access dir1: No such file or directory

inode表结构和文件管理底层原理

inode表结构

每个文件的属性信息,例如文件的大小,时间,类型等,称为文件的元数据(meta data)。这些元数据是存放在node(index node)表中。node表中有很多条记录组成,第一条记录对应的存放了一个文件的元数据信息。

第一个node表记录对应保存以下信息:

  • inode number节点号 由系统自动分配
代码语言:text
复制
[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: 
  • 文件类型
  • 权限
  • UID
  • GID
  • 链接数(指向这个文件名路径名称个数)
  • 该文件的大小和不同的时间戳
  • 指向磁盘上文件的数据块指针(直接指针最多有12个,最多48K)
  • 有关文件的其他数据

目录

目录是一个特殊文件,目录文件的内容保存了目录文件的表及inode number对应关系

代码语言:text
复制
[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: -

节点编号是有限的是可以用尽的

代码语言:text
复制
[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节点数使用情况。

如果有大文件需要清除如何来做?

先测试创建大文件命令

代码语言:text
复制
[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

代码语言:text
复制
[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

再次查看磁盘空间使用情况

代码语言:text
复制
[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,这个是将文件内容清除但是文件没有被删除,应用是可以继续使用的。

代码语言:text
复制
[root@centos7 test]# ll /boot/ 
total 98976 -rw-r--r--. 1 root root        0 Dec  8 22:08 big 

硬链接

硬链接本质上就给一个文件起一个新的名称,实质是同一个文件

硬件连接特性

  • 创建硬链接会在对应的目录中增加额外的记录项以引用文件
  • 对于同一文件系统上一个物理文件
  • 每个目录引用相同的inode号
代码语言:text
复制
[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

  • 创建时链接数递增
代码语言:text
复制
[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: -

  • 删除文件时:rm命令递减计数的链接,文件要存在,至少有一个链接数,当链接数为零时,该文件被删除
代码语言:text
复制
[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: -
  • 不能跨越驱动器或分区
代码语言:text
复制
[root@centos7 data]# ln filed.txt /opt/filed.txt 
ln: failed to create hard link ‘/opt/filed.txt’ => ‘filed.txt’: Invalid cross-device link 

用法格式:

代码语言:text
复制
[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中快捷方式,软链接文件和原文件本质上是不同一个文件

软链接特点:

  • 一个符号链接的内容是它引用文件的名称
  • 可以对目录创建软链接
  • 可以跨分区的文件实现
  • 指向的是另一个文件的路径;其大小为指向的路径字符串的长度;不增加或减少目标文件inode的引用计数。
  • 软链接如果使用相对路径,是相对于原文件的路径,而非相对于当前目录

使用格式

-s, --symbolic make symbolic links instead of hard links

[root@centos7 dir2]# ln -s filex.txt filex.txt.link

查看创建的filex软链接和原文件的inode节点编号等信息是不一样的,也就是软链接不是同一文件

代码语言:text
复制
[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 

查看软链接内容与真实文件内容一致

代码语言:text
复制
[root@centos7 dir2]# cat filex.txt.link  adf adf adf afd asdf 
[root@centos7 dir2]# cat filex.txt adf adf adf afd asdf 

硬链接和软链接区别

  • 硬链接本质是同一个文件,而软链接不是同一文件(快捷方式和原文件)
代码语言:text
复制
[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 
  • 硬链接不支持跨设备创建,而软链接支持
代码语言:text
复制
[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
  • 硬链接的inode节点号一致,软链接inode节点号不同
  • 硬链接在创建新链接后,链接数会增加,删除硬链接后链接数会减少,而软链接创建删除后,链接数不会变化。
  • 硬链接不支持对文件夹创建,而软链接支持。
代码语言:text
复制
[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
  • 硬链接的原始文件相对路径相对于当前工作目录,软链接的原始文件的相对路径是相对于链接文件的相对路径。
  • 硬链接在删除源文件后只是链接数减一,但链接文件访问不受影响,但是软链接在删除源文件后链接文件将无法访问。
代码语言:text
复制
[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 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • Linux文件管理
    • 文件系统目录结构
      • 常见文件系统目录功能
        • Linux系统文件类型
          • 文件操作命令
            • 显示当前工作目录
            • 更改工作目录
            • 列出目录内容
            • 文件的三个时间TIME
            • 确定文件内容
          • windows文件和linux文件区别
            • 文件管理基本命令
              • 文件通配符模式
              • 创建空文件和刷新时间
              • 复制文件和目录
              • 移动和重命名文件
              • 删除文件
            • 目录操作
              • 显示目录树tree
              • 创建目录mkdir
              • 删除空目录
            • inode表结构和文件管理底层原理
              • inode表结构
            • 硬链接
              • 硬件连接特性
            • 符号(或软)链接
              • 软链接特点:
              • 硬链接和软链接区别
          相关产品与服务
          文件存储
          文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档