In this case I’m asking for anything greater than 31 days. These will be the files that bash script will delete. Outputs the files that the find command finds to the log file. find $folder_path -mtime +31 -name "*.log" > $log_of_files Outputting the systems current date/time and then a line of hyphens into the log file. date > $log_of_files echo "-" > $log_of_files The location of where I want my script to log what it has done. I thought best to hard code this here so that it is not accidentally mistyped at run time. #!/usr/bin/env bash # geektechstuff # script to remove old files folder_path=/home/pi/test/ log_of_files=/home/pi/files_deleted.log date > $log_of_files echo "-" > $log_of_files find $folder_path -mtime +31 -name "*.log" > $log_of_files find $folder_path -mtime +31 -name "*.log" -delete echo "- " > $log_of_filesīreaking the bash script down: #!/usr/bin/env bashĪ she-bang line to tell the operating system what to use to run the script folder_path=/home/pi/test/ The bash script that finds and deletes the files is only a few lines long and I’ve added in a basic logging function so that I know when it ran and what it deleted. The Bash Script Bash script to delete files older than 31 days old Messing up a wildcard, folder path or file name pattern can have major consequences. Warning: I recommend testing (and retesting) scripts like this before using them on a live system. With this in mind I wanted to test the script, run it multiple times and tweak it, so I needed a test set up. In theory it could delete a lot of files, and if run with sudo or root it could delete system files. log files using the Linux touch command: touch. 99 “log files” in a directoryįor this scenario I created the. The scenario could also be that the log file is generated daily. 100MB) it generates a new log file (sequentially numbered). When the log file reaches a certain size (e.g. In this scenario I have a folder ( /home/pi/test/) where one of my programs writes regular log files to. In this blog post I am going to look at a quick bash script that can be used via cron to delete older log files. However, over time they can take up space and become a pain to remove. Log files are a brilliant feature of any piece of software / operating system.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |