mirror of
https://github.com/jbranchaud/til
synced 2026-08-31 16:51:46 +00:00
Add Delete Empty Files With Find as a Unix TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||
|
||||
_1860 TILs and counting..._
|
||||
_1861 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -1765,6 +1765,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Curling For Headers](unix/curling-for-headers.md)
|
||||
- [Curling With Basic Auth Credentials](unix/curling-with-basic-auth-credentials.md)
|
||||
- [Deduplicate List While Preserving Original Order](unix/deduplicate-list-while-preserving-original-order.md)
|
||||
- [Delete Empty Files With Find](unix/delete-empty-files-with-find.md)
|
||||
- [Determine ipv4 And ipv6 Public IP Addresses](unix/determine-ipv4-and-ipv6-public-ip-addresses.md)
|
||||
- [Diff Two Files In Unified Format](unix/diff-two-files-in-unified-format.md)
|
||||
- [Different Ways To Generate A v4 UUID](unix/different-ways-to-generate-a-v4-uuid.md)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# Delete Empty Files With Find
|
||||
|
||||
I was discussing a command with a colleague for finding and deleting empty files
|
||||
from a given directory. This command involved a series of pipes and I probably
|
||||
wouldn't have solved it too differently. I was curious what other ways there
|
||||
were of doing such a task, so I asked Claude. The first option it came back with
|
||||
taught me about two new-to-me flags that `find` supports.
|
||||
|
||||
First is the `-empty` flag which applies a filter on the results to files or
|
||||
directories that are empty.
|
||||
|
||||
Second is the `-delete` flag which will delete found files and directories.
|
||||
|
||||
Those two can be combined to _delete_ any results that are _empty_. Then to
|
||||
target just _files_, I can include the `-type f` flag. And if I want to prevent
|
||||
it from recursing down some unexpected tree of directories, I could also add in
|
||||
`-maxdepth 1`.
|
||||
|
||||
To delete all empty files in `specific-directory`, I can run the following:
|
||||
|
||||
```bash
|
||||
❯ find ./specific-directory -maxdepth 1 -type f -empty -delete
|
||||
```
|
||||
|
||||
My first thought for a command like this is that I will need to `xargs rm`, so
|
||||
it's neat to know about the `-delete` flag in particular.
|
||||
|
||||
See `man find` for more details.
|
||||
Reference in New Issue
Block a user