Write a shell script to remove the zero sized file from the current directory.
In this article, you will discover 3 methods for writing a shell script that locates and deletes zero-sized files in the current directory. All three approaches share two common steps: firstly, clearing the terminal using the clear
command, and secondly, displaying a relevant message to confirm the successful deletion of files to the user.
Method 1: find -size 0c -delete
clear
find -size 0c -delete
echo "All zero-sized files successfully deleted"
As you can see in the above script, find -size 0c -delete
uses the find
utility to search for files in the current directory (represented by .
).
-size 0c
specifies the size criteria for the files to be searched. Here, 0c
means files with a size of 0 bytes (0
for size and c
for bytes). So, it searches for files with a size of 0 bytes.
The -delete
flag instructs find
to delete the files that match the specified criteria.
Method 2: find . -type f -size 0 -delete
clear
find . -type f -size 0 -delete
echo "All zero-sized files successfully deleted"
Again the find
command is used to search for files in the current directory denoted by .
The -type f
option specifies that we are looking for regular files only (not directories or other types of files).
The -size 0
option specifies that we’re searching for zero-sized files.
The -delete
option tells find
to essentially search for zero-sized files in the current directory and delete them.
Method 3: find . -type f -empty -delete
clear
find . -type f -empty -delete
echo "All zero-sized files successfully deleted"
Previous method uses -size 0
, which directly checks for files with a size of 0 bytes whereas above method uses -empty
, which checks for files with no data, effectively also identifying zero-sized files. In the end, both do the same job by deleting the empty or zero-sized files.
Comments