+64-3-3595735

Home » Tips and Hints » Linux » Recursive Actions On Folders using BASH in Linux

Recursive Actions On Folders using BASH in Linux

Previously I posted how to reset file permissions recursively using Linux Bash / Command Line.

Building on that is how to recursively remove files and folders using bash on a Linux box.

find . -type d -name 'folder_*_name' -exec rm -r {} +

Using find to find a folder (-type d) or a file (-type f) you then pipe that through -exec and run the command you want to on the found folder.

This is a similar process to resetting file permissions. exec runs the command then returns control to find which finds the next file that matches the find syntax.

Some examples”

  • find files ending in .log and remove them  find . -type f -name '*.log' -exec rm -r {} +
  • find files with .log in the file name and remove them  find . -type f -name '*.log*' -exec rm -r {} +
  • find temp folders  and remove them  find . -type d -name 'tmp' -exec rm -r {} +
  • find all captcha folders in websites hosted under /var/www and remove them  find /var/www/ -type d -name '*captcha*' -exec rm -r {} +

Recursively changing file permissions can be found here: Reset permissions for files and folders in Linux / Bash