• 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 ]
then
echo $file "---> It is a ORDINARY FILE."
elif [ -d $file ]
then
echo $file "---> It is a DIRCTORY."
else
echo $file "---> It is something else."
fi

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."


21 views

Recent Posts

See All

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

Passwordless SSH in Linux

Every time you ssh from one Linux server to another, you need to enter target server user's password. We can also setup passwordless ssh in Linux between two Linux servers so that you don't have to en

  • Facebook
  • Twitter
  • YouTube
  • Instagram

© 2020 by DBA Genesis