How To

How To Check If A File Or Directory Exists In Bash

Introduction

Checking if a file or directory exists is one of the most common tasks in Bash scripting. Many scripts need to confirm that a file is available before reading it, editing it, deleting it, or writing output to it. Similarly, scripts often need to check if a directory exists before saving backups, logs, scan results, or reports.

If you are learning Bash scripting, this is an important topic because file and directory checks are used in automation, Linux administration, backup scripts, cybersecurity monitoring, and log analysis.

Check If A File Exists In Bash

To check if a regular file exists, use the -f operator.

Basic syntax:

if [[ -f "filename" ]]; then    echo "File exists"else    echo "File does not exist"fi

Example:

#!/bin/bashfile="/etc/passwd"if [[ -f "$file" ]]; then    echo "File exists: $file"else    echo "File not found: $file"fi

The -f operator checks whether the given path exists and is a regular file.

Check If A Directory Exists In Bash

To check if a directory exists, use the -d operator.

#!/bin/bashdir="/home/kali"if [[ -d "$dir" ]]; then    echo "Directory exists: $dir"else    echo "Directory not found: $dir"fi

This is useful before saving files inside a directory.

Create Directory If It Does Not Exist

You can check for a directory and create it if missing.

#!/bin/bashbackup_dir="$HOME/backups"if [[ -d "$backup_dir" ]]; then    echo "Backup directory already exists"else    echo "Creating backup directory..."    mkdir -p "$backup_dir"fi

The mkdir -p command creates the directory and avoids errors if the parent path is needed.

Check If File Or Directory Exists

To check if a path exists as either a file or directory, use -e.

#!/bin/bashpath="/tmp/test"if [[ -e "$path" ]]; then    echo "Path exists"else    echo "Path does not exist"fi

Use -e when you do not care whether the path is a file or directory.

Check File Permissions

Bash can also check file permissions.

[[ -r "$file" ]]   # file is readable[[ -w "$file" ]]   # file is writable[[ -x "$file" ]]   # file is executable

Example:

#!/bin/bashscript="test.sh"if [[ -x "$script" ]]; then    echo "Script is executable"else    echo "Script is not executable"fi

Cybersecurity Example: Check Auth Log File

#!/bin/bashlog_file="/var/log/auth.log"if [[ -f "$log_file" ]]; then    echo "Checking failed SSH login attempts..."    grep "Failed password" "$log_file" | tail -10else    echo "Auth log file not found"fi

This script checks whether the authentication log exists before searching for failed SSH login attempts.

Conclusion

Checking if a file or directory exists in Bash is a basic but important scripting skill. You can use -f for files, -d for directories, and -e for any existing path.

For beginners, this concept is useful for writing safer Bash scripts. It helps prevent errors before reading files, creating backups, saving reports, checking logs, or running cybersecurity automation tasks

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

2 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

2 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

2 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

2 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

2 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

3 days ago