Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Bash – Test if file or directory exists

    Bash – Test if file or directory exists

    RahulBy RahulApril 16, 20142 Mins ReadUpdated:December 27, 2019

    While working with bash programming, we many times need to check if a file already exists, create new files, insert data in files. Also sometimes we required executing other scripts from other scripts.

    This article has few details about the test if file or directory exists in the system. Which can be very helpful for you while writing shell scripting.

    #1. Test if File Exists

    If we required adding some content or need to create files from the script. First, make sure that the file already exists or not. For example one of my script creating logs in file /tmp/testfile.log and we need to make sure this file exists or not

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    if [ -f /tmp/testfile.log ]
    then
        echo "File exists"
    fi

    The above statements can be also written using the test keyword as below

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    if test -f /tmp/testfile.log
    then
        echo "File exists"
    fi

    Or in a single line we can write it as below. This is very useful while writing in shell scripting.

    1
    [ -f /tmp/testfile.log ] && echo "File exists"

    to add else part in above command

    1
    [ -f /tmp/testfile.log ] && echo "File exists" || echo "File not exists"

    #2. Test if Directory Exists

    Sometimes we need to create files inside a specific directory or need directory any other reason, we should make sure that directory exists. For example we are checking /tmp/mydir exists for not.

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    if [ -d /tmp/mydir ]
    then
        echo "Directory exists"
    fi

    The above statements can be also written using the test keyword as below

    1
    2
    3
    4
    5
    6
    #!/bin/bash
     
    if test -d /tmp/mydir
    then
        echo "Directory exists"
    fi

    Or in a single line we can write it as below

    1
    [ -d /tmp/mydir ] && echo "Directory exists"

    #3. Create File/Directory if not Exists

    This is the best practice to check file existence before creating them else you will get an error message. This is very helpful while creating shell scripts required to file or directory creation during runtime.

    For File:

    1
    [ ! -f /tmp/testfile.log ] && touch /tmp/testfile.log

    For Directory:

    1
    [ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir

    bash directory else file if
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Replace Faulty Device from RAID Array
    Next Article Install Lighttpd Web Server on CentOS/RHEL 6/5 using Yum

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Creating Menu in Shell Script (Linux Select Command)

    Updated:May 31, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    3 Comments

    1. shifu on August 9, 2019 1:31 pm

      thank you Rahul

      Reply
    2. Sunil on December 17, 2018 12:54 am

      Thank you, Rahul!
      Valuable tips for everyone, working with directories.

      Reply
    3. Abhay on September 27, 2018 12:06 am

      Hi

      In my case I have multiple directories and have to check each directories if its not exist then create the directories.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.