Liunx运维(二)-文件与目录操作

文档目录:

一、pwd:显示当前位置

二、cd:切换目录

三、tree:树形结构显示目录

四、mkdir 创建目录

五、touch:创建空文件或改变文件时间戳

 六、ls:显示目录下内容相关属性信息

七、cp:复制文件或目录

八、mv:移动或冲命令

九、rm:删除文件或目录

十、rmdir:删除空目录

十一、ln:建立硬、软链接

十二、readlink:查看符号链接文件内容

十三、find:查找目录下文件

十四、xargs:将标准输入转换成命令行参数

十五、rename:重命名

十六、basename:显示文件名或目录名

十七、dirname:显示文件或目录路径

十八、chattr:改为文件扩展属性

十九、lsattr:查看文件属性

二十、file:显示文件类型

二十一、md5sum:计算与校验文件MD5

二十二、chown:改为文件或目录的用户和用户组

二十三、chmod:改变文件或目录权限

二十四、chgrp:更改文件用户组

二十五、umask:显示或设置权限掩码

--------------------------------------- 分割线:正文 --------------------------------------------------------

一、pwd: 显示当前位置

1、pwd:print working directory

2、 #显示逻辑路径

pwd -L 同 echo $pwd 

 

3、显示链接路径

 

二、cd:切换目录

1、cd:change directory

2、cd -P:切换到链接的路径

3、cd -L:切换逻辑目录

4、cd - : 切换到用户上一家目录

5、cd ~:切换到用户 HOME 对于目录,同 CD

6、cd.. :切换到上一级目录,同 cd . 与 cd ../

 

三、tree:树形结构显示目录

1、安装:yum -y install tree

 2、tree 当前目录的结构

 3、tree -a:包含. 开头的隐藏文件

 4、tree -L 1:遍历 1 层级结构

 5、tree -d . 只显示目录

 6、tree -f(i):显示完整的路径名称(不显示树枝)

 7、tree - L 1 F : 区分目录与文件

 过滤目录

 过滤斜线结尾(等于过滤目录)

 

四、mkdir 创建目录

1、mkdir:make directory

2、mkdir dir1 dir2 #创建多个目录

 3、mkdir -p #递归创建目录, 存在目录时不报错

 4、mkdir -pv #显示创建过程

 5、创建多目录 1:mkdir -pv dir0/{dir1-1,dir1-2}/{dir2-1,dir2-2}

 

 创建多目录 2:mkdir -pv test/dir{1..5} ok/{a..e}

 

 6、mkdir -m 333 dir2 #创建目录并设置权限

 

7、简单介绍 {} 用法

echo {b,c}

echo a{b,c}

echo a{,c}

 

 echo {1..8} 1{a..h} 

8、克隆目录结构

mkdir -pv test/dir{1..5} ok/{a..e}

tree -fid --noreport test >> ~/.test.txt

cd  /tmp/

mkdir -p `cat _/.test.txt`(反引号)

 

五、touch:创建空文件或改变文件时间戳

1、touch a.txt b.txt #创建多个空文件

2、查看时间戳:stat a.txt

 3、分别查询对应的时间戳

ls -lu:access time

ls -lt:modify time

ls -lc:change time

 4、touch -m a.txt   # 更改最后修改时间

 

  5、touch -a a.txt   # 更改最后访问时间

  6、touch -d 20201001 a.txt   # 指定创建 / 修改的时间(修改时间)

 

7、修改 b.txt 时间属性与 a.txt 一致(修改时间)

touch -r a.txt b.txt

 8、设置文件为 201512312234.50 的时间格式

touch -t 201512312234.50 a.txt

 

 六、ls:显示目录下内容相关属性信息

1、ls=list directory contents 同 dos 下 dir 命令

2、ls -a  #含隐藏文件,其中. 为当前目录 .. 为上级目录

3、ls -l  #详细信息含最后修改时间

 4、ll --time-style=long-iso #显示完整时间格式

等同于 ll --full time

5、ll --time-style=long-iso --time=atime   # 显示文件的访问时间

 可使用 cat 进行验证

 6、ls -F   # 过滤文件及目录

ls -F|grep /   # 过滤目录

ls -F|grep -v /   # 过滤普通文件

  7、ls -l mytest20201204/  #显示目录内内容

ls -ld mytest20201204/   # 显示目录本身

  8、ls -R:递归查看目录

9、ls -lt   # 按照时间顺序排序(最后显示最前的)

ls -lrt  #按照时间顺序倒排(最后显示最后的)

 10、 ls -F   # 链接展示为 @

 

 ls -lF /etc/init.d/  # * 代表可执行的普通文件

 11、ls -lhi  #-h 参数为文件大小人类可读, -i 显示文件的 inode 值,链接相关的

 

 

七、cp:复制文件或目录

1、cp=copy centos 加了别名 cp -i 覆盖需要确认

2、cp -a 包含

cp -p:复制时候保持文件的所有者,权限及时间属性

cp -d:复制链接本身,且保留符号链接指向的文件或目录

cp -r:递归渎职目录

 3、cp 覆盖直接文件不提示方案

普通复制时候需要人工确认如下:

 方法 1:/usr/bin/cp file1.txt file2.txt   # 使用绝对路径命令 - 直接覆盖

 方法 2: \cp file1.txt file2.txt

 方法 3:unalias cp file1.txt file2.txt   # 临时取消别名

 方法 4:修改系统环境变量(不建议使用)

 4、快速备份命令

cp file1.txt{,_backup}

cp -a mytest7{,_backup}

 

 

八、mv:移动或冲命令

1、mv=move+rename,默认别名 mv -i,提示是否覆盖

2、屏蔽 mv 别名:\mv file1.txt file2.txt

 

 3、移动多文件 * 匹配:mv dir* testdir/

  4、mv -t testdir1/ dir*   # 反转移动

 

九、rm:删除文件或目录

1、rm=remove files or directories,默认带 rm -i

2、rm -rf testdir1/  #强行删除目录,不需要确认

 3、rm 删除时需要先备份,并且避免使用通配符

 

十、rmdir:删除空目录

1、rmdir=remove empty directories

2、rmdir -p -v dir1/a/b  #递归删除目录且显示删除过程,顺序为从子目录到父目录

 

十一、ln:建立硬、软链接

1、ln=link 分 hard link 与 symbolic link

2、系统限制,暂无法创建硬链接

 3、ln -s dir1/dir1.txt dir_softlink  #软链接不能事先存在

 4、文件链接测试

删除源文件,软连接显示为红色

 删除软链接,源文件不受影响

 

十二、readlink:查看符号链接文件内容

1、readlink dir1_softlink

 2、readlink -f dir1_softlink   # 显示最后一个非符号链接文件

 

十三、find:查找目录下文件

1、查找指定时间内修改过的文件

find . -atime -2  #查找 2 天内受到访问的文件

 find /tmp/ -mtime -5  #绝对路径下,5 天内修改的文件

 

find /tmp/ -mtime 2  #绝对路径下,2 天前修改的文件

 

2、用 -name 指定关键字查找

find . -mtime +2 -name '*.txt'  #查找 2 天前以 txt 结尾的文件

 3、利用!反向查找

find . -type d  #查找所有目录

find . ! -type d  #查找所有非目录

 4、按目录或文件的权限查找

find . -perm 755  #查找 755 权限的内容

 5、按照大小查看

find . -size +20c  #查找文件大小 >20 字节的文件

 6、查找文件时忽略目录

find /root/mytest20201204/mytest1/ -path '/root/mytest20201204/mytest1/dir1' -prune -o -print  #忽略单个目录

  find  /root/mytest20201204/mytest1/ \(-path /root/mytest20201204/mytest1/dir1 -o -path /root/mytest20201204/mytest1/dir2 \) -prune -o -print #忽略多个文件

 7、user 与 nouser 的查找

[root@localhost mytest1]# find . -user nobody  #用户为 nobody

[root@localhost mytest1]# find . -nouser #查找无任务用户文件

 

 8、group 与 nogroup 选项(同上)

 9、查找出比某个文件新或旧的文件

find . -newer dir3  #查找比 dir3 新的文件

find . -newer dir1 ! -newer dir2  #查找比 dir1 新 但比 dir2 旧的文件

 10、逻辑操作符

[root@localhost mytest1]# find . -maxdepth 1 -type d #遍历 1 层,类似 tree -L 1

 11、正则表达式

[root@localhost mytest1]# find . -regex "dir" #完全匹配路径为 dir,无结果

[root@localhost mytest1]# find . -regex ".*dir" #匹配后缀

[root@localhost mytest1]# find . -regex ".*/dir" #匹配 /dir 后缀

12、查找并打印

[root@localhost mytest8]# find . -type f -exec ls -l {} \;

13、查找 n 天前文件并删除

[root@localhost mytest8]# find . -type f -mtime +2 -exec rm {} \;

 14、-exec 选项安全模式 -ok

[root@localhost mytest8]# find . -type f -mtime +2 -ok rm {} \;

 15、find+xargs 过滤

[root@localhost mytest8]# find . -type f | xargs ls -l #传递查找并显示

[root@localhost mytest8]# find . -name '*.txt' | xargs -i mv {} testdir/ #查找传递并移动

 

 [root@localhost mytest8]# find . -name '*dir*' |xargs -p rm -f #需要确认 y、n 并删除

 

 16、案例,将目录下所有扩展名.txt 文件内 test001 替换为 test002

方法(一)

[root@localhost mytest8]# find . -name '*.txt' -exec sed -i 's#test001#test002#g' {} \;

 

方法(二)

[root@localhost mytest8]# find . -name '*.txt'|xargs sed -i 's#test001#test002#g'

 

方法(三):高效方法(反引号优先执行)

[root@localhost mytest8]# sed -i 's#test001#test002#g' `find . -name '*.txt'`

 17、删除所有文件但保留其中一个指定的文件

方法(一):

[root@localhost mytest8]# find . -type f ! -name 'dir5.txt' | xargs rm -f

方法(二):

[root@localhost mytest8]# find . -type f ! -name 'dir5.txt' -exec rm -f {} \;

 

十四、xargs:将标准输入转换成命令行参数

预置数据:

1、[root@localhost mytest8]# xargs < test001.txt #所有数字一行显示

2、[root@localhost mytest8]# xargs -n 3 < test001.txt #每行输出 3 个

  3、echo splitXsplitXsplitXsplitX

[root@localhost mytest8]# echo splitXsplitXsplitXsplitX|xargs -d X #以 X 作为分隔符

 

[root@localhost mytest8]# echo splitXsplitXsplitXsplitX|xargs -d X -n2 #每行显示 2 #以 X 作为分隔符,每行显示 2

 

 4、xargs -i:指定一个符号替代前面的结果

[root@localhost mytest8]# find . -name 'test*' | xargs -I [] cp [] dir/

 

十五、rename:重命名

预置:

 1、[root@localhost mytest8]# rename '_finished' '' * #将所有文件 _finished 替换为空

 2、[root@localhost mytest8]# rename .jpg .hello *.jpg #将所有.jpg 替换为.hello

 

十六、basename:显示文件名或目录名

1、[root@localhost mytest8]# basename dir1/dir2/dir3/test001.txt #去除路径部分

[root@localhost mytest8]# basename dir1/dir2/dir3/test001.txt .txt #去除路径部分,并去除后缀

 

 

十七、dirname:显示文件或目录路径

1、[root@localhost mytest8]# dirname dir1/dir2/dir3/test001.txt

  2、[root@localhost mytest8]# dirname test001.txt #根据路径返回相对路径

 

十八、chattr:改为文件扩展属性

1、[root@localhost mytest8]# chattr +a test001.txt #只能添加数据,不能删除

 2、[root@localhost mytest8]# chattr +i test001.txt  #添加只读属性

 

十九、lsattr:查看文件属性

1、[root@localhost mytest8]# lsattr test001.txt #查看文件

2、[root@localhost mytest8]# lsattr -d testdir/ #查看目录

 

二十、file:显示文件类型

 

二十一、md5sum:计算与校验文件 MD5 值

1、生成一个文件的 md5

[root@localhost mytest8]# md5sum test001.txt

 2、检测文件是否改变

[root@localhost mytest8]# md5sum -c md5.log

 

二十二、chown:改为文件或目录的用户和用户组

1、更改文件所属的用户组‘

[root@localhost mytest8]# chown baikang test001.txt

  2、更改文件所属的用户组的属性

[root@localhost mytest8]# chown .baikang test001.txt

[root@localhost mytest8]# chown :baikang test001.txt

 3、同时更改文件所属的用户和组的属性

[root@localhost mytest8]# chown baikang.root test001.txt

 4、递归更改目录下的所有目录文件的用户和用户组属性

[root@localhost ~]# chown -R baikang:baikang mytest20201204/

 

二十三、chmod:改变文件或目录权限

1、设置权限为空

[root@localhost mytest1]# chmod a= test.txt test.txt

 2、设置 usr 文件属主执行权限

[root@localhost mytest1]# chmod u+x test.txt

 3、设置 group 文件用户组可写权限

[root@localhost mytest1]# chmod g+u test.txt

  4、设置 other 其他用户可读权限

[root@localhost mytest1]# chmod o+r test.txt

 5、设置多权限

[root@localhost mytest1]# chmod a= test.txt
[root@localhost mytest1]# chmod ug+r,o+r test.txt

 6、常用权限

目录:755

文件:644

全量:777

 

二十四、chgrp:更改文件用户组

1、chgrp testuser test.txt   # 更改文件

2、chgrp -R root dir/  #递归更改目录下文件

 

二十五、umask:显示或设置权限掩码

1、文件权限 =666- 掩码

2、目录权限 =777- 掩码

3、root 用户默认掩码:0022

 4、普通用户默认掩码:0002

 5、umask 044  #临时生效

 

一、pwd:显示当前位置