The Curious Website Designer

Bash - Recursively Removing Files and Folders

Posted by The Curious Website Designer | Posted on Sun 10 Jan 2021

Bash - Recursively Removing Files and Folders

Another reminder how to perform a routine task from the command line or in a Bash script.

This time it's deleting files & folders. Also how to remove a group of files that are older than (x) days.

 

To remove files recursively, the syntax to use is:

rm -rf /path/to/files/*

 

The 'f' in the command above is to force the file to be removed; it avoids the need for the user to confirm deletion.

 

To remove files older than 5 days, use:

find /path/to/folder/clamscan-2*.log -mtime +4 -exec rm {} \;

 

Note: The backslash and semi-colon are important. Don't leave them out.

 

 

Related Articles

Bash - How to Send stdout & stderr To a Logfile

Bash - How to Send stdout & stderr To a Logfile

Posted by: The Curious Website Designer
on Sat 28 Apr 2018

Debugging CRON jobs can be quite difficult - there is no visible output, so you don't know if the script failed to run or if the script ran but failed. If it's the latter (ran and failed), you don't get to see any error messages.

Bash - How To Recursively CHMOD Files And Folders

Bash - How To Recursively CHMOD Files And Folders

Posted by: The Curious Website Designer
on Sun 10 Jan 2021

Occasionally, especially after extracting an archive, the file and folder permissions are not as they should be; I had a recent example where many files were left with 640 permission instead of 644. As a reminder to myself, here's how to do it.

Tags: recursively delete files, bash, delete files, deleting files, delete, deleting