两个方式:find命令或者shell脚本。
1、find命令(1)find是linux下用于查找文件的通用方法。(2)find语法:find [指定查找目录] [查找规则] [查找完后执行的action](3)例如:find /tmp -namewa*-typel,是在/tmp下查找名为wa开头且类型为符号链接的文件。找到就表示存在。
2、shell脚本(1)在进行文件的自动处理中常常需要自动判别,下面的脚本判断test.log是否存在,存在则显示文件存在,否则显示文件不存在。(2)例子:编辑一个脚本判断文件是否存在。vi t.sh#!/bin/bashif [-e/temp/test.log];then //这里是判断语句,-e表示进行比较结果为真则存在echo"文件存在"elseecho"文件不存在"fi保存退出执行:sht.sh
linux判断文件是否存在脚本
#!/bin/bash
if [ -f $1 ]
then
echo "$1 exists!"
else
echo "$1 not exists!"
fi
***********************
运行
bash test.sh /home/user.txt
记得这个要加绝对路径~~~,在运行的时候,脚本后面接一个文件参数~~~
linux判断文件是否存在脚本文件夹
使用-e判断,例如if [ -e $FILE ] then echo $FILE 作为文件或文件夹是存在的fi
linux判断文件是否存在脚本错误
可以用“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),则为TrueFile1 -nt File2如果文件File1新于(Newer Then) File2,则为TrueFile2 -ot File2如果文件File1旧于(Older Then) File2,则为True
linux脚本判断文件夹是否存在
效果:
ping.sh代码:
#ssh连接服务器
function getConnection()
{
ssh -o NumberOfPasswordPrompts=0 $1@$2 "pwd" &>/dev/null
if [ $? != 0 ];then
echo -e "n无法连接到$2"
continue
else
echo -e "n成功连接$2"
fi
}
#这里可以通过读取文件配置自动循环,需要的话再修改
getConnection root 192.168.1.1
也可以把IP放host里通自动循环~
shell脚本中判断文件是否存在
Shell 判断FTP上某个路径、文件是否存在:#bin/bash#进入该目录,把文件down在这里cd /opt/#某个路径的某个文件变量定义f1=/file1/file2/filename;ftp -i -v -n 192.x.x.x <<okuse ftpuser ftppasswdbinaryget ${f1}byeokif [ -f /opt/filename ] ;thenecho "the ftp filename exist.";fiexit 0#首先要知道ftp上的路径和文件的名字,如果不知道的话,用通配符也可以,但是路径得知道,总不能全部下载下来吧。。。还有,ftp能登录、用户名、密码应该不会有问题吧,就没判断了。
shell脚本判断文件是否存在,不存在则报错
他可以通过他的判断机制去检测文件是否存