Use the shell to determine if a folder or file exists

Source: Internet
Author: User

Use the shell to determine if a folder or file exists
    • Determine if a folder exists

      if [! -d "/etc/open" ];thenecho "文件夹/etc/open不存在"esleecho "文件夹/etc/open存在"fi
    • Determine if the file exists and delete it if it exists

      if [ ! -f "/etc/filename" ];thenecho "文件不存在"elserm -rf /etc/lenameecho "filename文件已删除"fi
    • Determine if a folder exists

      if [ -d "/data/" ];thenecho "文件夹存在"elseecho "文件夹不存在"fi
    • Determine if a file exists

      if [ -f "/data/filename" ];thenecho "文件存在"elseecho "文件不存在"fi
    • Comparison characters
      -e 判断对象是否存在-d 判断对象是否存在,并且为目录-f 判断对象是否存在,并且为常规文件-L 判断对象是否存在,并且为符号链接-h 判断对象是否存在,并且为软链接-s 判断对象是否存在,并且长度不为0-r 判断对象是否存在,并且可读-w 判断对象是否存在,并且可写-x 判断对象是否存在,并且可执行-O 判断对象是否存在,并且属于当前用户-G 判断对象是否存在,并且属于当前用户组-nt 判断file1是否比file2新  [ "/data/file1" -nt "/data/file2" ]-ot 判断file1是否比file2旧  [ "/data/file1" -ot "/data/file2" ]

Use the shell to determine if a folder or file exists

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.