文件有两类数据:元数据【metadata】,数据【data】。
以下为元数据第一个字符的标识:
- 普通文件 实例:-rwxr-xr-x. 1 root root 81 May 15 20:55 test.sh
d 目录文件 实例:drwxr-xr-x. 139 root root 8192 May 18 19:45 /etc/
b 块设备文件 实例: brw-rw----. 1 root disk 8, 0 May 18 08:26 /dev/sda
c 字符设备文件 实例:crw-rw-rw-. 1 root tty 5, 0 May 18 08:26 /dev/tty
l 软链接文件 实例:lrwxrwxrwx. 1 root root 7 May 15 12:14 /sbin/clock -> hwclock
p 管道文件 实例:prw-r--r--. 1 root root 0 May 18 19:58 ok 【可使用mkfifo创建】
s 套接字文件 实例:srw-rw-rw-. 1 root root 0 May 18 08:26 /run/rpcbind.sock
-----------------------------------------------------------
pwd 显示当前所在的绝对路径
-P #如果当前所在位置是软链接目录,则显示源路径
实例:
[root@centos7 ~]# cd /etc/init.d/
[root@centos7 init.d]# pwd -P
/etc/rc.d/init.d
绝对路径与相对路径
# 绝对:从根开始的路径
[root@centos7 ~]# pwd
/root #绝对路径是/root
# 相对:相对于当前的路径
[root@centos7 ~]# cd Downloads/ #当前目录为/root/,Downloads是相对路径。
[root@centos7 Downloads]# pwd
/root/Downloads
----------------------------------------------------------------
#获取目录名 dirname
实例
[root@centos7 ~]# dirname /etc/sysconfig/network-scripts/ifcfg-ens33
/etc/sysconfig/network-scripts
#获取基名 basename
实例:
[root@centos7 ~]# basename /etc/sysconfig/network-scripts/ifcfg-ens33
ifcfg-ens33
-------------------------------------------------------------
cd 切换目录命令
cd 进入家目录
cd ~ 进入家目录
cd - 进入上一次的工作目录,反复执行会在两个目录间来回切换
实例:
[root@centos7 boot]# pwd #当前目录boot
/boot
[root@centos7 boot]# cd ~ #进入家root目录
[root@centos7 ~]# pwd
/root
[root@centos7 ~]# cd - #返回boot目录
/boot
-------------------------------------------------------------------
ls
[options][fils or directories]
-a 显示所有文件,包含隐藏文件
-A 显示所有文件,包含隐藏文件,除了. and ..
-l 长列出,显示属性信息
-dl 详细指定目录自己的属性信息,而不是显示子文件,子目录信息
-R 递归
-1 以一列显示
-S 按文件大小从大到小排序
-tl 默认以修改时间从新到旧排序,而如果使用--time=a[c]time,则按所指定的时间从新到旧排序
-utl 以atime从新到旧排序 也可以用--time=atime -tl
不加t只有ul存在时,表示以atime来显示详细信息
-rS 按文件大小从小到大 -rt 按时间从旧到新
-U 按文件的存放顺序显示
-X 按文件后缀排序
-i 显示文件的inode号
-Z 显示context
---------------------------------------------------------------------------------
stat 查看时间戳
access time(atime): 访问时间,当文件被读取时才更新
modify time(mtime): 数据修改时间,当文件的数据被修改时才更新
change time(ctime): 元数据修改时间,当文件的元数据(权限,大小,owner,group,mtime等)更新时则更新
stat ,ls --time=atime[ctime] -l
touch filename
若文件不存在,则创建一个空文件
若文件存在,则更新三个时间戳
-a 更新atime和ctime
-m 更新mtime和ctime
-c 若文件存在,则更新三个时间戳,若不在,不会创建新文件,无作为。
----------------------------------------------------------
文件名通配符
*:匹配零或多个任意字符
?:匹配任意一个单一字符
0-9:表示任何数字
a-Z:表示所有字母,包括大小写
[linux]:表示包含l或i或n或u或x
[^linux]:表示不包含l和i和n和u和x
[:digit:]:表示任意数字
[:lower:]:表示小写字母
[:upper:]:表示大写字母
[:alpha:]:表示大小写字母
[:alnum:]:表示任意字母或数字
[:blank:]:表示空格(水平制表符)及TAB
[:space:]:表示空格(水平制表符)、回车(垂直制表符)及TAB
[:punct:]:表示符号
[:print:]:表示可打印字符
----------------------------------------------------------------------------------------
cp
cp
当源是一个文件时
当目标文件不存时,则新建文件,并填充内容
当目标文件存在,且为一个文件时,则覆盖该文件,且为一个目录时,则将该文件放置入该目录
当源是多个文件时
当目标文件不存时,报错
当目标文件存在,且为一个文件时,报错 ,且为一个目录时,则将所有源目文件放置入该目录
当源是一个目录时, 要配合-r
当目标不存在时,则创建指定名称的目录,并将源目录当中的文件复制到新目录当中
当目标文件存在,且为一个文件时,报错 ,且为一个目录时,则两个目录内容合并,如有同名文件,则源目录下的文件覆盖目标目录的文件。
选项
-i 覆盖时交互式提示
-r 递归,可以复制目录
-d 默认不加-d时,cp复制软链接会将源复制过去,加上-d保留链接属性,把软链接复制过去
--preserv=
mode 权限不变
owner[ship] 权限及owner,group 不变
time 三个时间戳不变
xattr acl权限不变
links 链接不变
context 文件的selinux上下文关系不变
all
-p 等于 --preserv=mode,owner,time
-a 等于rdp
-v 显示复制过程
-f 强制覆盖: 当普通用户对目标文件不可更改时,但对该目标文件所在的目录有创建及删除文件的权限,则使用-f可以强制覆盖,实际上是先删除文件,再创建
-u 只覆盖源比目标文件新的文件
--backup-numbered 覆盖前,先将旧文件改名为 文件名.~数字~
实例:
cp -av /etc/ /app/back$(date +%F) #按当前时间备份/etc目录
cp /etc/passwd . --backup=numbered #复制passwd文件到当前文件,并且自备份
------------------------------------------------------------------------------------
mv [OPTION]... [-T] SOURCE DEST
• mv [OPTION]... SOURCE... DIRECTORY
• mv [OPTION]... -t DIRECTORY SOURCE...
常用选项:
-i: 交互式
-f: 强制 直接覆盖,不交互式,抵消i的作用
rename #批量修改文件名
实例:
[root@centos7 app]# ls
f10.log f1.log f2.log f3.log f4.log f5.log f6.log f7.log f8.log f9.log
[root@centos7 app]# rename '.log' '.log.bak' f* # #把f开头,log结尾的文件,修改为.log.bak
[root@centos7 app]# ls
f10.log.bak f1.log.bak f2.log.bak f3.log.bak f4.log.bak f5.log.bak f6.log.bak f7.log.bak f8.log.bak f9.log.bak
------------------------------------------------------------------
tree 显示目录树
-d: 只显示目录
-L level:指定显示的层级数目
-P pattern: 只显示由指定pattern匹配到的路径
实例:
[root@centos7 app]# tree -dL 1 /boot
/boot
├── efi
├── grub
└── grub2
3 directories
------------------------------------------------------
mkdir
-p 可以同时创建多级目录
-v 在创建目录时显示详细信息
-m xxx 创建同时指定权限
实例:
[root@centos7 app]# mkdir -pv d1/d2/d3
mkdir: created directory ‘d1’
mkdir: created directory ‘d1/d2’
mkdir: created directory ‘d1/d2/d3’
[root@centos7 app]# tree
.
└── d1
└── d2
└── d3
3 directories, 0 files
----------------------------------------------------------------
rmdir
-p 递归删除父空目录
实例:
[root@centos7 app]# tree
.
└── d1
├── 1.txt
└── d2
└── d3
3 directories, 1 file
[root@centos7 app]# rmdir -p d1/d2/d3
rmdir: failed to remove directory ‘d1’: Directory not empty
[root@centos7 app]# tree
.
└── d1
└── 1.txt
1 directory, 1 file
rm
-r #递归删除目录树
-f #强制删除
-i #交互式
--no-preserve-root
实例:
rm -rf / # 删除根
-----------------------------------------
inode【元数据】+block【数据】
ls -i #查看文件的inode号
df -i #查看inode号的使用情况。
echo f{1..52488} | xargs touch #创建文件52488个
watch -n 1 df -i /boot #每一秒显示df -i的状态
shred -zvun 10 file #删除并填充文件数据block区【数据不可恢复】
-------------------------------------------------------------------------------------------------------
硬链接
ln 源 目的 (注意:源与目的要在同一文件系统【同一分区】下)
实例:
[root@centos7 app]# ls -l
total 4
-rw-r--r--. 1 root root 2237 May 19 19:55 passwd
[root@centos7 app]# ln passwd mima #创建一个硬链接
[root@centos7 app]# ls -li
total 8
67 -rw-r--r--. 2 root root 2237 May 19 19:55 mima
67 -rw-r--r--. 2 root root 2237 May 19 19:55 passwd
-------------------------------------------
软链接
ln -s 源路径 目的路径 #注意:源文件的路径是"相对路径",一定是相对于软链接文件的路径。
实例:
[root@centos7 app]# tree
.
├── d1
│ └── d2
│ └── d3
└── passwd
3 directories, 1 file
[root@centos7 app]# cd d1/d2/d3/
[root@centos7 d3]# ln -s ../../../passwd password
[root@centos7 app]# tree
.
├── d1
│ └── d2
│ └── d3
│ └── password -> ../../../passwd
└── passwd
3 directories, 2 files
readlink 软链接 #查看软链接的原始路径
------------------------------------------------------------------
file [options]... #判断文件类型
常用选项:
-b 列出文件辨识结果时,不显示文件名称
-f filelist 列出文件filelist中文件名的文件类型
-F 使用指定分隔符号替换输出文件名后默认的”:”分隔符
-L 查看对应软链接对应文件的文件类型
实例:
file -L /etc/grub.conf
file -F"=====" -f list.txt