0

Write a script that deletes all the regular files (not the directories) with a .js extension that are present in the current directory and all its subfolders.

The answer should only contain one command after the shebang line, I've tried the following:

#!/bin/bash
rm -R *.js 

… and:

#!/bin/bash
rm -f *.js
Obsidian
  • 3,719
  • 8
  • 17
  • 30

2 Answers2

0
find . -name "*.js" -delete

Find all files in the current and child directories with the extension .js and delete the files.

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • The command above will not only delete files with the extension ```.js``` but also directories that include ```.js``` extension at the end. A directory with name ```my_dir.js```. You need to specify the type using ```-type f``` for file. – Onen simon Mar 09 '22 at 20:08
0

The best way to achieve this remains the find command:

find . -type f -name '*.js' -exec rm -f {} \;

If however you want to stick to rm alone, it remains possible if you know exactly how many subdirectories lie under the place you're working in. For instance:

rm -f */*/*.js
Obsidian
  • 3,719
  • 8
  • 17
  • 30