InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Bash shell scripting

  • Bash - add a number to a variable
  • Bash - append text to a variable
  • Bash - how to check if a variable is set
  • Bash - how to compare file timestamps
  • Bash - how to find last command exit status code
  • Bash - how to get main program and current file dir location
  • Bash - how to redirect stderr to stdout or file
  • Bash - how to run custom commands at script exit
  • Bash - how to stop at error
  • Bash - how to use functions - quick tutorial
  • Bash - iterate over array
  • Bash - local and global variables
  • Bash - newline and other escape character in string
  • Bash - pass all arguments from one script to another
  • Bash - set default value if a variable is empty
  • Bash - variables in double quotes vs without quotes
  • Bash associative array tutorial
  • Bash check if file begins with a string
  • Bash shell - check if file or directory exists
  • Can global variables be modified in bash function?
  • Find memcache request hit rate on linux command line
  • How to return a value from bash function
  • Iterate over specific file extension in a dir in shell script
  • Linux - Yesterday's Date in YYYYMMDD format
  • bash - extract urls from xml sitemap
  • bash - how to use regex in if condition
 
  • Home
  • > Tutorials
  • > Bash shell scripting

Bash shell – check if file or directory exists

By admin on Dec 4, 2015

Bash code snippet to check if file exists

FILE=/etc/passwd
if [ -f $FILE ] ; then
  echo "$FILE exists and is regular file"
else
  echo "$FILE either does not exist or is not regular file"
fi

Outcome:

/etc/passwd exists and is regular file

Bash code snippet to check if directory exists

DIR=/etc
if [ -d $DIR ] ; then
  echo "$DIR exists and is a dir"
else
  echo "$DIR does not exist or is not a dir"
fi

Outcome:

/etc exists and is a dir

Bash code snippet to check if file or directory exists

FILE_OR_DIR=/etc
if [ -e $FILE_OR_DIR ] ; then
  echo "$FILE_OR_DIR exists"
else
  echo "$FILE_OR_DIR does not exist"
fi

Outcome:

/etc exists

Suggested posts:

  1. Bash check if file begins with a string
  2. Iterate over specific file extension in a dir in shell script
  3. Bash – how to compare file timestamps
  4. Bash – how to check if a variable is set
  5. jQuery – check if an element exists
  6. How to return a value from bash function
  7. Csslint – quick command-line tutorial and how to use it for pre-deployment sanity check
  8. Bash – variables in double quotes vs without quotes
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Bash shell scripting, Linux, Tutorials, Ubuntu Linux

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress