Use BashShell to check whether a file exists

Source: Internet
Author: User
Tags bit set
In most cases, you can use test commands to test the conditions. For example, you can compare strings, determine whether a file exists, and whether the file is readable. The following article describes how to use BashShell to check whether a file exists. For more information, see the following. Preface

You may often encounter this kind of requirement at work. in the Bash environment of Unix-like systems, how can you check whether a file exists? The test command in Shell can be used to check whether the file type or comparison value are equal. this command can also be used to check whether the file exists.

You can use the following command to check:

test -e filename[ -e filename ]  test -f filename[ -f filename ]

The following Command uses the Shell conditional expression to determine whether the/etc/hosts file exists:

[ -f /etc/hosts ] && echo "Found" || echo "Not found"

The command output the following content:

Found

A more common usage is to place the test command in the conditional expression for if... else... fi condition judgment, and then write different branch logics in it.

#!/bin/bashfile="/etc/hosts"if [ -f "$file" ]then echo "$file found."else echo "$file not found."fi

Operator used to check file attributes

If the object exists and has corresponding attributes, true is returned for the following operators:

-b FILE  FILE exists and is block special-c FILE  FILE exists and is character special-d FILE  FILE exists and is a directory-e FILE  FILE exists-f FILE  FILE exists and is a regular file-g FILE  FILE exists and is set-group-ID-G FILE  FILE exists and is owned by the effective group ID-h FILE  FILE exists and is a symbolic link (same as -L)-k FILE  FILE exists and has its sticky bit set-L FILE  FILE exists and is a symbolic link (same as -h)-O FILE  FILE exists and is owned by the effective user ID-p FILE  FILE exists and is a named pipe-r FILE  FILE exists and read permission is granted-s FILE  FILE exists and has a size greater than zero-S FILE  FILE exists and is a socket-t FD file descriptor FD is opened on a terminal-u FILE  FILE exists and its set-user-ID bit is set-w FILE  FILE exists and write permission is granted-x FILE  FILE exists and execute (or search) permission is granted

The preceding command is copied from man test.

The methods using the above symbols are exactly the same:

if [ operator FileName ]then  echo "FileName - Found, take some action here"else echo "FileName - Not found, take some action here"fi

Summary

The above is all about this article. I hope this article will help you in your study or work. if you have any questions, please leave a message.

For more information about how to use Bash Shell to check whether a file exists, see PHP!

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.