Delete Old Files: Shell Script Guide
To delete files that are seven days old in a Shell script, you can follow these steps:
- discover
find /path/to/files -type f -mtime +7 > /tmp/files_to_delete.txt
The directory /path/to/files is where you need to search for the files.
- Read temporary files that contain file paths from seven days ago, and delete the files line by line. You can use the following command:
while IFS= read -r file; do
rm "$file"
done < /tmp/files_to_delete.txt
- Finally, remove temporary files.
rm /tmp/files_to_delete.txt
Combine the above commands into a Shell script, save and grant execute permissions, to achieve the functionality of deleting files from seven days ago.