Shell Script to Delete Zero Sized Files From a Directory Last Updated : 30 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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 Output: zero-sized file Approaches: Input directory name and check if the directory exists in the current folder/directory.Use for loop to traverse each file and check their size.Approach 1: Steps included: Firstly we are taking the directory name as input from the user using the read command.To check if the directory name entered by the user really exists, we are using the if [-d "$directory_name"] statement. Here we are using if statement with the -d flag it will return true if the directory exists.If the directory exists then we are going to use the looping statement (for loop) to iterate over files and check their size using -size 0, if the file is zero-sized then we enter the loop and remove the file using the rm command. rm (remove) is used to delete the files/folder.After that, we are displaying that the files have been successfully deleted using the echo command. echo is just like a print statement it is used to display things on the screen.If the directory doesn't exist we are going to display the directory that doesn't exist.#! /bin/bash # Taking directory name as input from user echo -n "Enter name of the directory : " read directory_name # If directory exists it will print # Directory exits # and remove the zero-sized files. # Or if directory doesn't exists it will print # Directory does not exists. if [ -d "$directory_name" ]; then echo "Directory exist" for i in `find $directory_name -size 0` do rm $i echo "Zero-sized files are Successfully deleted" done else echo "Directory does not exist" fiApproach 2: Steps included: First, we have to take the directory name as input from the user using the read command and store it in variable directory_name.To check if the directory name entered by the user really exists, we are using the if [-d "$directory_name"] statement. Here we are using if statement with the -d flag it will return true if the directory exists.If the directory exists then we are going to use the looping statement (for loop) to iterate over files and check their size using -size 0, as soon as we found a file that meets our requirements (i.e. file with zero-sized), using -delete argument we will delete that particular file from that directory. We do the same until there is no file with zero sizes.After that, we are displaying that the files have been successfully deleted using the echo command. echo is used to display things on the screen.If the directory doesn't exist we simply return that the directory that doesn't exist using the echo command.#! /bin/bash # Taking directory name as input from user echo -n "Enter name of the directory : " read directory_name # If directory exist it will print # Directory exits # and remove the zero-sized files. # Or if directory doesn't exist it will print # Directory does not exist. if [ -d "$directory_name" ]; then echo "$directory_name Directory exist" for i in `find $directory_name -size 0 -delete` do echo "" done echo "Zero-sized files are Successfully deleted" else echo "$directory_name does not exist" fi Output : Before After using script Comment More infoAdvertise with us Next Article Shell Script to Delete Zero Sized Files From a Directory A amnindersingh1414 Follow Improve Article Tags : Linux-Unix Shell Script Similar Reads Shell Script to Delete the Zero Sized File Using If and For There are many files in our system which are of no use for us as temporary files. So we need to clear all those files, but it is very hectic to find each file and delete them. Here we will create a script that will automatically search for the zero-sized file and delete them all. Before starting, we 2 min read Shell Script to Delete a File from Every Directory Above the Present Working Directory In this article, we will discuss how to delete a file from every directory above the parent working directory using shell script in Linux. To delete a file from a directory we use the rm command in Linux/UNIX. Syntax: rm [OPTION]... [FILE]... Using shell script, we have to delete the input file from 2 min read Shell Script to Measure Size of a File Understanding the size of a file is fundamental for various reasons. It allows users to monitor disk space usage, identify large or unnecessary files, and make informed decisions about storage management. The ability to measure file size programmatically through a shell script enhances efficiency an 4 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 Tools to Securely Delete Files from Linux Every time you delete a file from your Linux system using the shift + delete or rm command, it doesn't actually permanently and securely delete the file from the hard disk. When you delete a file with the rm command, the file system just frees up the appropriate inode but the contents of the old fil 5 min read Like