I have a lot of directories and sub-directories. I want to recursively remove one specific file from a directory if it's the only one there. Also assume that there are no sub-directories in that directory. So, to summarize, the assumptions are:
- The file name is README.TXT (case insensitive)
- There are no other files or directories in the directory where that file is found
Structure:
mkdir usecase cd usecase mkdir destroy_this touch destroy_this/readme.txt mkdir do_not_destroy_this touch do_not_destroy_this/readme.txt touch do_not_destroy_this/something-else.txt mkdir do_nothing cd do_nothing mkdir rm_this touch rm_this/README.TXT cd .. mkdir do_nothing_here cd do_nothing_here mkdir has_sub_dir touch README.TXT cd .. Running the above would results in this tree structure:
$ tree . . `-- usecase |-- destroy_this | `-- readme.txt |-- do_not_destroy_this | |-- readme.txt | `-- something-else.txt |-- do_nothing | `-- rm_this | `-- README.TXT `-- do_nothing_here |-- has_sub_dir `-- README.TXT This is what I've got so far:
find . -type f -iname "readme.txt" -exec sh -c 'ls $(dirname "{}") | wc -l' \; The idea is that I count the number of files and directories and if it's one then remove the file.