Blog Post
How to Check For Files and Directories in Bash
Scripting

How to Check For Files and Directories in Bash

Files and file systems are a key part of using Bash. In certain situations, we often need to check if the required files or directories exist in the system or not. There are a number of commands, including ‘test’, that provide the solutions to such queries in Bash.

If your project involves cross-platform software development, such as programming interfaces, using square brackets (‘[ ]’) will be useful. For instance, take a look at these syntaxes:

test filename

[ filename ] – this is the traditional square brackets command which works on Portable Operating System Interface (POSIX) shells.

[[ filename ]] – the double bracket command is for the newer systems that are integrated with shells such as Bash, Zsh, Ksh, etc. as the default.

Check for Files using Loops

The two most common commands for checking the availability of a file are ‘-e’ and ‘-f’. The ‘-e’ command searches for any and all types of files. If there are multiple types of files required, the ‘-e’ will be needed. Whereas for regular files, the ‘-f’ command will work.

It is possible to search for a file through the if statement. Just use the ‘test’ command followed by the name of the file. The syntax and examples for the ‘test’ commands:

Syntax:

if test -f "filename";

then

echo "filename found!"

fi

Example:

if test -f "/fwd/ubi.exe";

then

echo "/fwd/ubi.exe found!"

fi

Syntax:

if [ -f "filename" ];

then

echo "filename found!"

fi

Example:

if [ -f "/fwd/ubi.exe" ];

then

echo "/fwd/ubi.exe found!"

fi

Syntax:

if [[ -f "filename" ]];

then

echo "filename found!"

fi

Example:

if [[ -f "/fwd/ubi.exe" ]];

then

echo "/fwd/ubi.exe found!"

fi

We can also construct additional functions onto the ‘if’ block of statements, such as including an ‘else’ condition block. For example:

if [[ -f "/fwd/ubi.exe" ]];

then

echo "/fwd/ubi.exe exists."

else

echo "/fwd/ubi.exe does not exist."

fi

Check for Files Using Operators

If you need to immediately take action on the required file such as copying it, displaying a message, deleting it, etc. then you can simply initiate operators without the need for loops. By using test -f “/fwd/ubi.exe” && echo “/fwd/ubi.exe found!”, the system checks for the said file and if this statement returns true only then will the control be passed forward to the echo message saying the said file is found. The ‘&&’ (AND) operator only works when both parts of a statement are true. In this case, the file “/fwd/ubi.exe” does exist and hence the message “/fwd/ubi.exe found!” will display. Otherwise, it won’t. Similarly, we can also use:

[ -f "/fwd/ubi.exe ] && echo "/fwd/ubi.exe" found!"

[[ -f "/fwd/ubi.exe ]] && echo "/fwd/ubi.exe” found!"

Furthermore, we can link additional commands to the operator enabling further possible actions. Use curly brackets and separate each command with either ‘;’ or ‘&&’.

Example:

[ -f “/fwd/ubi.exe” ] && { echo “/fwd/ubi.exe found!” && cp “/fwd/ubi.exe” /bck/; } where ‘cp’ indicates copy and ‘/bck’ is the destination folder name.

In situations where you want to remove a file, run the ‘rm’ command directly. The ‘rm’ command, by default, will search for the mentioned file regardless. Therefore, in such cases:

rm filename or rm -i filename if you would prefer a warning prompt before removal.

Checking for Multiple Files

We can check for multiple files by combining the operator and loop command blocks. For example:

if [ -f "/fwd/ubi.exe" ] && [ -f "/ntg/lin.txt" ]

then

echo "Files exist."

fi

Or

if [ -f "/fwd/ubi.exe" -a -f "/ntg/lin.txt" ];

then

echo "Both files exist."

fi

Using “||” (OR) operator for output

if [ -f "/fwd/ubi.exe" ] && [ -f "/ntg/lin.txt" ]

then

echo "Both files exist." || echo "Both files do not exist."

fi

The ‘||’ (OR) operator works when the other part of the logic statement returns false. In this case, the statement “Both files do not exist.” will work only if the previous statement “Both files exist.” is false.

Another operator that can be used for searching files is the logical NOT operator, symbol ‘!’. Initiate the NOT operator to check if the file doesn’t exist. For example:

if [[ ! -f "/ntg/lin.txt" ]];

then

echo "/ntg/lin.txt does not exist."

fi

Or

[[ ! -f "/ntg/lin.txt" ]] && echo "/ntg/lin.txt does not exist."

Check for Directories

In order to work with multiple files at once, we can either use the operators as seen earlier or we can use commands on entire directories as well. First of all, to check if the directory exists or not we can use the ‘-d’ command.

Syntax:

directory= "/rpg/pg"

if [ -d "$directory" ]

then

echo "$directory found!"

fi

Example:

if [ -d "/rpg/pg" ]

then

echo "/rpg/pg found!"

fi

Or

[ -d "/rpg/pg" ] && echo "/rpg/pg directory exists."

Various File Test Operators

As said before, a system contains several types of files and hence it is essential to have operator keywords for each type. The list for these operators is as follows:

  1. -x – If the file exists and is executable
  2. -w – If the file exists and is writable
  3. -r – If the file exists and is readable
  4. -e – If the file exists and is of any type (directory, device, node, etc)
  5. -f – If the file exists and is a regular file
  6. -b – If the file exists and is of a special block type
  7. -c – If the file exists and if of a special char type
  8. -d – If the file that exists is a directory
  9. -G – If the file that exists has the same group as the user who is running the command
  10. -O – If the file is owned by the user who is running the command
  11. -g – If the file exists and has a group set id (sgid)
  12. -h – If the file is a symbolic link
  13. -k – If the file has a sticky bit flag set
  14.  -p – If the file that exists is a pipe
  15. -S – If the file that exists is a socket
  16. -s – If the file is of non-zero size

Conclusion

This guide walks the user through the file and file system commands in Bash by showing how to check for files and directories using loops, operators, and more.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2025 Blackdown.org. All rights reserved.