Linux Delete File with Size 0

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

remove all the files of zero size in specified directory

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

How to remove all files that are empty in a directory?

The easiest way is to run find with -empty test and -delete action, e.g.:

find -type f -empty -delete

The command finds all files (-type f) in the current directory and its subdirectories, tests if the matched files are empty, and applies -delete action, if -empty returns true.

If you want to restrict the operation to specific levels of depth, use -mindepth and -maxdepth global options.

Solve a shell script where i have to delete the files with size 0?

(recommend editing the posting where the script is complete and presented as code; eg the leading # is missing; note: invert the script and click the {} button)

As shown, the script seems somewhat odd; recommend checking stuff like whether the cmd-line arg-count is correct and then whether args $1 and $2 are valid; the expression /$1/* indicates you want to repeat the same search based on the wildcard * result of everything within the absolute path /$1, but the trailing /* is not needed (find will normally descend into subdirs), meanwhile the directory passed to find is the invariant abs path /$2. Here's a revision with some elements guessed:

#!/bin/bash

[ $# -ne 2 ] && { echo Usage: ${0##*/} dir user 1>&2; exit 1; }

# search the unmodified dir $1 for any file owned by $2 with size 0 and run an interactive delete
find $1 -user $2 -type f -size 0 -exec rm -i {} \;

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

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.



Related Topics



Leave a reply



Submit