How to List Empty Folders in Linux

Finding empty directories

Check whether find <dir> -type f outputs anything. Here's an example:

for dir in A B C; do
[ -z "`find $dir -type f`" ] && echo "$dir is empty"
done

How to find all empty folders and untracked files when using git?

TL;DR: just look for empty directories. You can safely remove them—well, "safe" depends on your own software, but as far as Git is concerned, it's safe. (Watch out for missing files—see the definition of "missing" below—which may remove a directory that Git might want later, but that's sort of OK, because Git will just create it again.)

On a Unix / Linux system (edited to correct lost word in transcription):

find . -name .git -prune -o -type d -empty -print

(at the top level of the work-tree) will find the empty directories.

Long(ish)

Git is not interested in folders / directories. There's no such thing as an untracked folder in the same way that there's no such thing as a tracked folder: Git only cares about files. Specifically, a file is either in the index, or not in the index, and if it's not in the index, it's untracked.

When you use the various options to list untracked files (which tend to skip over ones that are untracked-and-ignored since you normally want that), Git will, sometimes, aggregate together all the files that are in some folder, notice that there are no tracked files in that folder, and report them using the aggregated notation. You can stop this with, e.g., git status --untracked-mode=all; then you'll get the individual file names.

Note that it's possible to have some file that is tracked, yet missing. For instance, suppose sub/README.txt is a tracked file, and actually exists. Then we run rm sub/README.txt. The file sub/README.txt remains in Git's index, and will be in the next commit, but it's missing. If that was the only file in sub in your work-tree, sub is now empty, and you can remove it with rmdir sub. Even though sub/README.txt remains missing (and sub is missing too!), that does not affect the next commit: it will still contain sub/README.txt, because that file is in the index. (Using git rm --cached sub/README.txt, you can remove it from the index too, if that's what you wanted.)

If and when Git goes to copy sub/README.txt back out of the index into the work-tree, Git will, at this point, discover that there is no sub. Git will merely shrug its metaphorical shoulders and create the directory sub, and then put sub/README.txt into it. So this is why Git is not interested in folders / directories: they're just boring and dull, required only when needed to hold files, created on demand.

If you want Git to create a directory, you need to store a file in it. Since programs managed by Git need to be able to ignore the file named .gitignore, this is a very good file name to stick into such a directory. You can write * into that file, and add it to your commits, so that Git will create the directory and write a .gitignore file there containing *, and will thus ignore all additional untracked files within that directory automatically.

Side note: In general, when Git pulls the last file out of some directory, it will remove the directory too, but occasionally I've seen it leave some behind. (Of course, it has to leave the directory behind if it still contains some untracked files. Note that git clean -fd will remove the empty directories, though it also removes the untracked files.)

How to list non-empty directories with the command find or ls?

You're asking for "things that are not directories and are empty", which includes all zero-length files (and would exclude all directories). You want "things that are directories and are not empty", so:

find . -type d ! -empty -print

How to list non-empty subdirectories on linux?

 find . -mindepth 1 -maxdepth 1 -not -empty -type d

will give you all nonempty directories. If you want to exclude directories that contain only other directories (but no files), one of the other answers might be better...

Shell script - Linux - list empty directories

Try this

#!/bin/sh
find "$1" -type d -empty

This script will run relative to the current directory. If you're in bds2 and pass it Documents it will look for a directory in bds2 called Documents. But you can pass it a relative or absolute path to your directory and it will work, e.g.

$ ./getemptydir.sh ../../Documents
$ ./getemptydir.sh ~/Documents
$ ./getemptydir.sh /Users/xyz/Documents

etc.

or from the Home directory, you would run

$ Documents/bds/bds2/getemptydir.sh Documents

If you wanted the script to always look in your home directory, you could include that in the script, e.g.

#!/bin/sh
find "$HOME/$1" -type d -empty

List non empty directories

You can use list.files on the result of list.dirs:

dirlist <- list.dirs("./R/R-3.3.1/library/zoo")
dirlist [sapply(dirlist, function(x) length(list.files(x))>0)]

find directories

What about this:

grep -v "." *

I mean the following: "." means any character (I'm not sure the syntax is correct), so basically you look for every file which not even contain any character.



Related Topics



Leave a reply



Submit