linux中怎么查看文件是否存在

linux中怎么查看文件是否存在,第1张

两个方式:find命令或者shell脚本。

1、find命令

find是linux下用于查找文件的通用方法。

find语法: find [指定查找目录] [查找规则] [查找完后执行的action]

例如:find /tmp -name wa* -type l ,是在/tmp下坦滑孙查找名为wa开头且类型为符号链接的文件。找到就表示存在

2、shell脚本

在进行文件的自动处理中常常需要自动判别,下面的脚本判断test.log是否存在,存在则显示文件存在,否则显示文件不存在。

例子:编辑一个让晌脚本判让链断文件是否存在

vi t.sh

#!/bin/bash

if [ -e /temp/test.log ];then //这里是判断语句,-e表示进行比较结果为真则存在

echo "文件存在"

else

echo "文件不存在"

fi

保存退出

执行:

sh t.sh

可以用“test 条件表达式”进行测试,如:test -f /etc/fstab 测试文件/etc/fstab文件是否存在

-e File如果文件File存在(Exist),则为True

-s File如果文件File存在且文件大小(Size)大于正亩零迹清烂,则为True

-f File如果文件File存在且是普通文件(File),则为True

-d File如果文件File存在且是目录(Directory),则为True

-b File如果文件File存在且是块(Block)特殊文件,则为True

-c File如果文件File存在且是字符(Character)特殊文件,则为True

-L File如果文件File存在且是符号链接(Link)文件,则为True

-r File如果文件File存在且是可读的(Readable),则为True

-w File如果文件File存在且是可写的(Writable),则为True

-x File如果文件File存在且是可执行的(Executable),则为True

-O File如果文件File存在且属于当前用户(Owner),则为True

-G File如果文件File存姿漏在且属于当前用户组(Group),则为True

File1 -nt File2如果文件File1新于(Newer Then) File2,则为True

File2 -ot File2 如果文件File1旧于(Older Then) File2,则为True


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/8147341.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-13
下一篇2023-04-13

发表评论

登录后才能评论

评论列表(0条)

    保存