1

I've been spending the better part of an afternoon trying to figure this one out. 🤔 I have a directory that contains several project directories, and within each project directory there is a node_modules directory. I'd like to delete all files hidden included within the node_modules directory but keep the node_modules directory, ie. have an empy node_modules directory.

I've constructed a couple quasi solutions, but none seem to preserve / keep the node_modules directory. I'm using GNU find and grep on macOS 10.12.x via homebrew with fish shell.

2
  • Do you want to delete only the "hidden" files (in Unix, a file whose name starts with a . is hidden) with node_modules or do you want to delete all files within node_modules?
    – John1024
    Mar 1 '18 at 21:58
  • 1
    @John1024 all files within node_modules
    – ipatch
    Mar 1 '18 at 22:01
2

To delete all files within any directory that is under the current and is named node_modules while keeping node_modules:

find . -path  '*/node_modules/*' -delete

(This was tested with GNU find but I expect that BSD/OSX find will behave similarly.)

Example

Let's create a node_modules directory with some files in it:

$ mkdir -p node_modules/dir{1..3}
$ touch node_modules/file{1..3}

Now, let's see what files this find command returns:

$ find . -path  '*/node_modules/*'
./node_modules/dir3
./node_modules/dir1
./node_modules/file3
./node_modules/file2
./node_modules/file1
./node_modules/dir2

This shows the files inside node_modules but not the node_modules directory itself. Thus, we can delete the files with:

$ find . -path  '*/node_modules/*' -delete
$ ls
node_modules
1
  • 1
    thanks dude 👍 and as stated above in my question I have GNU find installed via homebrew. 🍺
    – ipatch
    Mar 1 '18 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.