File permissions management and related command

First, the file-related commands

1.rz and sz command

rz copy the file from Windows to Linux in
sz copy files from Linux to Windows,

Install the tool:
①lsblk view the disc mount path
②rpm -ivh /media/CentOS_6.9_Final/Packages/lrzsz-0.12.20-27.1.el6.x86_64.rpm
Note: No need to enter the full path of the file name, more use of the tab key

2.stat Display file or file system status. Display file or file system of state property

stat [OPTION]… FILE…

access time: access time, atime, read the contents of the file
modify time: modification time, mtime, change the contents of the file (data)
Change Time: to change the time, ctime, metadata changes

Create a file 3.touch

- A atime and changing only the ctime
 - m mtime and changing only the ctime
 - T [[the CC] YY] MMDDhhmm [.SS] Specifies mtime and atime timestamps
 -c If the file does not exist, creating not

4.cp copy files

cp [OPTION]… [-T] SOURCE DEST
cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…

. 1  CP -a = Same AS -dR --preserve All all copy, the backup file can be used for
 2  CP -d Same --preserve AS = the dereference --no- links the source file is not copied, only the link copy
 . 3  CP -i if replication covering another file, it will prompt before overwriting (Note: the average user $ default without - i)
 4  cp -n    do not Overwrite existing AN file 
5  cp -p = equivalent --preserv the MODE, Ownership, timestamp authority to retain, owner, timestamps,
 6  cp -f --force, IF AN Where do you want existing File CAN not BE the Opened, the Remove IT and the try Again (the this the Option at The IS ignored the when - the n-IS Also Used the Option)
 7  cp -v  显示复制过程
8 cp -u   copy only when the SOURCE file is newer than the destination file or when the destination file is missing
9 cp -r (-R) --recursive,copy directories recursively

Here Insert Picture Description

note:

cp / etc / hosts / data / test / hosts file to indicate Copy / data / test / directory, provided that the test directory exists, if there will be an error!
cp / etc / hosts / data / test shows a copy to the hosts file / data directory, rename it to test (so also with the rename function cp command), if the file exists directly covered test

 

  • Exercise:
  1. Define an alias command baketc, day, all the files under / etc / directory, to the backup / testdir separate subdirectory, and a subdirectory required format backupYYYY-mm-dd, visible backup process
Backup = Alias ' CP -av / etc / Data / the testdir / backup`date +% F` ' 
in order to save the permanent file .bashrc
  1. Created under / testdir / rootdir directory, and copy / root to all files within that directory, preserving permits required
cp -a  /root  /data/rootdir

5. mv move, rename files (move (rename) files)

mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE…

6.rm delete files (remove files or directories)

Tip:
When you rm alias mv to / data directory, and then execute the rm command, to be deleted automatically to the next file mv / data file
alias rm='mv -t /data' operations more insurance
rm -rf 强制递归删除目录树 (慎用)

7.tree display the directory tree

Tree - D only show the directory 
Tree - L Level: Displays the specified number of hierarchical 
Tree -P pattern: display only matching to the specified path patter

Create a directory 8.mkdir

mkdir - : does not exist in the p-error, and can automatically create the required each directory
 mkdir - Show Details: v
 mkdir assign permissions directly when creating a table of contents: -m MODE

9.rename batch rename

rename [options] expression replacement file…

the rename    " conf "    " conf.bak "    File * represents all open files for all file, the file name of all conf renamed conf.bak

10.file determine the file type

File - b When the identification result lists files, the file name is not displayed
 File - F filelist filelist file type listed in the file name
 File - F. Alternatively delimiter specified output file name default ":" separator
 File -L Check the corresponding soft link corresponding to the file type

 

Second, the file system


File system structure diagram

Some Features 1. File System

  •    File and directory structure to form a stump upside down
  • File begins with "/" under
  • File names are case sensitive

    (Note: The distinction between reason sensitive non-Linux systems, but the file system uses ext4 and xfs formats reason to copy files from a Windows fat32 is not case-sensitive.)

  • "." At the beginning of the file hidden file, can be expressed as a wildcard. *
  • Linux everything is a file, the file reflects begin with form
  • There are two types of data files:

    Metadata (metadata): describing data, the attribute information describes the data, such as size, file name, file permissions, creation time, modification time and other information.
    Data (data):

  • Based Linux configuration directory: FHS (Filesystem Hierarchy Standard)

    If you have time to see the details: http: //www.pathname.com/fhs/

  • File names up to 255 bytes
  • 一般,蓝色——目录、绿色——可执行文件,红色——压缩文件、浅蓝色——链接文件,等等

2.一些主要文件夹说明

  1. /boot : 引导文件存放目录,内核文件(vmlinuz)、引导加载器(bootloader, grub)都存放于此目录
  2. /bin :供所有用户使用的基本命令;不能关联至独立分区,OS启动即会用到的程序
  3. /sbin:管理类的基本命令;不能关联至独立分区,OS启动即会用到的程序,root权限下才可以使用
  4. /lib,/lib64:库文件,启动时程序依赖的基本共享库文件以及内核模块文件
  5. /etc:配置文件
  6. /root:管理员的家目录
  7. /home:普通用户的家目录
  8. /media:便携式移动设备挂载点
  9. /dev:设备文件及特殊文件
b,block device:随机访问
c, character device:线性访问
  1. /tmp:临时存储文件
  2. /usr:拥有第二层的FHS文件设置,universal shared, read-only data
/usr/bin:所有一般用户能够使用的指令都放在这里!CentOS 7 已经将全部的使用者指令放置于此,而使用链接文件的方式将 /bin 链接至此!也就是说, /usr/bin 与 /bin 是一模一样了!
/usr/share:主要放置只读架构的数据文件,当然也包括共享文件。在这个目录下放置的数据几乎是不分硬件架构均可读取的数据。在此目录下常见的还有这些:/usr/share/man;线上说明文档 /usr/share/doc:软件杂项的文件/usr/share/zoneinfo;与时区有关的时区文件
/usr/local:第三方应用程序的安装位置,其下也有bin、lib、etc、share等文件
  1. /var:variable data files,拥有第二层FHS目录结构
cache: 应用程序缓存数据目录
lib: 应用程序状态信息数据
local:专用于为/usr/local下的应用程序存储可变数据;
lock: 锁文件
log: 日志目录及文件
opt: 专用于为/opt下的应用程序存储可变数据;
run: 运行中的进程相关数据,通常用于存储进程pid文件
spool: 应用程序数据池
tmp: 保存系统两次重启之间产生的临时数据

3.Linux下的文件类型:

-:普通文件
d: 目录文件
b: 块设备
c: 字符设备. 如 ll /dev/zero. 该类文件可以迅速在磁盘中填充一个指定容量的文件或创建大文件.
  dd if=/dev/zero of=/data/bigfile bs=1M count=1024 表示在data目录中创建一个bigfile的1G大的文件
  其中,dd为命令,if为input file,of为output file,bs为 block size
  由于此文件问二进制文件,cat命令无法看,需要用hexdump命令查看二进制文件
  hexdump -c /data/bigfile
l: 符号链接文件
p: 管道文件pipe
s: 套接字文件socket

三、iNode(index node)索引节点

以下内容转自阮一峰理解iNode

1.iNode是什么

文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector)。每个扇区储存512字节(相当于0.5KB)。

操作系统读取硬盘的时候,不会一个个扇区地读取,这样效率太低,而是一次性连续读取多个扇区,即一次性读取一个"块"(block)。这种由多个扇区组成的"块",是文件存取的最小单位。"块"的大小,最常见的是4KB,即连续八个 sector组成一个 block。

文件数据都储存在"块"中,那么很显然,我们还必须找到一个地方储存文件的元信息,比如文件的创建者、文件的创建日期、文件的大小等等。这种储存文件元信息的区域就叫做inode,中文译名为"索引节点"。

每一个文件都有对应的inode,里面包含了与该文件有关的一些信息。

2.iNode的内容

node包含文件的元信息,具体来说有以下内容:

  • 文件的字节数

  • 文件拥有者的User ID

  • 文件的Group ID

  • 文件的读、写、执行权限

  • 文件的时间戳,共有三个:

    ctime指inode上一次变动的时间,
    mtime指文件内容上一次变动的时间,
    atime指文件上一次打开的时间。

    可以用stat命令,查看某个文件的inode信息:

Here Insert Picture Description

3.iNode大小

inode也会消耗硬盘空间,所以硬盘格式化的时候,操作系统自动将硬盘分成两个区域。一个是数据区,存放文件数据;另一个是inode区(inode table),存放inode所包含的信息。

每个inode节点的大小,一般是128字节或256字节。inode节点的总数,在格式化时就给定,一般是每1KB或每2KB就设置一个inode。假定在一块1GB的硬盘中,每个inode节点的大小为128字节,每1KB就设置一个inode,那么inode table的大小就会达到128MB,占整块硬盘的12.8%。
(128M是这么来的,1k/1G=128B/128M)

1 df -i   查看硬盘分区可用的iNode总数和已经使用的数量
2 sudo dumpe2fs -h /dev/hda | grep "Inode size"    查看每一个节点的大小

注意:由于每个文件都必须有一个inode,因此有可能发生inode已经用光,但是硬盘还未存满的情况。这时,就无法在硬盘上创建新文件。实验如下:

1 echo file{1..524288} | xargs touch  表示在当前文件夹下创建file1,file2....file524288个文件,将iNode全部用光
2 echo file*|xargs rm  删除当前文件夹内file开头的文件

Here Insert Picture Description

可见,当iNode号码用光后无法再创建新的文件

4.inode号码及系统打开文件的过程

每个inode都有一个号码,操作系统用inode号码来识别不同的文件。

这里值得重复一遍,Unix/Linux系统内部不使用文件名,而使用inode号码来识别文件。对于系统来说,文件名只是inode号码便于识别的别称或者绰号。

表面上,用户通过文件名,打开文件。实际上,系统内部这个过程分成三步:首先,系统找到这个文件名对应的inode号码;其次,通过inode号码,获取inode信息;最后,根据inode信息,找到文件数据所在的block,读出数据。

1 ls -i     #查看当前文件夹下各个文件的inode号码
2 ls -i /data/file1   #查看/data/file1文件的inode号码

 

5.目录文件

Unix/Linux系统中,目录(directory)也是一种文件。打开目录,实际上就是打开目录文件。

目录文件的结构非常简单,就是一系列目录项(dirent)的列表。每个目录项,由两部分组成:所包含文件的文件名,以及该文件名对应的inode号码。
/etc目录下各文件的inode号码
Here Insert Picture Description

/etc这个目录文件本身的inode号码
Here Insert Picture Description

目录文件的读权限(r)和写权限(w),都是针对目录文件本身。由于目录文件内只有文件名和inode号码,所以如果只有读权限,只能获取文件名,无法获取其他信息,因为其他信息都储存在inode节点中,而读取inode节点内的信息需要目录文件的执行权限(x)。

6.硬链接(hard link)

一般情况下,文件名和inode号码是"一一对应"关系,每个inode号码对应一个文件名。但是,Unix/Linux系统允许,多个文件名指向同一个inode号码。

这意味着,可以用不同的文件名访问同样的内容;对文件内容进行修改,会影响到所有文件名;但是,删除一个文件名,不影响另一个文件名的访问。这种情况就被称为"硬链接"(hard link)。
ln 源文件 目标文件

1 ln a b    
2 ln a c   a b c三个文件完成了硬链接

实验,a,b,c三个文件完成了硬链接,现在修改b文件内容,结果如何?

Here Insert Picture Description
可见,对硬链接的文件,修改任何一个三个文件的大小均有变动,但inode号码保持相同且不变

注意:由于inode号码在各分区之间互相独立,多以硬链接不可以跨分区创建
这里顺便说一下目录文件的"链接数"。创建目录时,默认会生成两个目录项:".“和”…"。前者的inode号码就是当前目录的inode号码,等同于当前目录的"硬链接";后者的inode号码就是当前目录的父目录的inode号码,等同于父目录的"硬链接"。所以,任何一个目录的"硬链接"总数,总是等于2加上它的子目录总数(含隐藏目录)。

7.软链接(可以理解成windows的快捷方式)

文件A和文件B的inode号码虽然不一样,但是文件A的内容是文件B的路径。读取文件A时,系统会自动将访问者导向文件B。因此,无论打开哪一个文件,最终读取的都是文件B。这时,文件A就称为文件B的"软链接"(soft link)符号链接(symbolic link)

ln -s命令可以创建软链接。

Here Insert Picture Description

注意:创建软链接时,注意绝对路径与相对路径的区分

8.inode的特殊作用

由于inode号码与文件名分离,这种机制导致了一些Unix/Linux系统特有的现象。

1. 有时,文件名包含特殊字符,无法正常删除。这时,直接删除inode节点,就能起到删除文件的作用。

2. 移动文件或重命名文件,只是改变文件名,不影响inode号码。

3. 打开一个文件以后,系统就以inode号码来识别这个文件,不再考虑文件名。因此,通常来说,系统无法从inode号码得知文件名。

第3点使得软件更新变得简单,可以在不关闭软件的情况下进行更新,不需要重启。因为系统通过inode号码,识别运行中的文件,不通过文件名。更新的时候,新版文件以同样的文件名,生成一个新的inode,不会影响到运行中的文件。等到下一次运行这个软件的时候,文件名就自动指向新版文件,旧版文件的inode则被回收。

三、练习

  1. 如何创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b
mkdir -p /testdir/dir1/{x,y}/{a,b}
tree

└── testdir
    └── dir1
        ├── x
        │   ├── a
        │   └── b
        └── y
            ├── a
            └── b
  1. 如何创建/testdir/dir2/x,/testdir/dir2/y,/testdir/dir2/x/a,/testdir/dir2/x/b
mkdir -p  /testdir/dir2/{x/{a,b},y}

└── testdir
    └── dir2
        ├── x
        │   ├── a
        │   └── b
        └── y
  1. 如何创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7
mkdir  -p  /testdir/{dir3,dir4,dir5/{dir6,dir7}}
mkdir -p  /testdir/dir{3,4,5/{6,7}}

 Transfer: https://blog.csdn.net/tai20031229/article/details/83720565

Guess you like

Origin www.cnblogs.com/liuzhiyun/p/11249346.html