Category Archives: Shell Script

Shell – Redirect output to file with datetime as filename

This is an example shell script which creates a file with datetime as filename.
eureka.sh

#!/bin/bash

echo "Eureka!" > eureka.$(date '+%Y-%m-%d').txt

 

The file is created.
shell-script-datetime-as-filename
 

Done =)

Reference: StackOverflow – linux redirector to log filename with $date variable

About these ads

Shell Script – Delete Old Files

For housekeeping the backup files on a server, we can write a shell script to remove files which were created some days ago and scheduled it by cron job.

The following code is an example to remove backup files which were created 90 days ago. Please note that the target folder is just the same as the .sh file location and the .sh file is excluded in the command.
Continue reading