Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

bash - How do I check if a folder has contents?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can check if a directory is empty using find, and processing its output:

#!/bin/sh
target=$1
if find "$target" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
    echo "Not empty, do something"
else
    echo "Target '$target' is empty or not a directory"
fi

That is:

  • Use find to find the first filesystem entry under $target (-mindepth 1), print it (-print), and stop processing (-quit)
    • Redirect stderr to suppress any error messages (= noise)
  • Check if the output of the find command is empty using grep -q .
    • grep -q . will exit after processing at most one character. If it sees a character it exits with success, if it doesn't (its input is empty) then it exits with failure.
  • If the output of the find command is not empty, then the directory is not empty, and grep -q . exits with success.
  • If the output of the find command is empty, then $target is either an empty directory, or not a directory (does not exist), and grep -q . exits with failure.

The reason we have to rely on the stdout of find rather than its own exit code directly is that there's no way to make the find command use distinguishable exit codes in case files were found or not.

Instead of piping to grep -q, another alternative would be to capture the output of find and check if it's an empty string or not.

#!/bin/sh
target=$1
if [ "$(find "$target" -mindepth 1 -print -quit 2>/dev/null)" ]; then
    echo "Not empty, do something"
else
    echo "Target '$target' is empty or not a directory"
fi

Capturing command output like this uses a sub-shell. I think the solution using grep is probably faster, but I haven't tested it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...