File Type: sh
Generated Description:
This bash script, date-range-clean.sh
, cleans up files and directories within the current directory that are older than a specified reference date. It takes a reference date as a command-line argument, converts it to Unix epoch time, and then uses find
and awk
to identify and delete files and directories accordingly. It prioritizes removing files before directories. Hidden files and the current directory itself are excluded from deletion.
-
usage()
function: This function displays the correct usage of the script if incorrect arguments are provided, including an example. It then exits with an error code. -
Argument Check: The script verifies that exactly one command-line argument (the reference date) is supplied.
-
Date Conversion: The script uses
date -d "$REFERENCE_DATE" +%s
to convert the user-supplied reference date string into its Unix epoch timestamp (seconds since January 1, 1970). Error handling is included to catch invalid date formats. -
find
command: The script uses twofind
commands: one to locate files and another to locate directories. Both commands are configured to:- Search only at the current directory level (
-maxdepth 1
). - Exclude hidden files and directories (
-not -path "*/\.*"
). - Print the last modification time in epoch format (
%T@
) and the file/directory path (%p
).
- Search only at the current directory level (
-
awk
command: Theawk
commands process the output offind
. They filter entries older than the reference epoch time (ref
) and extract the filename/dirname by removing the leading timestamp. -
File and Directory Removal: The script uses
rm -f
(force remove files) andrm -rf
(force remove directories recursively) to delete the identified files and directories. The script prints the name of each item before deleting it for user confirmation.
-
Error Handling: The script includes error handling for invalid date formats and incorrect number of arguments.
-
Epoch Time Conversion: Using epoch time provides a numerically comparable representation of dates, making comparison and filtering straightforward.
-
Piping and Filtering: The script effectively uses piping (
|
) to chain commands and filter results, enhancing efficiency. -
Separate File and Directory Handling: Files are deleted before directories; this is a good practice to avoid potential issues that could arise from deleting directories that contain files that are later scheduled for deletion.
-
Explicit
echo
statements: Before each removal action, the script provides a user confirmation by echoing the file or directory name being removed.
- Log file cleanup: Automatically remove old log files to save disk space.
- Temporary file cleanup: Delete temporary files and folders older than a certain time.
- Backup cleanup: Remove old backups that are no longer needed.
- Data archival: Move older data to an archive location after keeping it in the current location for a defined period.
- Automated system maintenance: Integrate this into a cron job to perform regular cleanup tasks.
This script provides a solid foundation for automated file and directory cleanup based on age. However, for production environments, adding more robust error checking (e.g., checking disk space before deletion) and logging capabilities would be beneficial. Also, consider adding a "dry-run" option that shows what would be deleted without actually performing the deletions.
Description generated on 4/4/2025, 3:24:57 AM