How to Remove Zero Byte Files

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.

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

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).

remove all the files of zero size in specified directory

find . -size 0c -delete removes all such files in the current folder.

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

jolt spec to remove 0 byte files

You can use successive shift transformations along with a conditional logic to separate the case when length = 0 or !=0, and lastly use a remove transformation to drop the unnecessary attribute such as

[
{
"operation": "shift",
"spec": {
"*": "&", // elements other than "content" array
"content": {
"*": {
"length": {
"0": "AttributeToRemove", // case when length = 0
"*": { // case when length != 0
"@(2,objectName)": "files.&3.filename",
"@(2,path)": "files.&3.filepath",
"@1": "files.&3.filesize"
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"fi*": {
"*": {
"@": "&2[]" // go 2 levels up the tree to grab the literal `files` to replicate by using &2
}
}
}
},
{
// get rid of redundantly generated attribute due to length = 0 case
"operation": "remove",
"spec": {
"AttributeToRemove": ""
}
}
]

the demo on the site http://jolt-demo.appspot.com/ is

Sample Image



Related Topics



Leave a reply



Submit