Liunx运维(二)-文件与目录操作
文档目录:
---------------------------------------分割线:正文--------------------------------------------------------
1、pwd:print working directory
2、 #显示逻辑路径
pwd -L 同echo $pwd
3、显示链接路径
1、cd:change directory
2、cd -P:切换到链接的路径
3、cd -L:切换逻辑目录
4、cd - :切换到用户上一家目录
5、cd ~:切换到用户HOME对于目录,同CD
6、cd.. :切换到上一级目录,同cd . 与 cd ../
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 :区分目录与文件
过滤目录
过滤斜线结尾(等于过滤目录)
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`(反引号)
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
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值,链接相关的
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}
1、mv=move+rename,默认别名mv -i,提示是否覆盖
2、屏蔽mv别名:\mv file1.txt file2.txt
3、移动多文件*匹配:mv dir* testdir/
4、mv -t testdir1/ dir* #反转移动
1、rm=remove files or directories,默认带rm -i
2、rm -rf testdir1/ #强行删除目录,不需要确认
3、rm删除时需要先备份,并且避免使用通配符
1、rmdir=remove empty directories
2、rmdir -p -v dir1/a/b #递归删除目录且显示删除过程,顺序为从子目录到父目录
1、ln=link分hard link与symbolic link
2、系统限制,暂无法创建硬链接
3、ln -s dir1/dir1.txt dir_softlink #软链接不能事先存在
4、文件链接测试
删除源文件,软连接显示为红色
删除软链接,源文件不受影响
1、readlink dir1_softlink
2、readlink -f dir1_softlink #显示最后一个非符号链接文件
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 {} \;
预置数据:
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/
预置:
1、[root@localhost mytest8]# rename '_finished' '' * #将所有文件_finished替换为空
2、[root@localhost mytest8]# rename .jpg .hello *.jpg #将所有.jpg替换为.hello
1、[root@localhost mytest8]# basename dir1/dir2/dir3/test001.txt #去除路径部分
[root@localhost mytest8]# basename dir1/dir2/dir3/test001.txt .txt #去除路径部分,并去除后缀
1、[root@localhost mytest8]# dirname dir1/dir2/dir3/test001.txt
2、[root@localhost mytest8]# dirname test001.txt #根据路径返回相对路径
1、[root@localhost mytest8]# chattr +a test001.txt #只能添加数据,不能删除
2、[root@localhost mytest8]# chattr +i test001.txt #添加只读属性
1、[root@localhost mytest8]# lsattr test001.txt #查看文件
2、[root@localhost mytest8]# lsattr -d testdir/ #查看目录
1、生成一个文件的md5
[root@localhost mytest8]# md5sum test001.txt
2、检测文件是否改变
[root@localhost mytest8]# md5sum -c md5.log
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/
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
1、chgrp testuser test.txt #更改文件
2、chgrp -R root dir/ #递归更改目录下文件
1、文件权限=666-掩码
2、目录权限=777-掩码
3、root用户默认掩码:0022
4、普通用户默认掩码:0002
5、umask 044 #临时生效
一、pwd:显示当前位置
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了