Bash – How to use find to delete hidden files and folders

bashfindsearchterminal

How can I find and delete all files, including hidden ones, that do not not have the extension java. I know how to use find to delete files and run this command:

find . -not -name "*.java" -type f -delete 

This recursively searches folders all for all files that do not end in *.java. But the command misses files in folders that are hidden.

I run the command found here: ls -lahR And I found that the command I ran missed files. For example:

./node_modules/.bin:
total 24
drwxr-xr-x  5 whitecat  staff   170B Apr  5 12:47 .
drwxr-xr-x  4 whitecat  staff   136B Apr  5 02:25 ..
lrwxr-xr-x  1 whitecat  staff    19B Apr  5 12:47 nopt -> ../nopt/bin/nopt.js
lrwxr-xr-x  1 whitecat  staff    20B Apr  5 12:47 semver -> ../semver/bin/semver
lrwxr-xr-x  1 whitecat  staff    19B Apr  5 12:47 shjs -> ../shelljs/bin/shjs

What flags do I need to not miss these "hidden" files. I've seen "How to view hidden files using Linux `find` command" and that only shows the command find /path -type f -iname ".java" -ls. I have already use that command and it still misses the hidden files.

Best Answer

With -type f you are looking for files.

The three files you mentioned are symbolic links:

rwxr-xr-x 1 whitecat staff 19B Apr 5 12:47 nopt -> ../nopt/bin/nopt.js

To find links you need to find for -type l

So to delete this symlinks you would have to change your command to (edited with the input of Whitecat's comment below):

find . -not -name "*.java" -type f -delete -o -type l -delete