문제

I want to list all empty directories below the directory specified in the input parameter using a shell script, but all I can find is command for listing empty directories in the current directory only. Please help.

For example: I have:

Documents
   directory 1 (the empty directory)
   directory 2
      Subdirectory 1
      Subdirectory 2 (where I have my shell script, getemptydir.sh)

So when I type ./getemptydir.sh Documents, I would want it to print out directory 1

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top