Skip to content
Related Articles

Related Articles

Improve Article

Shell Script to Delete Zero Sized Files From a Directory

  • Last Updated : 30 Apr, 2021

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:

creating zero sized file sin linux

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:

  1. Firstly we are taking the directory name as input from the user using the read command.
  2. 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.
  3. 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.
  4. 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.
  5. 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"

fi

Approach 2:

Steps included:

  1. First, we have to take the directory name as input from the user using the read command and store it in variable directory_name.
  2. 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.
  3. 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.
  4. 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.
  5. 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 :

zero_sized_files

Before 

script to delete zero sized file

After using script

My Personal Notes arrow_drop_up
Recommended Articles
Page :