How to Name a Unix File With a Date Stamp


On Unix and Linux systems, the "date" command can be used to create formatted date and time stamp strings for naming files and directories. Date stamps are useful in scripts and time-based, or "cron," jobs to create unique file names, create date-based directory structures and add useful information about the file. Date-stamped file names are commonly used for the automated backup and archival of files and creating temporary files.

  • Use the "date" command to generate the date string you want, using the format option to format the output. The format string is preceded by a plus sign (+). Format sequences can be combined, and static characters can be interspersed with the interpreted sequences. For example, the command "date +%F" outputs today's date in YYYY-MM-DD format. For November 25, 2009, the command would output "2009-11-25". Common format sequences include:

    %D: date in m/d/y format
    %T: time in HH:MM:SS format
    %M: minute, padded with 0's (00..59)
    %H: hour, 24-hour clock, padded with 0's (01..23)
    %I: hour, 12-hour clock, padded with 0's (01..12)
    %d: day of month, padded with 0's
    %m: month, padded with 0's (01..12)
    %y: last two digits of the year (00..99)
    %Y: year

    View more format sequences and information about the date command by viewing the manual (command "man date").

  • Test the date stamped file name using the "echo" command. Surround the entire date command with backticks, made by pressing the accent grave (), which is on the same key as the tilde (~) on U.S. keyboard layouts. For example, the command: "echo myMonthlyBackup_date +%Y-%m_%B`.bak" outputs: myMonthlyBackup_2009-11_November.bak.

  • Use the date-stamped file name in the command you want to execute. The following example creates a directory structure using the year, month and day in the current working directory (2009/11-November/25 for November 25, 2009):
    "mkdir -p date +%Y/date +%m-%B/date +%d". This example copies process.log to process[datestamp].log.old: "cp process.log processdate +%Y-%m-%d_%H%M.log.old".

Tips & Warnings

  • If you frequently create a date-stamped file name on the command line using the same date formatting sequence, create a script or alias as a shortcut for your command to save a few keystrokes.
  • Make sure to add enough refinement to the date stamp to ensure your file will not be accidentally overwritten. Add the time, as detailed seconds (%S) or nanoseconds (%N), if necessary.

Related Searches


Promoted By Zergnet



You May Also Like

  • How to Create a File in Unix

    There are three ways to create a file in Linux. The method you choose depends on the source of the data for...

  • How to Delete Files in Unix

    The 'rm' command in Unix is a very powerful tool for deleting files. Unlike Windows, it performs its operation silently and assumes...

  • How to Edit a Crontab

    Crontab is an administrator tool usually found within all Linux or UNIX distributions. The cron daemon typically runs in the background and...

  • How to Redirect the Output to a File in Linux

    The Linux operating system includes a powerful command line facility that can also serve as a scripting language for task automation and...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!