浅谈Linux文件系统

Linux操作系统中有一句话叫做“一切皆文件”,接下来我们就用一篇文章研究下Linux的文件系统!

1 文件系统概述

Linux文件系统中的文件是数据的集合,文件系统不仅包含着文件中的数据而且还有文件系统的结构,所有Linux 用户和程序看到的文件、目录、软连接及文件保护信息等都存储在其中。

在Linux中普通文件和目录文件保存在称为块物理设备的磁盘或者磁带上。一套Linux系统支持若干物理盘,每个物理盘可定义一个或者多个文件系统。每个文件系统由逻辑块的序列组成,一个逻辑盘空间一般划分为几个用途各不相同的部分,即引导块、超级块、inode区以及数据区等。

  • 引导块:在文件系统的开头,通常为一个扇区,其中存放引导程序,用于读入并启动操作系统;
  • 超级块:用于记录文件系统的管理信息。特定的文件系统定义了特定的超级块;
  • inode区(索引节点):一个文件或目录占据一个索引节点。第一个索引节点是该文件系统的根节点。利用根节点,可以把一个文件系统挂在另一个文件系统的非叶节点上;
  • 数据区:用于存放文件数据或者管理数据。

以上内容来自百度百科:https://baike.baidu.com/item/Linux%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F/10986747

2 Linux中文件系统层级关系

[root@bogon ~]# cd /
[root@bogon /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
2.1 一般模式下:

在这里插入图片描述

2.2 多用户模式下:

在这里插入图片描述

2.3 几个常见目录的作用
  • /:根目录位于目录结构的最顶层
  • /bin:目录又称为二进制目录,包含了那些供系统管理员和普通用户使用的重要linux命令的二进制映像
  • /boot:目录存放系统核心文件以及启动时必须读取的文件
  • /dev:目录保存着外部设备代码的文件
  • /etc:目录是整个Linux系统的中心,其中包含所有系统管理和维护方面的配置文件
  • /lib:目录下存放必要的运行库,主要是编程语言的库
  • /opt:目录用来安装附加软件包
  • /root:超级用户root的主目录
  • /sbin:放了该目录启动系统时需执行的程序,如管理工具、应用软件和通用的根用户权限命令等内容
  • /usr:这是个最庞大的目录,我们要用到的很多应用程序和文件几乎都存放在这个目录了
  • /var :用于存放很多不断变化的文件,例如日志文件等。包含了日志文件、计划性任务和邮件等内容。

3 常用的文件操作

3.1 创建文件
3.1.1 命令

touch [文件名]

3.1.2 案例演示
[root@iZ1608aqb7ntn9Z TestFile]# touch file01
[root@iZ1608aqb7ntn9Z TestFile]# ls
file01
3.1.3 常用参数
  • touch -t [时间戳] [文件名] :创建并指定文件的时间戳
  • touch -r [目标文件] [源文件] :将两个文件的时间戳设置为相同
[root@iZ1608aqb7ntn9Z TestFile]# touch -t 201810121230 file-2
[root@iZ1608aqb7ntn9Z TestFile]# ll
总用量 8
-rw-r--r-- 1 root root  0 1012 2018 file-2
-rw-r--r-- 1 root root 12 818 14:22 myFile01
-rw-r--r-- 1 root root 12 818 14:37 myFile02
[root@iZ1608aqb7ntn9Z TestFile]# touch -r myFile01 file-2
[root@iZ1608aqb7ntn9Z TestFile]# ll
总用量 8
-rw-r--r-- 1 root root  0 818 14:22 file-2
-rw-r--r-- 1 root root 12 818 14:22 myFile01
-rw-r--r-- 1 root root 12 818 14:37 myFile02
3.2 查看文件/文件列表
3.2.1 命令

查看文件列表:ls

查看文件内容:cat、vi、vim

3.2.2 案例演示

查看文件列表:

[root@iZ1608aqb7ntn9Z TestFile]# ls
file01

查看文件内容:

[root@iZ1608aqb7ntn9Z TestFile]# echo Hello World  > file01  # 先将内容写入文件
[root@iZ1608aqb7ntn9Z TestFile]# cat file01   #cat查看文件
Hello World
[root@iZ1608aqb7ntn9Z TestFile]# vi file01 
Hello World
                                                                  
"file01" 1L, 12C
[root@iZ1608aqb7ntn9Z TestFile]# vim file01 
Hello World

"file01" 1L, 12C

注意:vim命令如果有一下报错

[root@iZ1608aqb7ntn9Z TestFile]# vim file01 
-bash: /usr/bin/vim: 没有那个文件或目录

解决方式(需要联网哦):

[root@iZ1608aqb7ntn9Z TestFile]# yum -y install vim
3.2.3 常用参数

查看文件列表:

  • ls -l :查看文件以列表的形式
  • ls -a :查看全部文件,包括隐藏文件
  • ls -d : 查看当前文件夹下目录
[root@iZ1608aqb7ntn9Z TestFile]# ls -a
.  ..  file-2  myFile01  myFile02
[root@iZ1608aqb7ntn9Z TestFile]# ls -l
总用量 8
-rw-r--r-- 1 root root  0 818 14:22 file-2
-rw-r--r-- 1 root root 12 818 14:22 myFile01
-rw-r--r-- 1 root root 12 818 14:37 myFile02
[root@iZ1608aqb7ntn9Z TestFile]# ls -d
.

扩展:

ls -l 命令等价与ll命令,都可以以列表形式查看文件,但是在不同发行版本的Linux系统中ll命令无法使用,其次,文件列表中-rw-r--r--的含义如下:
在这里插入图片描述

3.3 修改文件
3.3.1 命令

mv [源文件] [目标文件/目录]

3.3.2 案例演示
[root@iZ1608aqb7ntn9Z TestFile]# ls
file-2  myFile01  myFile02
[root@iZ1608aqb7ntn9Z TestFile]# mv file-2 myFile03
[root@iZ1608aqb7ntn9Z TestFile]# ls
myFile01  myFile02  myFile03
3.3.3 常用参数
  • mv -f [源文件] [目标文件] :覆盖前不询问
  • mv -i [源文件] [目标文件] : 覆盖前询问
  • mv -t [源文件] [目标目录] :将所有源文件参数移动到目录中
3.4 复制文件
3.4.1 命令

cp [源文件] [目标文件]

3.4.2 案例演示
[root@iZ1608aqb7ntn9Z TestFile]# cp myFile01 myFile02
[root@iZ1608aqb7ntn9Z TestFile]# ls
myFile01  myFile02
3.4.3 常用参数
  • cp -n [源文件] [目标文件]: 不要覆盖已存在的文件
  • cp -P [源文件] [目标文件]:不跟随源文件中的符号链接
3.5 删除文件
3.5.1 命令

rm [文件名]

3.5.2 案例演示
[root@iZ1608aqb7ntn9Z TestFile]# ls
myFile01  myFile02  myFile03
[root@iZ1608aqb7ntn9Z TestFile]# rm myFile03
rm:是否删除普通空文件 'myFile03'?y
[root@iZ1608aqb7ntn9Z TestFile]# ls
myFile01  myFile02
3.5.3 常用参数
  • rm -r [文件或目录名]:可删除目录
  • rm -f [文件名]:删除前不询问

4 小Tips

如果大家有什么命令的参数不了解的话,Linux提供了很简便直白的帮助文档,使用[命令] --help就能进行查看,举个例子:

[root@iZ1608aqb7ntn9Z TestFile]# ls --help
用法:ls [选项]... [文件]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

必选参数对长短选项同时适用。
  -a, --all                     不隐藏任何以. 开始的项目
  -A, --almost-all              列出除. 及.. 以外的任何项目
      --author                  与-l 同时使用时列出每个文件的作者
  -b, --escape                  以八进制溢出序列表示不可打印的字符
      --block-size=SIZE      with -l, scale sizes by SIZE when printing them;
                               e.g., '--block-size=M'; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                            类似-l,但不列出所有者
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --hyperlink[=WHEN]     hyperlink file names; WHEN can be 'always'
                               (default if omitted), 'auto', or 'never'
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage;
                               used only with -s and per directory totals
  -l                            使用较长格式列出信息
  -L, --dereference             当显示符号链接的文件信息时,显示符号链接所指示
                                的对象而并非符号链接本身的信息
  -m                            所有项目以逗号分隔,并填满整行行宽
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print entry names without quoting
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
                               (overrides QUOTING_STYLE environment variable)
  -r, --reverse                 逆序排列
  -R, --recursive               递归显示子目录
  -s, --size                    以块数形式显示每个文件分配的尺寸
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u);
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time (newest first)
      --time-style=TIME_STYLE  time/date format with -l; see TIME_STYLE below
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid '\n' with -q or -b
      --help            显示此帮助信息并退出
      --version         显示版本信息并退出
......
  • 5
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
iptables 是 Linux 系统中的一个强大的防火墙工具,它可以用于过滤、重定向和修改网络数据包。它是一个基于内核的软件,可以对数据包进行过滤和修改,从而实现网络连接的控制和管理。在 Linux 系统中,iptables 是最常用的防火墙软件之一,它可以在多个层面上进行过滤和管理网络连接。以下是 iptables 在 Linux 系统中的一些应用: 1. 防火墙:iptables 可以配置防火墙规则,保护系统免受来自外部网络的攻击。它可以限制不同网络之间的数据流量,并允许或阻止特定的端口和协议。 2. NAT:iptables 可以实现网络地址转换(NAT),它可以将私有 IP 地址转换为公共 IP 地址以实现 Internet 访问。通过 NAT,可以将多个设备连接到 Internet,而不需要每个设备都拥有公共 IP 地址。 3. 端口转发:iptables 可以将进入系统的数据包重定向到其他系统或端口。这是在服务器环境中非常有用的,因为它可以将外部流量重定向到内部服务器。 4. 流量限制:iptables 可以限制流量,这对于限制恶意用户或应用程序的带宽使用非常有用。它可以限制连接的数量、速率和带宽,从而保证网络的稳定性和可靠性。 总之,iptables 是 Linux 系统中非常有用的一个工具,可以用于保护网络安全、管理网络连接和实现网络地址转换等功能。它的功能非常强大,但也需要用户具备一定的技术水平和经验,才能正确配置和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海风极客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值