• Arun Kumar

Shell Script to Check File or Directory

Linux shell script allows users to check and determine if the user input is a file or a directory. To achieve this we are using operators -f and -d.

vi check_file_directory.sh
# !/bin/bash
echo "Enter the file name: "
read file
if [ -f $file ]
echo $file "---> It is a ORDINARY FILE."
elif [ -d $file ]
echo $file "---> It is a DIRCTORY."
echo $file "---> It is something else."

Here is the sample output of the above script

More ways to use -f and -d Operators

  • You can use it to check If a Directory Exists In a Shell Script [ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists."

  • You can also check if File Exists test -f /etc/resolv.conf && echo "$FILE exists."


Recent Posts

See All

Oracle 19c Installation on Linux

Oracle 19c adds more features to Oracle database and this article demonstrates Oracle 19c installation on Linux 7. We will be using Oracle Linux 7 and Oracle Database 19.3 softwares for this activity.

Basic Linux Commands

This article describes basic Linux commands that you must know as a system or database administrator. Below commands in Linux help you manage any Linux server fast and smoothly. Linux Command List Che

Linux Firewall with iptables and firewalld

In this article will be covering details regarding iptables and firewalld which helps in Linux firewall management. We will also be looking at how to enable specific ports (1521 for Oracle) inside ipt