Linux文件管理

                 Linux文件管理

                                           作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

 

一.文件系统结构元素

1>.文件系统

  文件和目录被组织成一个单根倒置树结构 

  文件系统从根目录下开始,用“/”表示

  根文件系统(rootfs):root filesystem

  文件名称区分大小写

  以.开头的文件为隐藏文件

  路径分隔的符为:"/"

  文件有两类数据:
    元数据:metadata
    数据:data
  
  文件系统分层结构:
    LSB Linux Standard Base

  FHS: (Filesystem Hierarchy Standard)
    官方文档:http://www.pathname.com/fhs/

2>.文件的命名规则

  文件名最长255个字节 
  包括路径在内文件名称最长4095个字节
  文件类型类型Linux用不同的类型进行颜色划分,默认为:  
    蓝色-->目录
    绿色-->可执行文件
    红色-->压缩文件
    浅蓝色-->链接文件
    灰色-->其他文件
  除了斜杠和NUL,所有字符都有效.但使用特殊字符的目录名和文件不推荐使用,有些字符需要用引号来引用它们
  标准Linux文件系统(如ext4,xfs),文件名称大小写敏感
    例如:MAIL, Mail, mail, mAiL

3>.文件系统结构

/boot:
  引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader, grub) 都存放于此目录 

/bin:   所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
/sbin:   管理类的基本命令;不能关联至独立分区,OS启动即会用到的程序
/lib:   启动时程序依赖的基本共享库文件以及内核模块文件(/lib/modules)
/lib64:   专用于x86_64系统上的辅助共享库文件存放位置
/etc:   配置文件目录
/home/USERNAME:   普通用户默认家目录
/root:   管理员的家目录
/media:   便携式移动设备默认挂载点
/mnt:   临时文件系统挂载点
/dev:   设备文件及特殊文件存储位置     b: block device,随机访问     c: character device,线性访问
/opt:   第三方应用程序的安装位置
/srv:   系统上运行的服务用到的数据
/tmp:   临时文件存储位置。
/usr: universal shared, read-only data   bin: 保证系统拥有完整功能而提供的应用程序   sbin:   lib:32位使用   lib64:只存在64位系统   include: C程序的头文件(header files)   share:结构化独立的数据,例如doc, man等   local:第三方应用程序的安装位置     bin     sbin     lib     lib64     etc     share
/var: variable data files   cache: 应用程序缓存数据目录   lib: 应用程序状态信息数据   local:专用于为/usr/local下的应用程序存储可变数据   lock: 锁文件   log: 日志目录及文件   opt: 专用于为/opt下的应用程序存储可变数据   run: 运行中的进程相关数据,通常用于存储进程pid文件   spool: 应用程序数据池   tmp: 保存系统两次重启之间产生的临时数据
/proc:   用于输出内核与进程信息相关的虚拟文件系统
/sys:   用于输出当前系统上硬件设备相关信息虚拟文件系统
/selinux: security enhanced Linux,   selinux相关的安全策略等信息的存储位置

4>.Linux上的应用程序的组成部分 

二进制程序:
  /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, /usr/local/sbin

库文件:
  /lib, /lib64, /usr/lib, /usr/lib64, /usr/local/lib, /usr/local/lib64

配置文件:
  /etc, /etc/DIRECTORY, /usr/local/etc

帮助文件:
  /usr/share/man, /usr/share/doc, /usr/local/share/man, /usr/local/share/doc

5>.Linux下的文件类型 

- 普通文件 
d 目录文件
b 块设备
c 字符设备
l 符号链接文件
p 管道文件pipe
s 套接字文件socket
[root@node101.yinzhengjie.org.cn ~]# mknod pfile p        #我们可以手动创建出来管道文件,但是实际生产环境中是开发人员自定义的,我们运维人员不需要去手动创建它,管道文件用于进程间通信,遵循FIFO模式。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
prw-r--r--. 1 root root 0 Aug  7 10:12 pfile            #注意第一个字母为文件类型,这个p则为管道类型
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mkfifo pfile2        #除了使用mknod命令还可以使用mkfifo命令来创建一个管道文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
prw-r--r--. 1 root root 0 Aug  7 10:12 pfile
prw-r--r--. 1 root root 0 Aug  7 10:16 pfile2
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mknod pfile p           #我们可以手动创建出来管道文件,但是实际生产环境中是开发人员自定义的,我们运维人员不需要去手动创建它,管道文件用于进程间通信,遵循FIFO模式。
[root@node101.yinzhengjie.org.cn ~]# nc -Ul socketfile      #执行这条命令就会卡在当前界面,他会监听一个套接字文件,其实我们也不需要手动去创建它,一般都是应用程序自动创建的
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
prw-r--r--. 1 root root 0 Aug  7 10:12 pfile
prw-r--r--. 1 root root 0 Aug  7 10:16 pfile2
srwxr-xr-x. 1 root root 0 Aug  7 10:19 socketfile         #不难发现,我们可以在这里看到对应的套接字文件类型,看首字母
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nc -Ul socketfile        #执行这条命令就会卡在当前界面,他会监听一个套接字文件,其实我们也不需要手动去创建它,一般都是应用程序自动创建的,和管道文件不同的是,套接字文件是双向传输数据的
[root@node101.yinzhengjie.org.cn ~]# ll /dev/sda*           #查看设备文件
brw-rw----. 1 root disk 8, 0 Aug  6 12:27 /dev/sda          #注意,这里的块设备有2个数字组成,第一个数字8表示的是主要类型,第二个数字表示的是属于第一个数字对应类型的第几个设备。
brw-rw----. 1 root disk 8, 1 Aug  6 12:27 /dev/sda1
brw-rw----. 1 root disk 8, 2 Aug  6 12:27 /dev/sda2
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mknod ./disk b 8 0       #一般情况下不需要手动创建设备文件,当然我们也可以创建一个设备文件,
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
brw-r--r--. 1 root root 8, 0 Aug  7 10:24 disk
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mknod ./disk b 8 0       #一般情况下不需要手动创建设备文件,当然我们也可以创建一个设备文件,
[root@node101.yinzhengjie.org.cn ~]# ll /dev/zero /dev/null      #查看设备文件,我们发现系统默认有很多字符设备文件,观察首字母是否是c 
crw-rw-rw-. 1 root root 1, 3 Aug  6 12:27 /dev/null
crw-rw-rw-. 1 root root 1, 5 Aug  6 12:27 /dev/zero
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mknod ./zore c 1 5        #一般情况下我们不需要创建字符设备,当然我们也可以手动去创建一个字符设备
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
crw-r--r--. 1 root root 1, 5 Aug  7 10:29 zore             #很显然,这里有个字符设备是咱们自己创建的
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mknod ./zore c 1 5        #一般情况下我们不需要创建字符设备,当然我们也可以手动去创建一个字符设备

6>.CentOS 7目录变化 

/bin 和 /usr/bin  
/sbin 和 /usr/sbin
/lib 和/usr/lib
/lib64 和 /usr/lib64

7>.显示当前工作目录

每个shell和系统进程都有一个当前的工作目录,即CWD(current work directory)    
  [root@node101.yinzhengjie.org.cn /bin]# pwd       #显示当前shell CWD的绝对路径,pwd: printing working directory ,当前路径会被保存到一个叫做"$PWD"的变量中
  /bin
  [root@node101.yinzhengjie.org.cn /bin]# 
  [root@node101.yinzhengjie.org.cn /bin]# pwd -L      #显示连接路径(很显然,默认就是使用该参数,我们发现该参数和上面没有加-L参数输出一致)
  /bin
  [root@node101.yinzhengjie.org.cn /bin]# 
  [root@node101.yinzhengjie.org.cn /bin]# pwd -P      #显示真实物理路径
  /usr/bin
  [root@node101.yinzhengjie.org.cn /bin]#

8>.绝对和相对路径

绝对路径  
  以正斜杠开始
  完整的文件的位置路径
  可用于任何想指定一个文件名的时候

相对路径名
  不以斜线开始
  指定相对于当前工作目录或某目录的位置
  可以作为一个简短的形式指定一个文件名

基名:basename
目录名:dirname

9>.更改目录

cd  改变目录(使用绝对或相对路径) 
  切换至父目录: cd ..
  切换至当前用户主目录: cd
  切换至上一次的工作目录,原理是有一个"$OLDPWD"的变量来保存上一次变量: cd -         
  切换至指定用户的家目录,注意权限问题哟:cd ~yinzhengjie

选项:-P

相关的环境变量:
  PWD:当前目录路径
  OLDPWD:上一次目录路径

10>.列出目录内容 

列出当前目录的内容或指定目录 
  用法:ls [options] [ files_or_dirs ]

示例:
  ls -a 包含隐藏文件
  ls -l 显示额外的信息
  ls -R 目录递归
  ls -ld 目录和符号链接信息
  ls -1 文件分行显示
  ls –S 按从大到小排序
  ls –t 按mtime排序
  ls –u 配合-t选项,显示并按atime从新到旧排序
  ls –U 按目录存放顺序显示
  ls –X 按文件后缀排序
[root@node101.yinzhengjie.org.cn ~]# ls -a          #显示所有隐藏文件,包括当前目录和父目录
.   .bash_history  .bash_profile  .cache   .cshrc  .esd_auth      .lesshst  .tcshrc
..  .bash_logout   .bashrc        .config  .dbus   .ICEauthority  .local    zore
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls -a          #显示所有隐藏文件,包括当前目录和父目录
[root@node101.yinzhengjie.org.cn ~]# ls -A          #显示隐藏文件,但不包含当前目录和家目录
.bash_history  .bash_profile  .cache   .cshrc  .esd_auth      .lesshst  .tcshrc
.bash_logout   .bashrc        .config  .dbus   .ICEauthority  .local    zore
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls -A          #显示隐藏文件,但不包含当前目录和家目录
[root@node101.yinzhengjie.org.cn ~]# ls -l          #显示额外的信息,但不显示隐藏文件
total 0
crw-r--r--. 1 root yinzhengjie 1, 5 Aug  7 10:29 zore
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls -l --author     #我们还可以查看文件的所有者
total 0
crw-r--r--. 1 root yinzhengjie root 1, 5 Aug  7 10:29 zore
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls -l --author     #我们还可以查看文件的所有者

11>.查看文件状态

文件:
  metadata, data 

三个时间戳:      
  access time 访问时间,atime,读取文件内容      
  modify time 修改时间, mtime,改变文件内容(数据内容发生改变)      
  change time 改变时间, ctime,元数据发生改变(比如文件名称发生改变) 

 案例如下:
  [root@node101.yinzhengjie.org.cn ~]# stat a.txt 
  File: ‘a.txt’
  Size: 7 Blocks: 8 IO Block: 4096 regular file
  Device: fd00h/64768d    Inode: 100663373 Links: 1
  Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
  Context: unconfined_u:object_r:admin_home_t:s0
  Access: 2019-08-03 18:25:32.826008829 +0800
  Modify: 2019-08-03 18:25:30.531008976 +0800
  Change: 2019-08-03 18:25:30.531008976 +0800
  Birth: -
  [root@node101.yinzhengjie.org.cn ~]#

12>.通配符模式 wild-card pattern 

* 匹配零个或多个字符 
? 匹配任何单个字符
~ 当前用户家目录
~yinzhengjie 用户yinzhengjie家目录
~+ 当前工作目录
~- 前一个工作目录
[0-9] 匹配数字范围
[a-z]:字母
[A-Z]:字母
[jason] 匹配列表中的任何的一个字符
[^jason] 匹配列表中的所有字符以外的字符 预定义的字符类:man 7 glob
  [:digit:]:任意数字,相当于0-9
  [:lower:]:任意小写字母
  [:upper:]: 任意大写字母
  [:alpha:]: 任意大小写字母
  [:alnum:]:任意数字或字母
  [:blank:]:水平空白字符
  [:space:]:水平或垂直空白字符
  [:punct:]:标点符号
  [:print:]:可打印字符
  [:cntrl:]:控制(非打印)字符
  [:graph:]:图形字符
  [:xdigit:]:十六进制字符

 

二.文件常见的管理命令

1>.创建空文件和刷新时间 

touch命令 

格式:touch [OPTION]... FILE...   
  -a   仅改变 atime和ctime   
  更多帮助可参考,"touch --help"或者"man touch"
[root@node101.yinzhengjie.org.cn ~]# stat a.txt 
  File: ‘a.txt’
  Size: 7             Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 100663373   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2019-08-07 11:33:36.200660510 +0800          #注意观察这一行的时间
Modify: 2019-08-07 11:33:36.200660510 +0800
Change: 2019-08-07 11:33:36.200660510 +0800          #还有这一行的时间
 Birth: -
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# touch -a a.txt       #如果文件存在,仅改变atime和ctime
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# stat a.txt 
  File: ‘a.txt’
  Size: 7             Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 100663373   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2019-08-07 11:34:17.806657839 +0800          #我们发现时间发生改变啦
Modify: 2019-08-07 11:33:36.200660510 +0800
Change: 2019-08-07 11:34:17.806657839 +0800          #这里的时间也发生改变啦~
 Birth: -
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# touch -a a.txt       #如果文件存在,仅改变atime和ctime

2>.复制文件和目录

[root@node101.yinzhengjie.org.cn ~]#  cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST          #将一个文件拷贝到另一个为位置
  or:  cp [OPTION]... SOURCE... DIRECTORY         #将多个文件拷贝到一个目录中,目录放在最后一个
  or:  cp [OPTION]... -t DIRECTORY SOURCE...       #也是将多个文件拷贝到一个目录中,只不过我们需要加一个“-t”的参数且目录得放在第一位
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dR --preserve=all                                  #这个选项一般用于备份,它可以保留所有的文件属性
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument          #目标存在,覆盖前先备份,名称形式为filename~
      --copy-contents          copy contents of special files when recursive
  -d                           same as --no-dereference --preserve=links            #不复制元文件,只复制链接名
  -f, --force                  if an existing destination file cannot be            #强制覆盖
                                 opened, remove it and try again (this option
                                 is ignored when the -n option is also used)
  -i, --interactive            prompt before overwrite (overrides a previous -n        #覆盖前提示,在别名中设置默认就有该参数
                                  option)
  -H                           follow command-line symbolic links in SOURCE
  -l, --link                   hard link files instead of copying
  -L, --dereference            always follow symbolic links in SOURCE
  -n, --no-clobber             do not overwrite an existing file (overrides          #不覆盖,注意两者顺序
                                 a previous -i option)
  -P, --no-dereference         never follow symbolic links in SOURCE
  -p                           same as --preserve=mode,ownership,timestamps
      --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                 mode,ownership,timestamps), if possible
                                 additional attributes: context, links, xattr,
                                 all
  -c                           deprecated, same as --preserve=context
      --no-preserve=ATTR_LIST  don't preserve the specified attributes
      --parents                use full source file name under DIRECTORY
  -R, -r, --recursive          copy directories recursively
      --reflink[=WHEN]         control clone/CoW copies. See below
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --sparse=WHEN            control creation of sparse files. See below
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -s, --symbolic-link          make symbolic links instead of copying
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 copy only when the SOURCE file is newer        #只复制源比目标更新文件或目标不存在的文件
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done                          #备份的时候可以使用"-av"参数,因为不仅仅可以备份所有的数据即属性,还可以显示详细信息
  -x, --one-file-system        stay on this file system
  -Z                           set SELinux security context of destination
                                 file to default type
      --context[=CTX]          like -Z, or if CTX is specified then set the
                                 SELinux or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well.  That is the behavior
selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.

When --reflink[=always] is specified, perform a lightweight copy, where the
data blocks are copied only when modified.  If this is not possible the copy
fails, or if --reflink=auto is specified, fall back to a standard copy.

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cp invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp --help
[root@node101.yinzhengjie.org.cn ~]# cp -av /etc/sysconfig/network-scripts/ ./network_bak        #保留文件的所有属性并输出拷贝过程的详细信息
‘/etc/sysconfig/network-scripts/’ -> ‘./network_bak’
‘/etc/sysconfig/network-scripts/ifcfg-lo’ -> ‘./network_bak/ifcfg-lo’
‘/etc/sysconfig/network-scripts/ifdown’ -> ‘./network_bak/ifdown’
‘/etc/sysconfig/network-scripts/ifdown-bnep’ -> ‘./network_bak/ifdown-bnep’
‘/etc/sysconfig/network-scripts/ifdown-eth’ -> ‘./network_bak/ifdown-eth’
‘/etc/sysconfig/network-scripts/ifdown-ippp’ -> ‘./network_bak/ifdown-ippp’
‘/etc/sysconfig/network-scripts/ifdown-ipv6’ -> ‘./network_bak/ifdown-ipv6’
‘/etc/sysconfig/network-scripts/ifdown-isdn’ -> ‘./network_bak/ifdown-isdn’
‘/etc/sysconfig/network-scripts/ifdown-post’ -> ‘./network_bak/ifdown-post’
‘/etc/sysconfig/network-scripts/ifdown-ppp’ -> ‘./network_bak/ifdown-ppp’
‘/etc/sysconfig/network-scripts/ifdown-routes’ -> ‘./network_bak/ifdown-routes’
‘/etc/sysconfig/network-scripts/ifdown-sit’ -> ‘./network_bak/ifdown-sit’
‘/etc/sysconfig/network-scripts/ifdown-tunnel’ -> ‘./network_bak/ifdown-tunnel’
‘/etc/sysconfig/network-scripts/ifup’ -> ‘./network_bak/ifup’
‘/etc/sysconfig/network-scripts/ifup-aliases’ -> ‘./network_bak/ifup-aliases’
‘/etc/sysconfig/network-scripts/ifup-bnep’ -> ‘./network_bak/ifup-bnep’
‘/etc/sysconfig/network-scripts/ifup-eth’ -> ‘./network_bak/ifup-eth’
‘/etc/sysconfig/network-scripts/ifup-ippp’ -> ‘./network_bak/ifup-ippp’
‘/etc/sysconfig/network-scripts/ifup-ipv6’ -> ‘./network_bak/ifup-ipv6’
‘/etc/sysconfig/network-scripts/ifup-isdn’ -> ‘./network_bak/ifup-isdn’
‘/etc/sysconfig/network-scripts/ifup-plip’ -> ‘./network_bak/ifup-plip’
‘/etc/sysconfig/network-scripts/ifup-plusb’ -> ‘./network_bak/ifup-plusb’
‘/etc/sysconfig/network-scripts/ifup-post’ -> ‘./network_bak/ifup-post’
‘/etc/sysconfig/network-scripts/ifup-ppp’ -> ‘./network_bak/ifup-ppp’
‘/etc/sysconfig/network-scripts/ifup-routes’ -> ‘./network_bak/ifup-routes’
‘/etc/sysconfig/network-scripts/ifup-sit’ -> ‘./network_bak/ifup-sit’
‘/etc/sysconfig/network-scripts/ifup-tunnel’ -> ‘./network_bak/ifup-tunnel’
‘/etc/sysconfig/network-scripts/ifup-wireless’ -> ‘./network_bak/ifup-wireless’
‘/etc/sysconfig/network-scripts/init.ipv6-global’ -> ‘./network_bak/init.ipv6-global’
‘/etc/sysconfig/network-scripts/network-functions’ -> ‘./network_bak/network-functions’
‘/etc/sysconfig/network-scripts/network-functions-ipv6’ -> ‘./network_bak/network-functions-ipv6’
‘/etc/sysconfig/network-scripts/ifdown-ib’ -> ‘./network_bak/ifdown-ib’
‘/etc/sysconfig/network-scripts/ifup-ib’ -> ‘./network_bak/ifup-ib’
‘/etc/sysconfig/network-scripts/ifdown-Team’ -> ‘./network_bak/ifdown-Team’
‘/etc/sysconfig/network-scripts/ifdown-TeamPort’ -> ‘./network_bak/ifdown-TeamPort’
‘/etc/sysconfig/network-scripts/ifup-Team’ -> ‘./network_bak/ifup-Team’
‘/etc/sysconfig/network-scripts/ifup-TeamPort’ -> ‘./network_bak/ifup-TeamPort’
‘/etc/sysconfig/network-scripts/ifcfg-ens33’ -> ‘./network_bak/ifcfg-ens33’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -d /etc/sysconfig/network-scripts/             #查看文件目录的属性
drwxr-xr-x. 2 root root 4096 Aug  1 22:20 /etc/sysconfig/network-scripts/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll                                  #发现2个目录的属性是一致的
total 4
drwxr-xr-x. 2 root root 4096 Aug  1 22:20 network_bak
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp -av /etc/sysconfig/network-scripts/ ./network_bak        #保留文件的所有属性并输出拷贝过程的详细信息
[root@node101.yinzhengjie.org.cn ~]# touch a.txt b.txt                #创建测试文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mkdir ./data                   #创建测试目录
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# \cp -uv a.txt b.txt ./data           #由于"./data"目录是我刚刚创建的新目录,因此我们更新拷贝文件的时候,第一次更新拷贝会将所有文件拷贝过来
‘a.txt’ -> ‘./data/a.txt’
‘b.txt’ -> ‘./data/b.txt’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# touch a.txt                    #我们使用touch命令去刷新"a.txt"文件的访问时间
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# \cp -uv a.txt b.txt ./data           #我们知道此时"./data"目录下已经存在"a.txt""b.txt",由于上面我们只更新了"a.txt"属性,而"b.txt"文件并未修改,因此它只会拷贝更新状态的文件,我们使用"\"来禁用cp的别名!
‘a.txt’ -> ‘./data/a.txt’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# \cp -uv a.txt b.txt ./data           #我们知道此时"./data"目录下已经存在"a.txt"和"b.txt",由于上面我们只更新了"a.txt"属性,而"b.txt"文件并未修改,因此它只会拷贝更新状态的文件,我们使用"\"来禁用cp的别名!
[root@node101.yinzhengjie.org.cn ~]# ll -R                    #我们递归查看当前文件的内容,很明显在当前目录中存在"a.txt"和“b.txt”以及"data"目录
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 32 Aug  7 14:29 data          

./data:                                        #不难发现在"./data"目录下存在"a.txt""b.txt"文件
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp -b a.txt data/            #我们将"a.txt"文件拷贝到"./data"目录下,如果"./data"目录存在"a.txt"的话,它会把元文件进行一个备份操作,即"filenamme~"
cp: overwrite ‘data/a.txt’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -R
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 46 Aug  7 14:37 data      

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~                  #不难发现,它将之前的"a.txt"备份为"a.txt~",这样操作可以防止数据丢失的风险
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp -b a.txt data/                #我们将"a.txt"文件拷贝到"./data"目录下,如果"./data"目录存在"a.txt"的话,它会把元文件进行一个备份操作,即"filenamme~"
[root@node101.yinzhengjie.org.cn ~]# ll -R
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 46 Aug  7 14:37 data

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp  a.txt data/ --backup=numbered        #和"-b"参数不同,"--backup=numbered"可以保留多个不同的备份版本并用数字区分,而"-b"则只能保留之前的一个版本。
cp: overwrite ‘data/a.txt’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp  a.txt data/ --backup=numbered
cp: overwrite ‘data/a.txt’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp  a.txt data/ --backup=numbered
cp: overwrite ‘data/a.txt’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -R
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 97 Aug  7 14:45 data

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:45 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~          #这个文件使用”-b“参数去复制文件的
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt.~1~        #该文件有数字编号的就是使用"--backup=numberd"参数去实现备份的,可以保留多个版本
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~2~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~3~
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp a.txt data/ --backup=numbered        #和"-b"参数不同,"--backup=numbered"可以保留多个不同的备份版本并用数字区分,而"-b"则只能保留之前的一个版本。

3>.移动和重命名文件

[root@node101.yinzhengjie.org.cn ~]# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to 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     #目标存在,覆盖前先备份
  -f, --force                  do not prompt before overwriting             #强制
  -i, --interactive            prompt before overwrite                  #交互式
  -n, --no-clobber             do not overwrite an existing file
If you specify more than one of -i, -f, -n, only the final one takes effect.
      --strip-trailing-slashes  remove any trailing slashes from each SOURCE
                                 argument
  -S, --suffix=SUFFIX          override the usual backup suffix
  -t, --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -T, --no-target-directory    treat DEST as a normal file
  -u, --update                 move only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -Z, --context                set SELinux security context of destination
                                 file to default type
      --help     display this help and exit
      --version  output version information and exit

The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:

  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'mv invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv --help
[root@node101.yinzhengjie.org.cn ~]# ll -R                  #查看当前的目录结构
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 97 Aug  7 14:45 data

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:45 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt.~1~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~2~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~3~
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv a.txt ./data/            #我们将"a.txt"文件移动到"./data"目录中,由于已经存在了,因此它会提示我们是否覆盖该文件,这个交互式提示主要是因为mv别名中使用"-i"选项
mv: overwrite ‘./data/a.txt’? y                         #注意,使用mv命令移动成功后,源文件就不存在啦~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -R
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 97 Aug  7 14:57 data

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt.~1~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~2~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~3~
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv a.txt ./data/              #我们将"a.txt"文件移动到"./data"目录中,由于已经存在了,因此它会提示我们是否覆盖该文件,这个交互式提示主要是因为mv别名中使用"-i"选项
[root@node101.yinzhengjie.org.cn ~]# ll -R
.:
total 0
-rw-r--r--. 1 root root  0 Aug  7 14:29 b.txt
drwxr-xr-x. 2 root root 97 Aug  7 14:57 data

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt.~1~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~2~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~3~
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv -b b.txt ./data/b.txt         #和cp命令的"-b"选项一致,移动文件在目标位置存在则将其备份一份
mv: overwrite ‘./data/b.txt’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -R
.:
total 0
drwxr-xr-x. 2 root root 111 Aug  7 15:03 data

./data:
total 0
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 a.txt~
-rw-r--r--. 1 root root 0 Aug  7 14:37 a.txt.~1~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~2~
-rw-r--r--. 1 root root 0 Aug  7 14:44 a.txt.~3~
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt
-rw-r--r--. 1 root root 0 Aug  7 14:29 b.txt~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv -b b.txt ./data/b.txt         #和cp命令的"-b"选项一致,移动文件在目标位置存在则将其备份一份

4>.删除 

[root@node101.yinzhengjie.org.cn ~]# 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                        #交互式
  -I                    prompt once before removing more than three files, or        
                          when removing recursively; less intrusive than -i,
                          while still giving protection against most mistakes
      --interactive[=WHEN]  prompt according to WHEN: never, once (-I), or
                          always (-i); without WHEN, prompt always
      --one-file-system  when removing a hierarchy recursively, skip any
                          directory that is on a file system different from
                          that of the corresponding command line argument
      --no-preserve-root  do not treat '/' specially                      #我们如果想要删除根的话,需要加这个参数,切记!如果在生产环境千万千万千万不要这么玩!小心引火自焚!
      --preserve-root   do not remove '/' (default)
  -r, -R, --recursive   remove directories and their contents recursively          #递归删除
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
      --help     display this help and exit
      --version  output version information and exit

By default, rm does not remove directories.  Use the --recursive (-r or -R)
option to remove each listed directory, too, along with all of its contents.

To remove a file whose name starts with a '-', for example '-foo',
use one of these commands:
  rm -- -foo

  rm ./-foo

Note that if you use rm to remove a file, it might be possible to recover
some of its contents, given sufficient expertise and/or time.  For greater
assurance that the contents are truly unrecoverable, consider using shred.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'rm invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm --help
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
-rw-r--r--. 1 root root  0 Aug  7 15:14 a.txt
-rw-r--r--. 1 root root  0 Aug  7 15:14 b.txt
drwxr-xr-x. 2 root root 98 Aug  7 15:13 data
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# alias rm          #我们可以看到rm命令,其实别名中就使用了"-i"选项
alias rm='rm -i'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm a.txt           #我们在删除文件的时候他会提示我们就是"-i"选项在捣鬼,当然我们可以使用"-f"选项来强制删除,就不会出现这样的提示信息啦~
rm: remove regular empty file ‘a.txt’? y
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# \rm b.txt          #我们发现不使用别名的话,就不会有相应的提示信息啦
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
drwxr-xr-x. 2 root root 98 Aug  7 15:13 data
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -rf data/        #删除目录的话需要使用递归参数
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
[root@node101.yinzhengjie.org.cn ~]# 

5>. 目录操作

tree 显示目录树  
  -d: 只显示目录
  -L level:指定显示的层级数目
  -P pattern: 只显示由指定wild-card pattern匹配到的路径


mkdir 创建目录
  -p: 存在于不报错,且可自动创建所需的各目录
  -v: 显示详细信息
  -m MODE: 创建目录时直接指定权限

rmdir 删除空目录
  -p: 递归删除父空目录
  -v: 显示详细信息

rm -r 递归删除目录树
[root@node101.yinzhengjie.org.cn ~]# df
Filesystem              1K-blocks     Used Available Use% Mounted on
/dev/mapper/centos-root  52403200  4428108  47975092   9% /
devtmpfs                  1913652        0   1913652   0% /dev
tmpfs                     1930756        0   1930756   0% /dev/shm
tmpfs                     1930756    12788   1917968   1% /run
tmpfs                     1930756        0   1930756   0% /sys/fs/cgroup
/dev/sda1                 1038336   182912    855424  18% /boot
/dev/mapper/centos-home 466511300    33100 466478200   1% /home
tmpfs                      386152       28    386124   1% /run/user/0
/dev/sr0                 10491772 10491772         0 100% /run/media/root/CentOS 7 x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rpm -ivh /run/media/root/CentOS\ 7\ x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm       #通过本地光盘按照tree命令
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rpm -ivh /run/media/root/CentOS\ 7\ x86_64/Packages/tree-1.6.0-10.el7.x86_64.rpm       #通过本地光盘按照tree命令
[root@node101.yinzhengjie.org.cn ~]# tree /etc/sysconfig/        #查看"/etc/sysconfig"目录结构
/etc/sysconfig/
├── anaconda
├── atd
├── authconfig
├── autofs
├── cbq
│   ├── avpkt
│   └── cbq-0000.example
├── cgred
├── chronyd
├── console
├── cpupower
├── crond
├── ebtables-config
├── fcoe
├── firewalld
├── grub -> ../default/grub
├── init
├── ip6tables-config
├── iptables-config
├── irqbalance
├── kdump
├── kernel
├── ksm
├── libvirtd
├── man-db
├── modules
├── netconsole
├── network
├── network-scripts
│   ├── ifcfg-ens33
│   ├── ifcfg-lo
│   ├── ifdown -> ../../../usr/sbin/ifdown
│   ├── ifdown-bnep
│   ├── ifdown-eth
│   ├── ifdown-ib
│   ├── ifdown-ippp
│   ├── ifdown-ipv6
│   ├── ifdown-isdn -> ifdown-ippp
│   ├── ifdown-post
│   ├── ifdown-ppp
│   ├── ifdown-routes
│   ├── ifdown-sit
│   ├── ifdown-Team
│   ├── ifdown-TeamPort
│   ├── ifdown-tunnel
│   ├── ifup -> ../../../usr/sbin/ifup
│   ├── ifup-aliases
│   ├── ifup-bnep
│   ├── ifup-eth
│   ├── ifup-ib
│   ├── ifup-ippp
│   ├── ifup-ipv6
│   ├── ifup-isdn -> ifup-ippp
│   ├── ifup-plip
│   ├── ifup-plusb
│   ├── ifup-post
│   ├── ifup-ppp
│   ├── ifup-routes
│   ├── ifup-sit
│   ├── ifup-Team
│   ├── ifup-TeamPort
│   ├── ifup-tunnel
│   ├── ifup-wireless
│   ├── init.ipv6-global
│   ├── network-functions
│   └── network-functions-ipv6
├── nfs
├── ntpd
├── ntpdate
├── qemu-ga
├── radvd
├── raid-check
├── rdisc
├── readonly-root
├── rpcbind
├── rpc-rquotad
├── rsyncd
├── rsyslog
├── run-parts
├── samba
├── saslauthd
├── selinux -> ../selinux/config
├── smartmontools
├── sshd
├── sysstat
├── sysstat.ioconf
├── virtlockd
├── virtlogd
└── wpa_supplicant

4 directories, 85 files            #这里告诉我们在我们查看的路径中存在的目录和文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tree /etc/sysconfig/          #查看"/etc/sysconfig"目录结构
[root@node101.yinzhengjie.org.cn ~]# tree -d /etc/sysconfig/      #使用"-d"选项只查看文件夹
/etc/sysconfig/
├── cbq
├── console
├── modules
└── network-scripts

4 directories
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tree -d /etc/sysconfig/        #使用"-d"选项只查看文件夹
[root@node101.yinzhengjie.org.cn ~]# tree -d -L 1 /etc           #使用"-L"选项可以指定查看的层级
/etc
├── abrt
├── alsa
├── alternatives
├── audisp
├── audit
├── auto.master.d
├── avahi
├── bash_completion.d
├── binfmt.d
├── bluetooth
├── brltty
├── certmonger
├── cgconfig.d
├── chkconfig.d
├── cifs-utils
├── cron.d
├── cron.daily
├── cron.hourly
├── cron.monthly
├── cron.weekly
├── cups
├── cupshelpers
├── dbus-1
├── dconf
├── default
├── depmod.d
├── dhcp
├── dnsmasq.d
├── dracut.conf.d
├── egl
├── exports.d
├── fcoe
├── festival
├── firefox
├── firewalld
├── flatpak
├── fonts
├── foomatic
├── fwupd
├── gconf
├── gcrypt
├── gdbinit.d
├── gdm
├── geoclue
├── ghostscript
├── glvnd
├── gnupg
├── groff
├── grub.d
├── gss
├── gssproxy
├── hp
├── init.d -> rc.d/init.d
├── ipa
├── iproute2
├── ipsec.d
├── iscsi
├── java
├── jvm
├── jvm-commmon
├── kernel
├── krb5.conf.d
├── ld.so.conf.d
├── libblockdev
├── libibverbs.d
├── libnl
├── libreport
├── libvirt
├── logrotate.d
├── lsb-release.d
├── lsm
├── lvm
├── maven
├── modprobe.d
├── modules-load.d
├── multipath
├── my.cnf.d
├── ndctl
├── NetworkManager
├── ntp
├── oddjob
├── oddjobd.conf.d
├── openldap
├── opt
├── PackageKit
├── pam.d
├── pkcs11
├── pki
├── plymouth
├── pm
├── polkit-1
├── popt.d
├── postfix
├── ppp
├── prelink.conf.d
├── profile.d
├── pulse
├── purple
├── python
├── qemu-ga
├── qemu-kvm
├── rc0.d -> rc.d/rc0.d
├── rc1.d -> rc.d/rc1.d
├── rc2.d -> rc.d/rc2.d
├── rc3.d -> rc.d/rc3.d
├── rc4.d -> rc.d/rc4.d
├── rc5.d -> rc.d/rc5.d
├── rc6.d -> rc.d/rc6.d
├── rc.d
├── rdma
├── redhat-lsb
├── request-key.d
├── rpm
├── rsyslog.d
├── rwtab.d
├── samba
├── sane.d
├── sasl2
├── scl
├── security
├── selinux
├── setroubleshoot
├── setuptool.d
├── sgml
├── skel
├── smartmontools
├── speech-dispatcher
├── ssh
├── ssl
├── sssd
├── statetab.d
├── sudoers.d
├── sysconfig
├── sysctl.d
├── systemd
├── target
├── terminfo
├── tmpfiles.d
├── tuned
├── udev
├── udisks2
├── unbound
├── UPower
├── vmware-tools
├── wpa_supplicant
├── X11
├── xdg
├── xinetd.d
├── xml
├── yum
└── yum.repos.d

151 directories
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tree -d -L 1 /etc           #使用"-L"选项可以指定查看的层级
[root@node101.yinzhengjie.org.cn ~]# tree -d -P /etc/rc*          #使用"-P"参数可以使用匹配模式,列出符合我们想要的目录
/etc/rc1.d
/etc/rc2.d
/etc/rc3.d
/etc/rc4.d
/etc/rc5.d
/etc/rc6.d
/etc/rc.d
├── init.d
├── rc0.d
├── rc1.d
├── rc2.d
├── rc3.d
├── rc4.d
├── rc5.d
└── rc6.d
/etc/rc.local [error opening dir]

8 directories
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tree -d -P /etc/rc*          #使用"-P"参数可以使用匹配模式,列出符合我们想要的目录
[root@node101.yinzhengjie.org.cn ~]# mkdir ./data/a/b/c -pv        #"-v"参数表示创建目录并显示详细信息,"-p"参数表示父目录不存在的话就递归创建出来
mkdir: created directory ‘./data’
mkdir: created directory ‘./data/a’
mkdir: created directory ‘./data/a/b’
mkdir: created directory ‘./data/a/b/c’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mkdir ./data/a/b/c -pv        #"-v"参数表示创建目录并显示详细信息,"-p"参数表示父目录不存在的话就递归创建出来

6>.小试牛刀

  (1)显示/var目录下所有以l开头,以一个小写字母结尾,且中间出现至少一位数 字的文件或目录 
  (2)显示/etc目录下以任意一位数字开头,且以非数字结尾的文件或目录 
  (3)显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符 的文件或目录 
  (4)显示/etc/目录下所有以rc开头,并后面是0-6之间的数字,其它为任意字符 的文件或目录 
  (5)显示/etc目录下,所有以.d结尾的文件或目录 
  (6)显示/etc目录下,所有.conf结尾,且以m,n,r,p开头的文件或目录 
  (7)只显示/root下的隐藏文件和目录 
  (8)只显示/etc下的非隐藏目录 
  (9)每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录 格式为 backupYYYY-mm-dd,备份过程可见 
  (10)创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有 权限 
  (11)如何创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b 
  (12)如何创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b 
  (13)如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7 
  (14)用mv代替rm实现安全性,比如:rm a.txt ==> mv a.txt /tmp/2019-08-06_15:36:35/
 

 

三.索引(index)

1>.索引节点

  inode(index node)表中包含文件系统所有文件列表 

  一个节点 (索引节点)是在一个表项,包含有关文件的信息( 元数据 ),包括如下信息:
    文件类型,权限,UID,GID
    链接数(指向这个文件名路径名称个数)
    该文件的大小和不同的时间戳
    指向磁盘上文件的数据块指针
    有关文件的其他数据

 温馨提示:
    文件的名称并非是元数据信息哟,文件名称是存放在当前目录下的,目录中的数据存放这文件名称及文件名称对应的唯一inode number,通过这个inode number中对应的直接块指针或者间接块指针会找到相应的数据信息。
    当我们删除文件时并没有真正意味着删除,也就是说我们删除一个数据时,其实并没有真的把数据给清空了,而是在目录的数据中擦除了对应文件名称的记录且回收了相应的inode节点,之前的存储数据区域被标记无人使用了,如果这个时候你还想恢复之前的数据的话,就最好不要对该文件做任何写操作了,否则可能存在部分数据无法恢复的情况,因为当有相应的inode节点被其他文件命名所引用,那么往该文件名称写入的数据会不断的将你之前的数据给覆盖掉,从而导致数据被算坏的情况。
[root@node101.yinzhengjie.org.cn ~]# df -i        #我们使用df命令可以查看到操作系统中各个分区所维护的Inodes的节点数
Filesystem                 Inodes  IUsed     IFree IUse% Mounted on
/dev/mapper/centos-root  26214400 135455  26078945    1% /
devtmpfs                   478413    417    477996    1% /dev
tmpfs                      482689      1    482688    1% /dev/shm
tmpfs                      482689   1507    481182    1% /run
tmpfs                      482689     16    482673    1% /sys/fs/cgroup
/dev/sda1                  524288    341    523947    1% /boot
/dev/mapper/centos-home 233369600     10 233369590    1% /home
tmpfs                      482689     18    482671    1% /run/user/0
/dev/sr0                        0      0         0     - /run/media/root/CentOS 7 x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# touch {1..5}.txt    #我们创建5个文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i           #很明显,不同文件对应的inode编号是不同的哟
total 0
100663386 -rw-r--r--. 1 root root 0 Aug  7 15:51 1.txt
100891213 -rw-r--r--. 1 root root 0 Aug  7 15:51 2.txt
100891219 -rw-r--r--. 1 root root 0 Aug  7 15:51 3.txt    #注意观察"3.txt"的inode编号
100891248 -rw-r--r--. 1 root root 0 Aug  7 15:51 4.txt
100891250 -rw-r--r--. 1 root root 0 Aug  7 15:51 5.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -f 3.txt   ·    #我们把"3.txt"文件给它删除掉
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# touch 6.txt       #创建一个"6.txt"的空文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i           
total 0
100663386 -rw-r--r--. 1 root root 0 Aug  7 15:51 1.txt
100891213 -rw-r--r--. 1 root root 0 Aug  7 15:51 2.txt
100891248 -rw-r--r--. 1 root root 0 Aug  7 15:51 4.txt
100891250 -rw-r--r--. 1 root root 0 Aug  7 15:51 5.txt
100891219 -rw-r--r--. 1 root root 0 Aug  7 15:52 6.txt    #是不是很神奇?"6.txt"竟然用了之前的"3.txt"对应的inode节点信息,说明节点编号可以在删除文件后可以重复利用使用
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i              #很明显,不同文件对应的inode编号是不同的哟
[root@node101.yinzhengjie.org.cn ~]# cat name.txt       #查看文件中时有内容的
尹正杰
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file name.txt 
name.txt: UTF-8 Unicode text
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hexdump -C name.txt   #我们可以看到虽然我们存储的时文字,但是在底层依旧时二进制方式存储我们这里看到的是16进制的数字,会发现数据对应的底层编码对应的数据
00000000  e5 b0 b9 e6 ad a3 e6 9d  b0 0a                    |..........|
0000000a
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# shred -zvn 10 name.txt   #据说比rm命令删除的更彻底的命令,删除文件后,需要将原文件内容进行覆盖操作,我们使用-n参数来指定覆盖的次数。
shred: name.txt: pass 1/11 (random)...
shred: name.txt: pass 2/11 (aaaaaa)...
shred: name.txt: pass 3/11 (eeeeee)...
shred: name.txt: pass 4/11 (555555)...
shred: name.txt: pass 5/11 (249249)...
shred: name.txt: pass 6/11 (random)...
shred: name.txt: pass 7/11 (ffffff)...
shred: name.txt: pass 8/11 (000000)...
shred: name.txt: pass 9/11 (6db6db)...
shred: name.txt: pass 10/11 (random)...
shred: name.txt: pass 11/11 (000000)...
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hexdump -C name.txt     #很显然,发现数据被覆盖了,内容的确发生了改变
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001000
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# shred -zvn 10 name.txt   #据说比rm命令删除的更彻底的命令,删除文件后,需要将原文件内容进行覆盖操作,我们使用-n参数来指定覆盖的次数。
[root@node101.yinzhengjie.org.cn /boot/test]# pwd
/boot/test
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# df -i
Filesystem                 Inodes  IUsed     IFree IUse% Mounted on
/dev/mapper/centos-root  26214400 135458  26078942    1% /
devtmpfs                   478413    417    477996    1% /dev
tmpfs                      482689      1    482688    1% /dev/shm
tmpfs                      482689   1507    481182    1% /run
tmpfs                      482689     16    482673    1% /sys/fs/cgroup
/dev/sda1                  524288    342    523946    1% /boot
/dev/mapper/centos-home 233369600     10 233369590    1% /home
tmpfs                      482689     18    482671    1% /run/user/0
/dev/sr0                        0      0         0     - /run/media/root/CentOS 7 x86_64
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# ll
total 0
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# echo myfile{1..524288} | xargs touch         #我们这里批量创建524288个文件,我们知道每个文件都对应的唯一一个inode编号,因此会将"/boot"分区下的所有inode节点内容占满。还会抛出如下所示的异常。
......
touch: cannot touch ‘myfile524283’: No space left on device
touch: cannot touch ‘myfile524284’: No space left on device
touch: cannot touch ‘myfile524285’: No space left on device
touch: cannot touch ‘myfile524286’: No space left on device
touch: cannot touch ‘myfile524287’: No space left on device
touch: cannot touch ‘myfile524288’: No space left on device
[root@node101.yinzhengjie.org.cn /boot/test]#
[root@node101.yinzhengjie.org.cn /boot/test]# df -h                          #我们从上面的报错可以看出是设备没有足够的使用空间啦,我们可以通过"df"命令来查看相应的信息排查问题
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  4.3G   46G   9% /
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G   13M  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  454M  561M  45% /boot                          #我们发现"boot"分区使用率才45%呢,说明还有足够的存储数据的空间呀,但是就是往里面存不了数据啦!
/dev/mapper/centos-home  445G   33M  445G   1% /home
tmpfs                    378M   28K  378M   1% /run/user/0
/dev/sr0                  11G   11G     0 100% /run/media/root/CentOS 7 x86_64
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# df -i                          #我们查看各个分区的inode number使用情况
Filesystem                 Inodes  IUsed     IFree IUse% Mounted on
/dev/mapper/centos-root  26214400 135458  26078942    1% /
devtmpfs                   478413    417    477996    1% /dev
tmpfs                      482689      1    482688    1% /dev/shm
tmpfs                      482689   1507    481182    1% /run
tmpfs                      482689     16    482673    1% /sys/fs/cgroup
/dev/sda1                  524288 524288         0  100% /boot                    #找到原因了,原来是inode节点数使用率已经100%啦,当inode使用率达到100%时那就没法使用磁盘分区的空间啦。
/dev/mapper/centos-home 233369600     10 233369590    1% /home
tmpfs                      482689     18    482671    1% /run/user/0
/dev/sr0                        0      0         0     - /run/media/root/CentOS 7 x86_64
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# 
[root@node101.yinzhengjie.org.cn /boot/test]# df -i                          #我们查看各个分区的inode number使用情况

2>.inode表结构 

直接块指针(Direct Blocks):
  前12个直接指针,直接指向存储的数据区域,如Blocks大小为4096 bytes(实际上Linux操作系统默认的块大小就是4KB),则前12个直接指针就可以保存48KB.(12 * 4096)

间接块指针(Single Indirect):
  一级指针可存储大小计算,假设每个指针占用4个字节。则一级指针指向的Bloick可保存4096/4个指针,可指向1024个blocks,一级指针可存储文件数据大小为4MB.(1024*4096)

双重间接块指针(Double Indirect):
  二级指针可存储大小计算,同样按照Blocks大小为4096,则二级指针可保存的Block指针数量为(4096/4)*(4096/4)=1024*1024,则二级指针可保存的文件数量大小为4GB.((1024*1024)*4096)

三重间接块指针(Triple Indirect):
  三级指针可存储大小计算,以一级,二级指针计算方法类推,三级指针可存储的文件大小为4TB.

3>.目录

  文件引用一个是 inode号 
  人是通过文件名来引用一个文件
  一个目录是目录下的文件名和文件inode号之间的映射

  一张Inode 表如下图所示:

4>.cp和inode 

  分配一个空闲的inode号,在inode表中生成新条目  
  在目录中创建一个目录项,将名称与inode编号关联
  拷贝数据生成新的文件

5>.rm和inode 

  链接数递减,从而释放的inode号可以被重用  
  把数据块放在空闲列表中
  删除目录项
  数据实际上不会马上被删除,但当另一个文件使用数据块时将被覆盖

6>.mv和inode 

如果mv命令的目标和源在相同的文件系统,作为mv 命令  
  用新的文件名创建对应新的目录项
  删除旧目录条目对应的旧的文件名
  不影响inode表(除时间戳)或磁盘上的数据位置:没有数据被移动!

如果目标和源在一个不同的文件系统, mv相当于cp和rm

 

四.硬,软(符号)链接

1>.硬连接

  1>.创建硬链接会增加额外的记录项以引用文件,硬连接的本质就是对同一个分区的inode number创建多个不同的文件名。
  2>.对应于同一文件系统上一个物理文件
  3>.每个目录引用相同的inode号
  4>.创建时链接数递增
  5>.删除文件时:
      (1)rm命令递减计数的链接
      (2)文件要存在,至少有一个链接数
      (3)当链接数为零时,该文件被删除
  6>.不能跨越驱动器或分区,这是为什么呢?原因是各个不同的分区维护着不同的inode number数,而创建硬连接的本质就是不同的文件名指向了同一个inode number。就好比中国人有一张身份证,一个美国人在他们国家也有一个身份证,而且这个中国人和美国人身份证编号一模一样,但我们都清楚的知道这是2个不同的人,因为他们归属于不同的国家。各个国家维护这自己的身份证号。同理,咱们的操作系统中的不同分区也维护着不同的inode number。由于硬连接存在这样的一个缺陷,因此在生产环境中俺用的次数并不多~更多使用的是软连接。
  7>.语法:
      ln filename [linkname ]


温馨提示:(删除大文件要考虑对服务器性能的影响!!!)
  在生产环境中我们的数据库很轻松的就能超过1TB,甚至数据上PB的也有可能。在这个时候我们使用"rm -rf /data/mysql/mydb",可能会需要等待很长的时间。我们之前解释过关于执行"rm"命令操作系统会干啥了,其实在执行该命令时他会将inode number的引用计数减去1,与此同时会将1T空间的所有数据库都标记为空闲空间,我们之前计算过,假设一个block块为4k,那么1T的数据操作系统得去硬盘上将“268435456(1T*1024*1034*1024/4)”个(2.6亿个block)标记为空闲状态,此时CPU可能使用率会很高,而且会频繁的做I/O操作,在删除的过程中会严重影响操作系统的性能甚至死机的情况。
  如果这个时候你不想影响操作系统性能的话,其实有一个妙招就可以不用1秒的时间就可以把相应的数据"标记"为删除。这种方法就是先做一个硬连接,然后再把"/data/mmysql/mydb"目录删除掉。这样对应inode number的链接数会减去1,但不影响操作系统的使用。缺点就是数据并没有真正被删除,因为我们该inode number做了一个硬连接。当服务器空闲的时候(比如凌晨2~4点)再去删除数据。
[root@node101.yinzhengjie.org.cn ~]# ll -i                          #大家注意观察"name.txt"的inode数以及链接数。
total 4
100663386 -rw-r--r--. 1 root root 4096 Aug  7 16:45 name.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# stat name.txt                      #当然我们通过"stat"命令也可以看到相应的文件信息
  File: ‘name.txt’
  Size: 4096          Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 100663386   Links: 1                     #注意,这里的"Links"属性就是对应的链接数
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2019-08-07 16:45:48.296458127 +0800
Modify: 2019-08-07 16:45:45.743458291 +0800
Change: 2019-08-07 16:45:45.743458291 +0800
 Birth: -
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln name.txt name_bak                 #我们为文件做一个硬连接,这样就可以使得2个不同的文件名指向同一个inode number的编号
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i                          #我们仔细观察2个文件的inode编号发现是一致的哟,还要注意的是链接数也增加了"1"
total 8
100663386 -rw-r--r--. 2 root root 4096 Aug  7 16:45 name_bak
100663386 -rw-r--r--. 2 root root 4096 Aug  7 16:45 name.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "尹正杰到此一游" > name.txt            #我们往"name.txt"文件中重定向一行字符串,其本质就是找到"name.txt"的inode number编号,并在该编号指定的数据块中写入相应的数据
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i
total 8
100663386 -rw-r--r--. 2 root root 22 Aug  9 09:28 name_bak
100663386 -rw-r--r--. 2 root root 22 Aug  9 09:28 name.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat name.txt                       #由于我们往"name.txt"写入的数据,我们查看一下发现的确能看到相应的数据信息。
尹正杰到此一游
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat name_bak                       #由于"name_bak""name_txt"指向的是同一个"inode number",因此我们可以看到2个文件的内容是一致的哟~
尹正杰到此一游
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln name.txt name_bak                 #我们为文件做一个硬连接,这样就可以使得2个不同的文件名指向同一个inode number的编号
[root@node101.yinzhengjie.org.cn ~]# mkdir jason_dir                  #我们这里创建一个文件夹
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i
total 8
 68690933 drwxr-xr-x. 2 root root  6 Aug  9 10:08 jason_dir              #注意观察这里的inode number以及引用链接数,你有没有想问为什么链接数为2?那么接指往下看。
100663386 -rw-r--r--. 2 root root 22 Aug  9 09:28 name_bak
100663386 -rw-r--r--. 2 root root 22 Aug  9 09:28 name.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -ia jason_dir/
total 0
 68690933 drwxr-xr-x. 2 root root   6 Aug  9 10:08 .                  #大家看这里,注意观察"."目录对应的inode number是否是和"jason_dir"目录的一致。这就是为什么引用链接数位2的原因
100663361 dr-xr-x---. 7 root root 271 Aug  9 10:08 ..          
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mkdir jason_dir/test               #我们位"jason_dir"目录创建一个子文件夹
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -ia jason_dir/test/
total 0
100891213 drwxr-xr-x. 2 root root  6 Aug  9 10:09 .
 68690933 drwxr-xr-x. 3 root root 18 Aug  9 10:09 ..                  #大家发现么有?这里的".."对应的inode number是否似曾相识?没错,它就是指向了"jason_dir"目录所对应的inode number。
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i
total 8
 68690933 drwxr-xr-x. 3 root root 18 Aug  9 10:09 jason_dir
100663386 -rw-r--r--. 2 root root 22 Aug  9 09:28 name_bak
100663386 -rw-r--r--. 2 root root 22 Aug  9 09:28 name.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln jason_dir jason_dir2             #需要大家注意的是,目录是不允许做硬连接的,但是我们可以通过一些手短来不断的让目录的引用链接数增加,如上所演示。
ln: ‘jason_dir’: hard link not allowed for directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln jason_dir jason_dir2             #需要大家注意的是,目录是不允许做硬连接的,但是我们可以通过一些手短来不断的让目录的引用链接数增加,如上所演示。

2>.符号(或软)链接 

  1>.一个符号链接指向另一个文件 
  2>.ls - l的 显示链接的名称和引用的文件 
  3>.一个符号链接的内容是它引用文件的名称 
  4>.可以对目录进行 
  5>.可以跨分区 
  6>.指向的是另一个文件的路径;其大小为指向的路径字符串的长度;不增加或减少目标文件inode的引用计数 
  7>.语法:  
      ln -s  filename  ​​[linkname] 

温馨提示:
  建议大家使用相对路径做符号链接,我们会发现Linux操作系统默认的符号链接基本上都是相对路径,当然我们使用绝对路径做符号链接也是可以的,但在生产环境不推荐使用,因为存在迁移目录可能会到不到对应的文件的情况。具体情况可以看下面我给的案例。
    [root@node101.yinzhengjie.org.cn ~]# ll /etc/grub2.cfg                     #操作系统的开发人员们也喜欢相对路径做符号链接~
    lrwxrwxrwx. 1 root root 22 Aug  1 22:00 /etc/grub2.cfg -> ../boot/grub2/grub.cfg     #注意,这里的相对链接时针对符号链接的。
    [root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i          
total 0
34209525 drwxr-xr-x. 2 root root 71 Aug  9 10:34 jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls jason/
1.txt  2.txt  3.txt  4.txt  5.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln -s jason jason_link            #和硬连接不同,我们可以对目录做软连接而且还支持跨分区,主要原因是软(符号)连接和源文件的inode number是不一致的!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i
total 0
 34209525 drwxr-xr-x. 2 root root 71 Aug  9 10:34 jason
100663386 lrwxrwxrwx. 1 root root  5 Aug  9 10:35 jason_link -> jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls jason_link/
1.txt  2.txt  3.txt  4.txt  5.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -f jason_link                #只删除软链接文件,不要使用"-r"选项
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll -i
total 0
34209525 drwxr-xr-x. 2 root root 71 Aug  9 10:34 jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls jason/                    #发现数据还存在
1.txt  2.txt  3.txt  4.txt  5.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -rf jason_link/               #删除软连接目录下的所有数据,但不删除软链接符号!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls jason                     #发现数据没啦~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug  9 10:38 jason
lrwxrwxrwx. 1 root root 5 Aug  9 10:38 jason_link -> jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -rf jason_link               #虽然这里加了"-r"选线,但只删除软连接符号本身,因为它没在相对路径后面加"/"符号哟~可以和上面删除数据的作业给对比!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Aug  9 10:38 jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln -s jason jason_link            #和硬连接不同,我们可以对目录做软连接而且还支持跨分区,主要原因是软(符号)连接和源文件的inode number是不一致的!
[root@node101.yinzhengjie.org.cn ~]# ll                              #我在这里设计了2个目录
total 0
drwxr-xr-x. 7 root root 64 Aug  9 10:55 devops
drwxr-xr-x. 2 root root  6 Aug  9 10:57 my_web
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll my_web/    
total 0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat devops/script/start.sh                #看一下我写的模拟脚本
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie


echo "服务正在启动..."
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ./devops/script/start.sh                 #发现是可以执行的
服务正在启动...
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# pwd
/root
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln -s /root/devops/script/start.sh /root/devops/bin/start    #我这里使用绝对路径的方式做了一个符号链接
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ./devops/bin/start                            #在这里我们也看到了,的确是可以使用的
服务正在启动...
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv devops my_web/                            #我们这里将devops目录移至到"my_web/"目录下
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll ./my_web/devops/bin/start                     #我们将之前的目录移动到另外一个目录时,发现符号链接并没有跟着变化
lrwxrwxrwx. 1 root root 28 Aug  9 11:07 ./my_web/devops/bin/start -> /root/devops/script/start.sh
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ./my_web/devops/bin/start                      #不出所料,没法正常执行啦
-bash: ./my_web/devops/bin/start: No such file or directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv my_web/devops/ ./                         #于是,二话不说,我们目录移动回来
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -f /root/devops/bin/start                     #删除之前创建的符号链接
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ln -s ../script/start.sh devops/bin/start            #这一次我们以相对路径的方式创建符号链接,注意,这里的"../script/start.sh"是的路径是相对符号链接"devops/bin/start"而言的,大家不要搞混了,这并不是当前路径的相对路径哟~
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ./devops/bin/start                          #我们验证了下,使用符号链接的相对路径创建的符号链接时可以正常使用的
服务正在启动...
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll devops/bin/                            #查看我们创建的符号链接
total 0
lrwxrwxrwx. 1 root root 18 Aug  9 11:21 start -> ../script/start.sh
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 0
drwxr-xr-x. 7 root root 64 Aug  9 10:55 devops
drwxr-xr-x. 2 root root  6 Aug  9 11:19 my_web
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv devops my_web/                          #我们这里将devops目录移至到"my_web/"目录下
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ./my_web/devops/bin/start                      #发现它时可以正常启动的,因此我们推荐大家使用符号链接的相对路径,这样就算迁移目录了只要目录结果没有发生变化,那么符号链接依然可以使用
服务正在启动...
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
生产环境推荐大家使用符号链接的相对路径来创建软连接案例实操(经典案例,新手必看!!!)

3>.硬,软(符号)链接的区别

硬链接:
  硬链接的本质就是对同一个分区的inode number创建多个不同的文件名。
  硬链接不支持跨分区或者跨设备建立。
  硬链接不支持目录。
  硬链接创建(创建)后链接数会同时增长。
  源文件和硬链接之间的关系时平等的,不存在依赖关系,尽管源文件被删除了,硬链接文件依然时可以正常访问的。
  源文件的和硬链接的大小是一致的,包括属性都一致因为他们指向的是同一个inode number。
软(符号)链接:   软链接和硬连接并不是同一个文件,而是在软链接的存储数据中保存了源文件的名称而已。   软链接支持跨分区和跨设备建立。   软链接支持目录。   软链接创建(删除)后不影响链接数。   软链接和硬连接不关系并非平等,而是存在依赖关系,软链接以来源文件,当源文件被删除了,软链接也无法访问啦。
  软链接的文件大小其实就是其路径字符大小。和源文件大小无关。

温馨提示:
  创建链接的相对路径时应该是针对链接文件的相对路径而非当前路径的相对路径哟,具体案例可参考上面我提供新手必看的案例。


面试题:
  我几年前去一公司面试的时候人家问了我2个问题,现在我问问你,如果你学明白了本章节的知识,想必张口就能说出答案了,要是说不出来请静下心把本篇博客再看一遍,你会找到答案的。
  问题一:
      一台服务器挂在目录共有2T大小,现在使用了1.2T,还有0.8T可用空间,但是创建新文件时总是提示设备没有足够的空间,请问这是什么情况?为什么有足够的空间但就是无法使用呢?请排查原因。
  问题二:
      请问"ln -s /data/file1 /data/file2"和"mv /data/file1 /data/file2"之间的区别是什么?它们在底层做了哪些事?

 

五.确定文件内容 

  文件可以包含多种类型的数据 
  检查文件的类型,然后确定适当的打开命令或应用程序使用
  file [options] <filename>...
  常用选项:
    -b 列出文件辨识结果时,不显示文件名称
    -f filelist 列出文件filelist中文件名的文件类型
    -F 使用指定分隔符号替换输出文件名后默认的”:”分隔符
    -L 查看对应软链接对应文件的文件类型
    --help 显示命令在线帮助
[root@node101.yinzhengjie.org.cn ~]# ll
total 24
-rw-r--r--. 1 root root     6 Aug  9 12:12 a.txt
drwxr-xr-x. 3 root root    20 Aug  9 11:22 my_web
-rw-r--r--. 1 root root    22 Aug  9 12:11 name.txt
-r--r--r--. 1 root root 14120 Aug  9 12:10 smiley.jpg
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.txt                  #只保存了字母的文件
jason
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file a.txt                 #查看“a.txt”的文件类型,根据输出可以判断该文件的存储类型
a.txt: ASCII text                                   #它告诉我们这是使用ASCII编码的文本文件
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat name.txt 
尹正杰到此一游
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file name.txt 
name.txt: UTF-8 Unicode text
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file smiley.jpg               #查看图片文件
smiley.jpg: JPEG image data, JFIF standard 1.01
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file my_web                 #目录也可以查看
my_web: directory
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file a.txt                  #查看“a.txt”的文件类型,根据输出可以判断该文件的存储类型
[root@node101.yinzhengjie.org.cn ~]# file a.txt 
a.txt: ASCII text
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -b a.txt                #只显示识别文件的类型,不显示文件名称
ASCII text
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -b a.txt                #只显示识别文件的类型,不显示文件名称
[root@node101.yinzhengjie.org.cn ~]# cat test.txt               #定义文件的路径时候我们要一行一个
/dev/null
/root/a.txt
/dev/sr0
/dev/sda1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -f test.txt            #列出指定文件中的列出的文件类型
/dev/null:   character special
/root/a.txt: ASCII text
/dev/sr0:    block special
/dev/sda1:   block special
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -f test.txt              #列出指定文件中的列出的文件类型
[root@node101.yinzhengjie.org.cn ~]# cat test.txt 
/dev/null
/root/a.txt
/dev/sr0
/dev/sda1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -f test.txt              #不难发现,输出的文件和文件类型的分隔符默认都是":"
/dev/null:   character special
/root/a.txt: ASCII text
/dev/sr0:    block special
/dev/sda1:   block special
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -F ===== -f test.txt        #我们可以不适用默认的分隔符,可以使用"-F"参数来指定文件名和文件类型之间的分隔符
/dev/null=====   character special
/root/a.txt===== ASCII text
/dev/sr0=====    block special
/dev/sda1=====   block special
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -F ===== -f test.txt         #我们可以不适用默认的分隔符,可以使用"-F"参数来指定文件名和文件类型之间的分隔符
[root@node101.yinzhengjie.org.cn ~]# ll /etc/grub2.cfg               #我们先来找一个链接文件测试
lrwxrwxrwx. 1 root root 22 Aug  1 22:00 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file /etc/grub2.cfg              #如果我们查看的是一个链接文件,file就会告诉咱们它是一个链接文件
/etc/grub2.cfg: symbolic link to `../boot/grub2/grub.cfg'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -L /etc/grub2.cfg             #查看软连接对应的文件类型,即他回去找软连接对应的源文件的类型
/etc/grub2.cfg: ASCII text
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file -L /etc/grub2.cfg           #查看软连接对应的文件类型,即他回去找软连接对应的源文件的类型
[root@node101.yinzhengjie.org.cn ~]# file --help                  #查看file的帮助信息
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.

      --help                 display this help and exit
  -v, --version              output version information and exit
  -m, --magic-file LIST      use LIST as a colon-separated list of magic
                               number files
  -z, --uncompress           try to look inside compressed files
  -b, --brief                do not prepend filenames to output lines
  -c, --checking-printout    print the parsed form of the magic file, use in
                               conjunction with -m to debug a new magic file
                               before installing it
  -e, --exclude TEST         exclude TEST from the list of test to be
                               performed for file. Valid tests are:
                               ascii, apptype, compress, elf, soft, tar, tokens, troff
  -f, --files-from FILE      read the filenames to be examined from FILE
  -F, --separator STRING     use string as separator instead of `:'
  -i, --mime                 output MIME type strings (--mime-type and
                               --mime-encoding)
      --apple                output the Apple CREATOR/TYPE
      --mime-type            output the MIME type
      --mime-encoding        output the MIME encoding
  -k, --keep-going           don't stop at the first match
  -l, --list                 list magic strength
  -L, --dereference          follow symlinks (default)
  -h, --no-dereference       don't follow symlinks
  -n, --no-buffer            do not buffer output
  -N, --no-pad               do not pad output
  -0, --print0               terminate filenames with ASCII NUL
  -p, --preserve-date        preserve access times on files
  -r, --raw                  don't translate unprintable chars to \ooo
  -s, --special-files        treat special (block/char devices) files as
                             ordinary ones
  -C, --compile              compile file specified by -m
  -d, --debug                print debugging messages

Report bugs to http://bugs.gw.com/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# file --help                  #查看file的帮助信息

 

posted @ 2019-08-05 12:39  尹正杰  阅读(564)  评论(0编辑  收藏  举报