您的位置:首页 > 其它

find或者test查看某一文件夹下某一文件是否存在

2017-03-06 11:03 316 查看
1. find:

find dir -type f -name "*filename*"

eg:

[root@VTB93-PC2 ~]# find /var/www/html -name 'index2.html'

/var/www/html/index2.html

[root@VTB93-PC2 ~]# find /var/www/html -name 'index2.*'

/var/www/html/index2.html

[root@VTB93-PC2 ~]#

2. test:

test -f /dir/filename

可是这样不输出任何内容:

eg:

[root@VTB93-PC2 ~]# test /var/www/html/index2.html

[root@VTB93-PC2 ~]# test /var/www/html/index3.html

[root@VTB93-PC2 ~]#

[root@VTB93-PC2 ~]# test -f /var/www/html/index3.html

所以在后面增加了判断输出内容:

[root@VTB93-PC2 ~]# test -f /var/www/html/index3.html && echo "exists" || echo "not found"

not found

[root@VTB93-PC2 ~]# test -f /var/www/html/index2.html && echo "exists" || echo "not found"

exists

3. 也可以用perl调用系统命令,然后对输出结果进行匹配判断:

eg:

$out = `ll /var/www/html`;

if($out =~ m/index2.html/s){

    print "exists";

}else{

   print "not found";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: