Add date to filename in cron output

Posted on

To generate a file with a filename that has a date embedded as part of the filename through job execution performed by cron as an output is a thing that can be done.  Below are steps which is described to achieve the goal stated before :

1. First of all, the script or the command which is going to be executed by cron as a scheduled job must be chosen first. In this article context, it is the simple command which is chosen only to show the creation of file with part of the filename contains date value. The command is ‘touch’ which is a command used to create an empty file. Below is the example of executing the command :

touch file_name

Description 
touch : It is a command usually available as a default installed command provided in the UNIX-like or Linux operating system distribution to create empty file. 
file_name : It is the argument of touch command which is used to specify the name of the empty file that is aimed to be created.   

For an example :

touch test-file

Below is the output of the above executed command :

user@hostname:~/test-folder$ touch test-file
user@hostname:~/test-folder$

2. After creating the file, it can be checked by executing the following command :

ls -al
user@hostname:~/test-folder$ ls -al
total 44
drwxrwxr-x 2 user user 4096 Sep 19 08:02 .
drwxr-xr-x 115 user user 20480 Sep 18 13:10 ..
-rwxr-xr-x 1 user user 52 Sep 3 21:30 my-bash-script
-rw-rw-r-- 1 user user 0 Sep 19 08:02 test-file
-rw-rw-r-- 1 user user 32 Aug 27 14:38 test-file-1
-rw-rw-r-- 1 user user 32 Aug 27 14:38 test-file-2
-rw-rw-r-- 1 user user 32 Aug 27 14:39 test-file-3
user@hostname:~/test-folder$

3.  As shown above, the new file named test-file has been created. Check to see whether it is empty or not by typing the following command :

user@hostname:~/test-folder$ cat test-file
user@hostname:~/test-folder$

4. Try to implement it in cron job scheduler by adding the following line in the crontab for an example.

30 23 * * * touch /home/user/test-folder/file-$(date +\%d\%m\%Y)

To create a new crontab schedule for running certain task, just type the following command in the bash prompt :

crontab -e

The above command will then display the following output :

user@hostname:~$ crontab -e
no crontab for user - using an empty one

Select an editor. To change later, run 'select-editor'.
1. /bin/ed
2. /bin/nano
3. /usr/bin/mcedit
4. /usr/bin/vim.basic
5. /usr/bin/vim.tiny

Choose 1-5 [2]: 4
crontab: installing new crontab

Depends on the preference of user, pick the most likable editor to create a crontab file.  In this article, I choose number 4 since it is quite familiar and I used on daily basis.  Below is the output when the option number 4 is chosen for editing crontab job schedule :

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts 
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

Add in the last line on the cron schedule job above with the previous line which is representing the job for creating a new file with additional date attribute on it as follows :

30 23 * * * touch /home/user/test-folder/file-$(date +\%d\%m\%Y)

Description : 
The above line will be executed as a job scheduled by cron every day at 23:30 creating a file in the /home/user/test/folder with the name of file-19092016 if it is executed in 19th September 2016. Read cron file entry description for further explanation.
  1. Save the above entry and wait for the cron scheduled job to be executed. Check the content of the file whether it has been created as the name specified with additional date attribute. Check it by typing the ‘ls -al’ command as follows :
user@hostname:~$ cd /home/user/test-folder/
user@hostname:~$ ls -al
drwxr-xr-x 115 user user 20480 Sep 19 15:05 ..
-rw-r--r--   1 user user     0 Sep 19 14:10 file-19092016
-rwxr-xr-x   1 user user    52 Sep  3 21:30 my-bash-script
-rw-rw-r--   1 user user     0 Sep 19 08:02 test-file
-rw-rw-r--   1 user user    32 Agu 27 14:38 test-file-1
-rw-rw-r--   1 user user    32 Agu 27 14:38 test-file-2
-rw-rw-r--   1 user user    32 Agu 27 14:39 test-file-3
user@hostname:/home/user/test-folder#

One thought on “Add date to filename in cron output

Leave a Reply