0

How can i loop through a directory (and subdirectories) and delete every .log file? I want to use a bash script to clean my Minecraft server directory.

In Windows (batch) i use this line to loop: for /r %%i in (*.log) do del /F %%i

Vico
  • 276
  • 1
  • 4
  • 15
  • Possible duplicate of [How to delete all hidden .swp files from terminal](https://superuser.com/questions/702913/how-to-delete-all-hidden-swp-files-from-terminal) – Jim L. Jul 09 '19 at 23:05

1 Answers1

1

To print all *.log files recursively using the current directory as start directory (just to make sure these are the right files), use:

find . -name "*.log" -type f

And to delete them, use:

find . -name "*.log" -type f -exec rm {} +
Freddy
  • 1,465
  • 6
  • 13
  • i've used `find . -name "*.log" -type f -delete` and it worked. Thanks! – Vico Jul 10 '19 at 19:21
  • Note that the `-delete` option is GNU specific and is not defined in the [posix standard](https://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html) and thus not available in all `find` implementations. And be careful with this option. If you use it as the first option, you can easily delete the whole directory tree. – Freddy Jul 10 '19 at 19:37