文件管理(一)

    1.文件管理

    2.硬链接和软链接

    3.扩展

文件管理

文件有两类数据:
元数据:metadata 数据描述信息
数据:data 数据信息本身

文件命名规则
文件名最长255个字节
包括路径在内文件名称最长4095个字节

[root@zbxmaster ~]# stat ss.log   #显示文件或文件系统状态
  File: ‘ss.log’
  Size: 9039            Blocks: 24         IO Block: 4096   regular file
Device: 802h/2050d      Inode: 101005465   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-09-24 17:50:09.133743159 +0800
Modify: 2019-04-04 08:35:37.000000000 +0800
Change: 2019-09-24 17:50:02.174742946 +0800
 Birth: -

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

file [OPTION…] [FILE…]
   描述:查看文件类型
常用选项

-f filelist 从指定文件中批量查看
-L filename 查看真实文件类型

及时释放磁盘空间

删除文件并立即释放空间
[centos7@localhost]# cat /dev/null > /dirnumber ;
[centos7@localhost]# rm -rf /dirnumber;

或覆盖掉该文件后再删除
[centos7@localhost]# > /dirnumber
[centos7@localhost]# rm -rf /dirnumber

rename 批量修改文件

[root@Centos6 data]# touch f{1..10}.txt
[root@Centos6 data]# type rename
rename is hashed (/usr/bin/rename)
[root@Centos6 data]# ls
f10.txt  f1.txt  f2.txt  f3.txt  f4.txt  f5.txt  f6.txt  f7.txt  f8.txt  f9.txt
[root@Centos6 data]# rename txt txt.bak f* #f开头的文件
[root@Centos6 data]# ls
f10.txt.bak  f1.txt.bak  f2.txt.bak  f3.txt.bak  f4.txt.bak  f5.txt.bak  f6.txt.bak  f7.txt.bak  f8.txt.bak  f9.txt.bak
[root@Centos6 data]# rename txt.bak txt * #也可不加
[root@Centos6 data]# ls
f10.txt  f1.txt  f2.txt  f3.txt  f4.txt  f5.txt  f6.txt  f7.txt  f8.txt  f9.txt

文件误删除
rm命令容易误删除文件,建立不是用直接把rm定义别名为mv,简单.shell脚本实现,类似于windos的回收站

注:别名在脚本中定义了不会生效,写入脚本,放入/root/.bashr或/etc/bashrc中有效

(1)写入环境变量

需要给文件加上执行权限
(2)直接赋值

[root@centos7 ~]# alias rm=/root/shell/rm19.sh
[root@centos7 2019-09-30_1569832888]# cat /root/shell/rm19.sh 
#!/bin/bash
#
Time=`date '+%F_%s'`
Destination="/tmp"

mkdir -p $Destination/$Time
mv $* $Destination/$Time
echo "文件已删除.你删除的文件已放入该目录 $Destination/$Time"
[root@centos7 2019-09-30_1569832888]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias hjq='who -m'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='/root/shell/rm19.sh'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@centos7 ~]# rm f2
文件已删除.你删除的文件已放入该目录 /tmp/2019-09-30_1569832888
[root@centos7 2019-09-30_1569832888]# ll
total 4
-rw-r--r--. 1 root root 2192 Jul 21 14:16 f2

rmdir 逆序删除空目录

rmdir [-p] dirName
参数:
-p 是当子目录被删除后使它也成为空目录的话,则顺便一并删除。
例:
rmdir AAA #将工作目录下,名为 AAA 的子目录删除
rmdir -p BBB/Test #在工作目录下的 BBB 目录中,删除名为Test 的子目录。若 Test 删除后,BBB 目录成为空目录,则BBB 亦予删除。

shred多次覆盖数据文件,达到彻底删除的目的

[Centos7@localhost ~]# shred -zvn5 f2.txt  #避免文件被恢复
shred: f2.txt: pass 1/6 (random)...
shred: f2.txt: pass 2/6 (ffffff)...
shred: f2.txt: pass 3/6 (random)...
shred: f2.txt: pass 4/6 (000000)...
shred: f2.txt: pass 5/6 (random)...
shred: f2.txt: pass 6/6 (000000)...

Inode
分区中节点编号唯一,不同分区中可以出现相同的节点编号

查看Inode

[root@centos7 ~]# ll -i 
[root@centos7 ~]# stat f1.txt  
注: stat [OPTION]...FILE...   
显示文件或文件系统状态
-L, --dereference 链接
-Z, --context 打印SELinux安全上下文
-f, --file-system 显示文件系统状态而不是文件状态
-c --format=FORMAT 使用指定的格式而不是默认格式;
-t, --terse 以简洁的形式打印信息

Inode表结构:直接指针48K数据;间接指针4M数据;双重间间接指针4G;依次叠加

inode表会占文件系统磁盘空间1%; 目录的内容就是该目录下文件的inode表集合

节点编号动态分配

ACL权限
扩展传统权限 setfacl
setfacl -b f1 彻底删除setfacl权限
例: setfacl -m -u|g:mage:rwx f1 可以是用户也可以是组

#getfacl file 查看文件访问控制表

设置了访问控制权限后第二位rwx不是组的权限而是mask的权限,mask相当于是一个限制权限的标杆,可以限制已经生效的文件,如果后面又加了mask以外的权限,就会认为失效,从而不会影响限制

复制ACL文件权限到另一文件
[root@CentOS7 testdir]# getfacl ./hjq/f2 | setfacl --set-file=- f2

setfacl -m 修改当前文件的ACL
setfacl -k dir 删除默认ACL权限
setfacl –b file1清除所有ACL权

ACL 文件生效次序
文件所有者 --> 文件自定义用户 --> 文件自定义组 --> 其他人

ACL文件权限可补救root失去chmod的执行权限造成无法赋值
示例:

[root@centos7 shell]# ll /usr/bin |grep chmod
-rw-r-xr-x.   1 root root      58656 Oct 31  2018 chmod
[root@centos7 shell]# chmod -x /usr/bin |grep chmod  #误操作丢失文件权限
[root@centos7 shell]# ll /usr/bin |grep chmod
-rw-r--r--.   1 root root      58656 Oct 31  2018 chmod
[root@centos7 shell]# setfacl -m u:root:rwx /usr/bin/chmod #添加ACL权限 添加后多了一个"+"
[root@centos7 shell]# ll /usr/bin |grep chmod
-rw-rwxr--+   1 root root      58656 Oct 31  2018 chmod
[root@centos7 shell]# chmod +x /usr/bin/chmod #添加源权限
[root@centos7 shell]# ll /usr/bin |grep chmod 
-rwxrwxr-x+   1 root root      58656 Oct 31  2018 chmod
[root@centos7 shell]# setfacl -b /usr/bin/chmod #删除ACL权限
[root@centos7 shell]# ll /usr/bin |grep chmod   
-rwxr--r-x.   1 root root      58656 Oct 31  2018 chmod

 
 

文硬链接和软链接

硬链接

ln f4.txt f44.txt
同一个文件名取多个名称
[root@CentOS8 ~]# ln anaconda-ks.cfg an.cfg
[root@CentOS8 ~]# ln anaconda-ks.cfg anac.cfg
[root@CentOS8 ~]# ll
total 16
-rw-------. 3 root root 1442 Sep 25 10:24 anac.cfg
-rw-------. 3 root root 1442 Sep 25 10:24 anaconda-ks.cfg
-rw-------. 3 root root 1442 Sep 25 10:24 an.cfg
-rw-r--r--. 1 root root 1597 Sep 25 10:27 initial-setup-ks.cfg

注:文件名之间没有主次之分只是名称不同,互补影响,删除后数字会减少一个

相对路径: 相对于当前目录路径
不允许跨分区跨设备创建,不能针对文件夹创建硬链接,否则会导致混合,只能对文件创建。每个文件夹至少都有两个层级,即本文件和下级的. ;三个则是,本目录和字母再加上下级的.

[root@CentOS8 data]# ll
total 12
drwxr-xr-x. 133 root root 8192 Sep 25 10:27 backup2019-09-25
-rw-r--r--.   1 root root    0 Sep 25 14:09 f3.txt
dr-xr-x---.   5 root root  217 Sep 25 10:31 rootdir

[root@CentOS8 rootdir]# ll -a
total 36
dr-xr-x---. 5 root root  217 Sep 25 10:31 .
drwxr-xr-x. 4 root root   59 Sep 25 14:09 ..

[root@CentOS8 rootdir]# cd ..
[root@CentOS8 data]# 

软连接可跨分区的快捷方式

ln -s /src /dst
文件或文件夹均可以创建软连接
/src 建议写相对路径,写相对路径也是针对于/dst目录的/scr相对路径
相对路径相对于软连接的目录
也可以写绝对路径,但一旦发生改变,链接全部失效

删除软链接

rm -rf lndir 删除链接本身
rm -ef lndir/ 删除原目录中的数据

 
硬链接和软连接的区别

1本质
硬: 是同一个文件,不同名称
软: 不是同一个文件,源地址相同
2跨分区
硬: 仅在当前分区有效
软: 可跨分区
3inode
硬: inode号相同
软: inode号不同
4链接数
硬: 链接数随链接个数增长
软: 不会增加
5文件夹
硬: 不能对文件夹创建
软:  可以创建
6相对路径
硬: 相对于当前目录
软: 相对于目标文件的相对路径
7删除原文件
硬: 删除单个不会失效,所有删除就删除了
软: 一旦删除立即失效
8文件类型
硬: -rw-r--r--
软: lrwxrwxrwx

扩展

   永久定义bashe变量

1、写入变量到配置文件中
(1)、[centos7@localhost]# vim ~.bashrc 永久写入当前用户
(2)、[centos7@localhost]# vim /etc/bashrc 所有用户
2、重读配置文件到内存或重启
[centos7@localhost]# source .bashrc
[centos7@localhost]# . .bashrc

命令的键入

Ctrl+d 正常退出
Ctrl+c 强制退出

; 多个命令一起执行 无法检测到错误,都会向下执行,不严谨

[root@centos7 ~]# cat f9;cat f199;cat f7
admin
cat: f199: No such file or directory
admin1
admin2

\ 过长的命令分行处理

[root@Centos7 /etc/quagga]#w\
> h\
> o
root     pts/0        2019-09-23 08:03 (192.168.146.1)
root     tty1         2019-09-22 19:52
root     pts/1        2019-09-23 08:03 (192.168.146.1)  

whatis:显示命令简介

[centos7@localhost]#whatis command
[centos7@localhost]依赖系统数据库,数据库系统自动加载时间不定
[centos7@localhost]#mandb 手动更新库
[centos7@localhost]#reset 初始化终端(终端界面乱码时使用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值