How to delete many 0 byte files in linux?
Use find
combined with xargs
.
find . -name 'file*' -size 0 -print0 | xargs -0 rm
You avoid to start rm
for every file.
Linux delete file with size 0
This will delete all the files in a directory (and below) that are size zero.
find /tmp -size 0 -print -delete
If you just want a particular file;
if [ ! -s /tmp/foo ] ; then
rm /tmp/foo
fi
How to remove zero byte files
Assuming you have a version of find
compliant enough with POSIX 2008 to support the +
notation:
find foo -size 0 -exec rm -f {} +
If you don't, there are variants you can use:
find foo -size 0 -print0 | xargs -0 rm -f # But you probably have + anyway
find foo -size 0 -exec rm -f {} \; # Slow but reliable
find foo -size 0 -print | xargs rm -f # Fails with spaces etc in file names
And the accepted answer to the duplicate question suggests -delete
, which is good when it is supported by the find
you are using (because it avoids the overhead of executing the rm
command by doing the unlink()
call inside find
):
find foo -size 0 -delete # Not POSIX standard
remove all the files of zero size in specified directory
find . -size 0c -delete
removes all such files in the current folder.
Deleting empty (zero-byte) files
Easy enough:
find . -type f -size 0 -exec rm -f '{}' +
To ignore any file having xattr content (assuming the MacOS find
implementation):
find . -type f -size 0 '!' -xattr -exec rm -f '{}' +
That said, note that many xattrs are not particularly useful (for example, com.apple.quarantine
exists on all downloaded files).
How delete all the empty files in linux irrespective of their directory using rm command
Well, rm(1)
command only deletes the files whose names you pass to it on the command line. There's no code in rm(1)
to allow you to filter those files, based on some condition or criteria. The old UNIX filosophy mandates here, write simple tools and couple them on pipes as neccessary to construct complex commands. In this case, find(1)
is the answer... this is a tool to select files based on quite arbitrary criteria (like the one you ask for) and generate the actual file names or simply call commands based on that. On this respect
find dir1 dir2 ... -type f -size 0 -print | xargs rm
would be the solution (batching the filenames with xargs(1)
command to do less fork(2)
and exec(2)
calls with less process fork overhead) to your problem, allowing to specify several dirs, selecting only files of size 0 and passing them to the batch command xargs(1)
to erase them in groups. you can even filter the filenames based on some regular expression with
find dir1 dir2 ... -type f -size 0 -print | grep 'someRegularExpression' | xargs rm
and you'll get erased only the files that match the regular expression (and the other two predicates you expressed in find(1)
) You can even get a list of the erased files with
find dir1 dir2 ... -type f -size 0 -print | grep 'someRegularExpression' | tee erased.txt | xargs rm
See find(1)
, grep(1)
, tee(1)
, xargs(1)
and rm(1)
for reference.
Deleting empty (zero-byte) files
Easy enough:
find . -type f -size 0 -exec rm -f '{}' +
To ignore any file having xattr content (assuming the MacOS find
implementation):
find . -type f -size 0 '!' -xattr -exec rm -f '{}' +
That said, note that many xattrs are not particularly useful (for example, com.apple.quarantine
exists on all downloaded files).
Using bash I need to perform a find of 0 byte files but report on their existence before deletion
With prompting from @JonathanLeffler I have succeeded with the following:
#!/bin/bash
## call this script with: find . -type f -empty -exec handleEmpty.sh {} +
for file in "$@"
do
file2="$(basename "$file")"
echo "$file2" >> "$(dirname "$file")"/deletedFiles.txt
rm "$file"
done
This means I retain a trace of the removed files in a deletedFiles.txt flag file in each respective directory for the users to see when files are missing. That way, they can pursue going back to archive CD's to retrieve these deleted files, which are hopefully not 0 byte files.
Thanks to @John1024 for the suggestion of using the empty
flag rather than size
.
Related Topics
@Reboot Is Not Working in Cron
How to Check If Hadoop Daemons Are Running
Linux Bash - Parse Date in Custom Format
How to List the Size of Each File and Directory and Sort by Descending Size in Bash
What Is Raw Socket in Socket Programming
How to Set a Static Ip Address in a Docker Container
Location of .Bashrc for "Bash on Ubuntu on Windows" in Windows 10
Which Drivers Are Used by Usb Mouse in Linux Kernel
Bash Alias with Argument and Autocompletion
How to Write Linux Driver Module Call/Use Another Driver Module
Changing Permissions of Files in a Directory Recursively
Why Does This Code Crash with Address Randomization On