站点图标 AI技术聚合

shell脚本怎样判断文件是否存在

shell脚本判断文件是否存在

在进行linux系统相关应用程序开发时,少不了要书写一些shell脚本,有时候要用到判断文件或者目录是否存在的脚本,本文笔者做一下笔记,已备后查。

shell判断文件是否存在的脚本如下:

//[ 与 ] 的前后必须有空格符
if [ -f /path/file.ext ] 
then 
     echo "The file exist"
else
     echo "The file doesn't exist"
fi
//判断某链接是否存在
if [ -L /path/link ] 
then 
    echo "The link exist"
else
    echo "The link doesn't exist"
fi

其实shell对于文件册测试有好几种选项开关

现在例举如下:

表达式 测试含义
-a filepath file exists. all files type
-b filepath file exists and is a block special file.
-c filepath file exists and is a character special file.
-d filepath file exists and is a directory.
-e filepath file exists (等同于 -a).
-f filepath file exists and is a regular file.
-g filepath file exists and has its setgid(2) bit set.
-G filepath file exists and has the same group ID as this process.
-k filepath file exists and has its sticky bit set.
-L filepath file exists and is a symbolic link.
-n filepath string length is not zero.
-o filepath Named option is set on.
-O filepath file exists and is owned by the user ID of this process.
-p filepath file exists and is a first in, first out (FIFO) special file ornamed pipe.
-r filepath file exists and is readable by the current process.
-s filepath file exists and has a size greater than zero.
-S filepath file exists and is a socket.
-t filepath file descriptor number fildes is open and associated with aterminal device.
-u filepath file exists and has its setuid(2) bit set.
-w filepath file exists and is writable by the current process.
-x filepath file exists and is executable by the current process.

shell脚本之文件是否存在、权限校验

判断目录是否存在

#判断目录是否存在,判断非加!号, [ ! -d '/home' ]
if [ -d '/home' ]
then
    echo "目录/home存在=========="
else
    echo "目录/home不存在========="
fi

判断文件是否存在

#判断文件是否存在
if [ -f '/home/docker.log' ]
then
    echo "文件/home/docker.log存在============="
else
    echo "文件/home/docker.log不存在==========="
fi

判断目录/文件是否存在

#判断文件是否存在,目录或文件存在都成立
if [ -e '/home' ]
then
    echo "/home存在=============="
else
    echo "/home不存在============"
fi

判断文件权限

#检测文件是否可读 -r ,可写 -w ,可执行 -x
if [ -r '/home/script/file.log' ]
then
    echo "文件/home/script/file.log存在并可读=============="
else
    echo "目录/home/script/file.log不存在或不可读=================="
fi

判断文件是否属于当前用户

#检测文件是否属于当前用户
file_path=/home/script/file.log
if [ -O $file_path ]
then
    echo "文件$file_path属于当前用户================="
else
    echo "文件$file_path不属于当前用户==============="
fi

判断文件是否与当前用户相同用户组

#检测文件是否存在,并且默认组与当前用户相同
file_path=/home/script/file.log
if [ -G $file_path ]
then
    echo "文件$file_path所属组与当前用户相同================="
else
    echo "文件$file_path所属组与当前用户不相同================"
fi

比较文件之间是否为新建

#检测文件file1是否比file2新
file1=/home/script/file.log
file2=/home/script/file_1.log
if [ $file1 -nt $file2 ]
then
    echo "文件$file1比文件$file2新=============="
fi
if [ $file1 -ot $file2 ]
then
    echo "文件$file1比文件$file2旧==============="
fi

复合条件判断文件

#判断既是文件 又 可读 ,用 && ,或用 ||
file=/home/script/file.log
if [ -f $file ] && [ -r $file ]
then
        echo "文件$file是文件,并且可读============="
fi

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持aitechtogether.com。

退出移动版