How to check if a directory or a file exists in system or not using Shell Scripting? Last Updated : 30 Jun, 2019 Comments Improve Suggest changes Like Article Like Report 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 man command as follows: man test Right now we will be covering these two highlighted arguments in your script, further, you can use and test all of them. First, create a shell script file like test.sh using any editor of your choice like nano, vim or sublime and give it executable permissions(chmod +x). In the below example we are checking if /usr/games directory is present or not. shell #!/bin/bash if [ -d /usr/games ]; then echo "The Directory Exists" else echo "The Directory is not present" fi Let's see another example where we have deleted a file from the directory and let's check what happens. shell #!/bin/bash if [ -e /usr/games/master.txt ]; then echo "The file Exists" else echo "The file is not present" fi Now we used -e argument to check for the file and we got the message that file is not present as we had deleted it. Like this, you can play with the shell script, refer to the test manual and test more awesome stuff with this. Comment More infoAdvertise with us Next Article How to check if a directory or a file exists in system or not using Shell Scripting? M Madhusudan_Soni Follow Improve Article Tags : Technical Scripter Linux-Unix Similar Reads Shell Script to Check if Every Passed Argument is a File or Directory 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 prov 4 min read How to Check a File or Directory Exists in Java? One of the most frequent tasks carried out by a file system in an operating system is checking the existence of a directory or a file. In the form of library functions, most programming languages provide some level of file system accessibility. You will discover how to test an existing file or direc 1 min read Bash Scripting - How to check If File Exists In this article, we will write a bash script to check if files exist or not. Syntax :test [expression][ expression ][[ expression ]] Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: - -f: It returns True if the file exists as a com 6 min read Shell Script to List all Hidden Files in Current Directory Here we are going to see how to write a script to list all hidden files in the current directory, But before starting we will see how to hide the file in the current directory. Hide Files in Linux: In Linux, the files which start with a period (.) sign are the hidden files. We will write a small scr 2 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 Like