Ubuntu: How to create a file or folder using today’s date

This is a useful little trick to use in your scripts – particularly for things like periodic backups.

 

For a file:

 

touch $(date +%F)

 

creates the file 2012-11-18

 

For a folder, let’s add the time after the date:

 

mkdir $(date +%F-%H:%M)

 

creates the folder 2012-11-18-09:00

 

We can use this in a command, like so:

 

tar -cf $(date +%F).tar /path/to/files/

 

This creates the tarball (archive) file 2012-11-18.tar from the files in the /path/to/files/.

 

To see the other options, type man date or visit the Ubuntu webpage on date here.


Posted

in

,

by