Shell determines whether a folder or file exists

Source: Internet
Author: User
Tags syslog

The folder does not exist and is created

if [!-d "/data/"];thenmkdir/dataelseecho "folder already exists" fi

File exists then delete

if [!-F "/data/filename"];thenecho "file does not exist" Elserm-rf/data/filenamefi

Determine if a folder exists

If [-d '/data/"];thenecho" folder exists "Elseecho" folder does not exist "fi

Determine if a file exists

If [-d '/data/test "];thenecho" file exists "Elseecho" file does not exist "fi

File comparison characters

-e filename  if filename exists, true  [-e/var/log/syslog]-d filename  if filename is directory, true  [-d/tmp/mydir]-f fi Lename  True if filename is a regular file  [-f/usr/bin/grep]-l filename  If filename is a symbolic link, true  [-l/usr/bin/grep]-r FileName  If filename is readable, true  [-r/var/log/syslog]-w filename  If filename is writable, true  [-w/var/mytmp.txt]- x filename  If filename is executable, true  [-l/usr/bin/grep]filename1-nt filename2  If filename1 is newer than filename2, true c15/>[/tmp/install/etc/services-nt/etc/services]filename1-ot filename2  If filename1 is older than filename2, then true  [/ Boot/bzimage-ot Arch/i386/boot/bzimage]

  

  

Shell determines whether 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.