Shell Script to Check if Every Passed Argument is a File or Directory
Last Updated :
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.
Approach 1:
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.
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

Approach 2:
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.
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
Similar Reads
How to check if a directory or a file exists in system or not using Shell Scripting? Shell scripting is really a powerful and dynamic way to automate your tasks. To test if a directory or file already exists in the system or not we can use shell scripting for the same along with test command. To proceed with the test script lets first check the test manual. To open a manual use the
2 min read
Automated Recursive Encryption in a Directory Using Shell Script This script would encrypt file provided as an argument or a directory and its constituent files and sub-directories recursively. It would be very useful for automating the encryption of multiple files in a directory all together. How it works? If no arguments are given, throw an error and exit the p
3 min read
Shell Script to Delete Zero Sized Files From a Directory A zero-sized file is a file that contains no data and has a length of 0. It is also called a zero-byte file. Incomplete transfers can be responsible for the zero-sized file. You can create a zero-sized file intentionally by running the following command in the terminal : touch zero_sized_file_name O
3 min read
How to check the given path is file or directory in node.js ? Sometimes there is a need to check whether the given path is a file or directory so that different operations can be performed based on the result. For instance, to log the information of the directory and file separately. In Node.js, file handling is handled by the fs module. You can read more abo
4 min read
Shell Script to List Files that have Read, Write and Execute Permissions In this article, We will learn how to list all files in the current directory that have Red, Write and Execute permission. Suppose, we have the following files in our current directory : Here, We have a total of 8 files in our current directory. Out of 8, we have Read, Write and Execute permission o
3 min read