Shell Script to Check if Every Passed Argument is a File or Directory

04 Oct, 2021
While automating things in Linux we always work on the file and directories. And sometimes we pass the files and directories as the arguments to the shell scripts. And we have to determine whether the provided argument is a file or a directory, and today we are going to see how to check whether provide argument is the file or directory. Before moving on further we need to know something about the file comparison in the bash shell which we can use with the if statement. Here is the same option that can be used with the if statement in bash shell-like
-d  file -  This option determines whether the provided 
file exists on the system and is it the directory.
-f  file -  This option determine the whether provided 
file is exist on the system and is it file.
Algorithm:
First check the provides argument is the directory or not using the if statement using the -d option for the first argument using the $1 parameter. If it is true then print the message that the provided argument is the directory.If the argument is not the directory then check it for the file. Use the -f option and if statement for the first argument using the $1 parameter. If it is true then the print the message that provided the argument is the file.If both conditions are false then it is clear that the provided argument is neither file and nor directory. Then print the message the given argument is neither file and nor directory.
First check the provides argument is the directory or not using the if statement using the -d option for the first argument using the $1 parameter. If it is true then print the message that the provided argument is the directory.
If the argument is not the directory then check it for the file. Use the -f option and if statement for the first argument using the $1 parameter. If it is true then the print the message that provided the argument is the file.
If both conditions are false then it is clear that the provided argument is neither file and nor directory. Then print the message the given argument is neither file and nor directory.
Script:
#!/bin/sh
#Using -d option we are checking whether the first argument is a directory or not.
#$1 refers to the first argument
if [ -d $1 ]
then
        echo "The provided argument is the directory."
#Using -f option we are checking whether the first argument is a file or not.
elif [ -f $1 ]
then
        echo "The provided argument is the file."
#if the provided argument is not file and directory then it does not exist on the system.   
else
        echo "The given argument does not exist on the file system."
fi
To Execute the Script use the following commands:
./script_name.sh filename  # For files
./script_name.sh foldername # For folders
Now let’s do the same thing for all provided arguments. We traverse through all arguments using the for loop with $@ variable.$@ refers to all of a shell script’s command-line arguments. Use the above algorithm with the for loop in the shell as follows:
Algorithm:
Use for loop in Bash and traverse through all passed arguments using $@ argument.Inside for loop check, the provides argument is the directory or not using the if statement using the -d option for the first argument using $1 parameter. If it is true then print the message that the provided argument is the directory.If the argument is not the directory then check it for the file. Use the -f option and if statement for the first argument using the $1 parameter. If it is true then print a message that provided the argument in the file.If both conditions are false then it is clear that the provided argument is neither file and nor directory. Then print the message the given argument is neither file and nor directory.
Use for loop in Bash and traverse through all passed arguments using $@ argument.
Inside for loop check, the provides argument is the directory or not using the if statement using the -d option for the first argument using $1 parameter. If it is true then print the message that the provided argument is the directory.
If the argument is not the directory then check it for the file. Use the -f option and if statement for the first argument using the $1 parameter. If it is true then print a message that provided the argument in the file.
If both conditions are false then it is clear that the provided argument is neither file and nor directory. Then print the message the given argument is neither file and nor directory.
Script:
#!/bin/sh

#traversing through all arguments using  for loop
for i in "$@"
do
        #Using -d option we are checking whether the first argument is a directory or not.
        if [ -d $i ]
        then
                echo "The provided argument $i is the directory."
                #Using -f option we are checking whether the first argument is a file or not.
        elif [ -f $i ]
        then
                echo "The provided argument $i is the file."
        #if the provided argument is not file and directory then it does not exist on the system. 
        else
                echo "The given argument does not exist on the file system."
        fi
done
To Execute the Script use the following commands:
./script.sh filename folder #for both files and folders
simmytarika5
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.