linux判断目录是否存在命令,linux shell 中判断文件、目录是否存在的方法

linux shell 中判断文件、目录是否存在的方法

-e filename 如果 filename存在,则为真
-d filename 如果 filename为目录,则为真
-f filename 如果 filename为常规文件,则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真
-w filename 如果 filename可写,则为真
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真

常用例子

  1. 如果存在某文件,则删除
if [[ -f trials ]]; then 
rm ${result_path}trials; 
fi
  1. 如果没有文件夹,则创建
if [[ ! -d $result_name ]];then
    mkdir -p $result_name
fi

除了以上常用的判断,还有其他的可以使用,如下

-a file exists(文件是否存在).
-b file exists and is a block special file(文件存在,并且是块特殊文件).
-c file exists and is a character special file(文件存在,并且是一个字符特殊文件).
-d file exists and is a directory(文件存在,并且是一个目录).
-e file exists (just the same as -a)(文件存在(与-a相同)).
-f file exists and is a regular file(文件存在,并且是一个常规文件).
-g file exists and has its setgid(2) bit set(文件存在并且设置了它的setgid(2)位。).
-G file exists and has the same group ID as this process(文件存在且与该进程具有相同的组ID).
-k file exists and has its sticky bit set(文件存在并且设置了sticky位).
-L file exists and is a symbolic link(文件存在,并且是一个符号链接).
-n string length is not zero(字符串长度不为0).
-o Named option is set on(Named选项设置为on).
-O file exists and is owned by the user ID of this process(文件存在,并由该进程的用户ID拥有).
-p file exists and is a first in, first out (FIFO) special file or
named pipe(文件存在,并且是先进先出(FIFO)特殊文件或
命名管道).
-r file exists and is readable by the current process(文件存在且当前进程可读).
-s file exists and has a size greater than zero(文件存在且大小大于零).
-S file exists and is a socket(文件存在并且是套接字).
-t file descriptor number fildes is open and associated with a
terminal device(文件描述符编号文件打开并与a关联
终端设备).
-u file exists and has its setuid(2) bit set(文件存在并且设置了它的setuid(2)位).
-w file exists and is writable by the current process(文件存在且当前进程可写).
-x file exists and is executable by the current process(文件存在,并可由当前进程执行).
-z string length is zero(字符串长度为零).
# linux   shell  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×