0

This might be unusual, as searching hasn't gotten me quite the right answer. Trying to simplify: I have 15 subdirectories, each one contains a differently named .vhd file.

I'd like to wipe all of the .vhd files from these subdirectories without removing the directories themselves.

Right now I just cd into the subdirectory, rm the .vhd file, then cd ../next directory, delete. I've tried fiddling with rm, but can't quite get it to work. I thought that rm -rf *.vhd would work, but nope. Do I need to write a shell script or is there a single command I can accomplish this with?

1 Answer 1

5

You can run the following command in the directory that contains all the other 15 subdirectories:

rm */*.vhd

This will delete all files that match *.vhd (all files that end with .vhd) that are inside any directory in the current working directory (that's what the */ is for).

Since *.vhd files are not directories, you don't need the -r flag on rm. And if you have permissions on the files, you shouldn't need the -f flag either.

3
  • Nice and clear answer. A little correction: -f is not related to permissions: you will not be able to delete if you do not have permissions, whether you use -f or not. -f is there to suppress any prompts and continue even if nonexisting files were supplied on the command line.
    – vanadium
    Sep 15, 2022 at 19:29
  • If you have write permissions on a file, you won't get prompted if you're sure you want to delete it. If you don't, even if you own the file (and can give yourself write permissions), you will get prompted. In this case, OP most likely wouldn't get prompted before removing the file regardless, so the -f is for sure unnecessary.
    – Esther
    Sep 15, 2022 at 19:35
  • @Esther Ah ha! Tested and confirmed that's exactly what I needed! I knew it had to be something simple I was missing. Thank you very much for the answer, it will save me a lot of unnecessarily wasted time every month. Funny coincidence, one of the servers in question in this process is named Esther, hah. Thanks again!
    – Isaac A
    Sep 16, 2022 at 14:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .