In this config you will learn how to find all empty files within a given directory using find the find
command. Here is our sandbox directory /tmp/temp
containing files from which some of them are empty:
. ├── dir1 │ ├── dir2 │ │ ├── file3 │ │ └── file4 │ ├── file2 │ └── file3 ├── file1 └── file2 2 directories, 6 files
Let’s first locate all empty files recursively starting from a current working directory using find
command:
$ pwd /tmp/temp $ find . -type f -empty OR $ find /tmp/temp -type f -empty ./dir1/dir2/file4 ./dir1/file3 ./file2
The following linux command will search for all empty file only within a current working directory, that is, not recursively:
$ find . -maxdepth 1 -type f -empty ./file2
To remove all empty files we will combine the find
command with its exec
option. The following linux command will remove all empty files found recursively:
$ find . -type f -empty -exec rm "{}" \; OR $ find /tmp/temp -type f -empty -exec rm "{}" \; $ tree . ├── dir1 │ ├── dir2 │ │ └── file3 │ └── file2 └── file1 2 directories, 3 files