0

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.

Whitecat
  • 105
  • 5

1 Answers1

3

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 
Whitecat
  • 105
  • 5
chloesoe
  • 335
  • 1
  • 17
  • I have to run a separate command for both `f` and `l`. I tried with both `-type fl` and it didn't work. – Whitecat Apr 05 '17 at 21:15
  • you could use `-type f -o -type l` then you could find symlinks and files at the same time and you don't have to run two commands. – chloesoe Apr 05 '17 at 21:30
  • The final command would look like this: `find . -not -name "*.java" -type f -o -type l -delete` – Whitecat Apr 05 '17 at 21:35
  • @Whitecat You should probably group it like `\( -type f -o -type l \)`. A future findutils release will support `-type f,l` as well. – Tavian Barnes May 03 '17 at 18:17