How can I improve my simple script for finding empty directories?

0
9
Asked By CoolBean23 On

I'm looking for suggestions on streamlining my script that recursively finds empty directories. The script lists them, opens the list in vim for editing (where a user can delete entries that shouldn't be removed), and after saving, prints the updated list to confirm before removal. I'm eager for constructive feedback, whether it's minor tweaks or major suggestions, especially from seasoned bash users. Also, just to clarify, I'm using the `find` command instead of `fd`, as the latter isn't standard, and I'm okay with bashisms since I've already incorporated arrays.

3 Answers

Answered By InfoJunkie50 On

Just a heads up, `find` isn't a bash-ism; it's a core utility found in most systems. You're using `printf`, which is great! For checking your array, try avoiding unnecessary nesting; instead, you can use `(( ${#empty_dirs[@]} )) || exit 0` to simplify your exit condition. Lastly, just be cautious with naming conflicts — for example, `fd` can refer to both filesystem entries and a floppy disk device, so making sure your script uses the right context is important.

Answered By SyntaxSorcerer12 On

A few tips: for building your `dirs` array, you can simplify it to `dirs=("${@:-.}")` — this handles the default case if no arguments are passed. Also, there's no need for the `else` after your `if` check for empty directories; just exit if there are none. This keeps your code clearer. And double-check your variable expansions like `${B}` and `${E}` — make sure they're defined in the script; running `shellcheck` could help catch these issues.

Answered By CodeNinja93 On

It’s crucial to clarify the logic you want. Are you only targeting completely empty directories, or should it also catch directories that only contain other directories? This distinction can really affect how you structure the script. If you're trying to clean out a complex directory structure, you'll want to think about how best to prompt users without making it an overwhelming task. Plus, when designing for user interaction, consider if you want to ask about each directory individually or provide one comprehensive prompt for everything found.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.