Find All (Non-)Empty Files Inward A Directory

Creating an empty file inwards Linux is easy. If a-file does non exist, practise the file in addition to choke far empty past times simply:
$ acquit on a-file
$ ls -l a-file
-rw-r--r-- 1 peter peter 0 2009-05-02 20:15 a-file

Finding all empty files inwards a directory tin every bit good move done using a unmarried command. Ditto for non-empty files.

Suppose yous desire to discovery all empty files inwards the directory /home/peter. The command is:
 $ discovery -L /home/peter -maxdepth 1  -type f -size 0

By default, the find command excludes symbolic files. Use the -L selection to include them.

The human face -maxdepth 1 specifies that the maximum depth to which the search volition drill is i only. By default, the find command volition recursively choke downward the directory. Influenza A virus subtype H5N1 maximum depth of 1 agency that yous alone desire the files ask nether /home/peter. Keep inwards heed that depth 0 is the marker of the command describe of piece of occupation declaration (/home/peter). You tin purpose -maxdepth in addition to -mindepth to finely command the depth levels yous desire included.

-type f agency include regular files only. This is non strictly necessary for empty files (as opposed to non-empty ones) because all directories, fifty-fifty those amongst no files, are of non-empty size.

-size 0 is self-explanatory.

To discovery all non-empty files inwards the same directory, merely add together ! before -size 0:
 $ discovery -L /home/peter -maxdepth 1  -type f ! -size 0

Note that -type f becomes necessary. Without this expression, subdirectories inwards /home/peter such every bit /home/peter/.Trash volition look inwards the output.

Most of the time, yous don't exactly desire to discovery the files: yous desire to practise something amongst them. Suppose yous desire to take away all empty files inwards the directory. For safety, kickoff move the find command past times itself to listing the files. Then amongst the xargs command every bit below.
 $ discovery -L /home/peter -maxdepth 1  -type f  -size 0 
/home/peter/file1.txt
/home/peter/file2.txt
$ discovery -L /home/peter -maxdepth 1 -type f -size 0 -print0 |xargs -0 -r rm -f

For to a greater extent than data virtually how to purpose the find in addition to xargs commands, come across my earlier weblog entry.

Berlangganan update artikel terbaru via email:

0 Response to "Find All (Non-)Empty Files Inward A Directory"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel