Find empty directoris OR directories with no files in them recursively
There’s the find . -type d -empty command, but that only finds literally empty directories. What I want to achieve is something a little bit more complex: I want to find empty directories or directories that ONLY have other directories or empty directories, and this rule should be passed on recursively. So for example, in this directory structure: ~1/11/111/ It would find 1/ , 1/11/ and 1/11/111/ But if at any point in the tree there’s a file, for example in this case if there’s a file in 1/11/111/file1 then none of them before the file should be returned. So if there’s an empty directory 1/11/111/1111/ next to 1/11/111/file1 , 1111 should be returned. The reason I want this is because I want to delete my empty folders. Now I could probably do that by running find . -type d -empty -delete over and over again, but I want a way to kind of visualize it before I remove them. Is this possible?
If, eventually, you’re going to remove all the empty directories, then you can just iterate the tree depth-first, and remove them as you see them. With the subdirectories removed, the parent directories would then be empty, too. I think there are questions about that here on the site
questions as this, How to remove all empty directories in a subtree?, but then I just realized you wanted to print them before deleting, and I suppose that’s slightly harder
I’d do this by piping find through sort into an awk-script to filter out directories having content (take a half hour or so, which is longer than I’d spend for this category).
How to list empty folders in linux?
One common task in managing files and directories in a Linux system is to identify empty directories. Empty directories are often unneeded, and can be cleaned up to reduce disk usage or for organizational purposes. In this article, we will discuss various methods to list empty folders in Linux.
Method 1: Using find command
To list empty folders in Linux using the find command, follow these steps:
- Open the terminal on your Linux machine.
- Navigate to the directory where you want to start searching for empty folders. For example, if you want to search for empty folders in the home directory, type cd ~ .
- Run the following command to list all empty folders in the current directory and its subdirectories:
find . -maxdepth 1 -type d -empty
find . -type d -empty -printf "%p\n"
find . -type d -empty -delete
These are some examples of how to list empty folders in Linux using the find command. You can customize these commands by adding more options or changing the starting directory.
Method 2: Using ls command with options
To list empty folders in Linux using the ls command with options, follow these steps:
- Open the terminal.
- Navigate to the directory you want to list empty folders from.
- Use the following command to list empty folders:
- This command uses the -d option to list only directories, and the */ pattern to match only directories that end with a forward slash (i.e., empty directories).
- The output will show the names of all empty directories in the current directory, with each directory name followed by a forward slash.
- If you want to list empty folders in subdirectories as well, add the -R option to the command:
- This will recursively list all empty directories in the current directory and its subdirectories.
- You can also use the find command to list empty folders:
- This command uses the -type d option to search for directories only, and the -empty option to match only empty directories.
- The output will show the names of all empty directories in the current directory and its subdirectories.
- To list only empty directories in the current directory, add the -maxdepth 1 option:
find . -maxdepth 1 -type d -empty
- This will limit the search to the current directory only.
- You can also combine the find command with the ls command to get more detailed output:
find . -type d -empty -exec ls -ld > \;
- This command uses the -exec option to run the ls -ld command on each empty directory found by find .
- The output will show the permissions, owner, group, and modification time of each empty directory, as well as its name.
- You can customize the output format of the ls command by adding additional options, such as -lh to show file sizes in human-readable format, or -t to sort files by modification time.
- With these commands, you can easily list empty folders in Linux using the ls command with options.
Method 3: Using tree command
To list empty folders in Linux using the tree command, follow these steps:
- Open your terminal and navigate to the directory you want to check for empty folders.
- Type the following command:
- tree : command to display the directory tree structure.
- -d : option to display only directories.
- -L 1 : option to limit the depth of the tree to 1 level (i.e., only the immediate subdirectories of the current directory will be displayed).
- | : pipe symbol to redirect the output of the tree command to the grep command.
- grep : command to search for a pattern in the output.
- -P : option to use Perl-style regular expressions.
- ‘^.\s*\d\s*$’ : pattern to match the lines that represent empty directories. This pattern consists of:
- ^ : anchor to match the start of a line.
- . : any character (represented by . ) repeated 10 times, to match the indentation of the directory name in the tree structure.
- \s* : zero or more whitespace characters, to match any padding between the indentation and the directory name.
- \d : exactly one digit, to match the number of files in the directory (which should be 0 for an empty directory).
- \s* : zero or more whitespace characters, to match any padding between the number of files and the end of the line.
- $ : anchor to match the end of a line.
This pattern will match lines that have 10 characters of indentation, followed by a single digit indicating the number of files in the directory (which should be 0 for an empty directory), and no other characters after that.
├── empty_dir1 │ ├── empty_dir2 │ └── empty_dir3
Method 4: Using a shell script
To list empty folders in Linux using a shell script, you can use the find command combined with the -type d option to only search for directories, and the -empty option to only list empty directories. Here’s an example script:
#!/bin/bash find . -type d -empty
This script will output a list of empty directories in the current directory and its subdirectories.
You can also modify the script to search for empty directories in a specific directory by replacing the period ( . ) with the path to the directory you want to search in. For example:
#!/bin/bash find /home/user/Documents -type d -empty
This script will output a list of empty directories in the /home/user/Documents directory.
If you want to save the output to a file, you can use the > operator to redirect the output to a file. For example:
#!/bin/bash find . -type d -empty > empty_directories.txt
This script will output a list of empty directories in the current directory and its subdirectories and save the output to a file named empty_directories.txt .
That’s it! With this script, you can easily find and list empty directories in Linux using a shell script.