Append date to filename in linux
Asked Answered
H

7

79

I want add the date next to a filename ("somefile.txt"). For example: somefile_25-11-2009.txt or somefile_25Nov2009.txt or anything to that effect

Maybe a script will do or some command in the terminal window. I'm using Linux(Ubuntu).

The script or command should update the filename to a new date everytime you want to save the file into a specific folder but still keeping the previous files. So there would be files like this in the folder eventually: filename_18Oct2009.txt , filename_9Nov2009.txt , filename_23Nov2009.txt

Hirsutism answered 25/11, 2009 at 9:21 Comment(1)
You may want yyyy-mm-dd instead of dd-mm-yyyy to get lexicographical sort of file names to also sort them chronologically.Twoedged
M
109

Info/Summary

With bash scripting you can enclose commands in back ticks or parantheses. This works great for labling files, the following wil create a file name with the date appended to it.

Methods

Backticks -

$ echo myfilename-"`date +"%d-%m-%Y"`"

$(parantheses) - :

$ echo myfilename-$(date +"%d-%m-%Y")

Example Usage:

echo "Hello World" > "/tmp/hello-$(date +"%d-%m-%Y").txt"

(creates text file '/tmp/hello-28-09-2022.txt' with text inside of it)

Note, in Linux quotes are your friend, best practice to enclose the file name to prevent issues with spaces and such in variables.

Mernamero answered 25/11, 2009 at 9:27 Comment(7)
This appends the date to the filename - in the example in the question the date needs to go between the file name and the extension.Vilberg
This worked for me: echo test > "data-csv-"`date +"%Y-%m-%d"`".txt"Infare
Or: echo test > "data-csv-"`date +"%Y-%m-%d.txt"`Infare
note that you have to remove the double quotes in "` (they are needed to bypass the SOF syntax interpreter to appear but they shall be removed in your commands)Prothallus
It might not answer the full question, but it took me ages to find out to use backticks, so thank you. (As @MehdiLAMRANI says, I use them without the double quotes.)Lunseth
@CJB, nowadays, I'd probably prefer $(date +"%d-%m-%Y").Mernamero
@Lunseth I think you have the better way to do this! You beat me to the punch ,Ascending
V
53

There's two problems here.

1. Get the date as a string

This is pretty easy. Just use the date command with the + option. We can use backticks to capture the value in a variable.

$ DATE=`date +%d-%m-%y` 

You can change the date format by using different % options as detailed on the date man page.

2. Split a file into name and extension.

This is a bit trickier. If we think they'll be only one . in the filename we can use cut with . as the delimiter.

$ NAME=`echo $FILE | cut -d. -f1
$ EXT=`echo $FILE | cut -d. -f2`

However, this won't work with multiple . in the file name. If we're using bash - which you probably are - we can use some bash magic that allows us to match patterns when we do variable expansion:

$ NAME=${FILE%.*}
$ EXT=${FILE#*.} 

Putting them together we get:

$ FILE=somefile.txt             
$ NAME=${FILE%.*}
$ EXT=${FILE#*.} 
$ DATE=`date +%d-%m-%y`         
$ NEWFILE=${NAME}_${DATE}.${EXT}
$ echo $NEWFILE                 
somefile_25-11-09.txt                         

And if we're less worried about readability we do all the work on one line (with a different date format):

$ FILE=somefile.txt  
$ FILE=${FILE%.*}_`date +%d%b%y`.${FILE#*.}
$ echo $FILE                                 
somefile_25Nov09.txt
Vilberg answered 25/11, 2009 at 9:30 Comment(5)
Thanks Dave but How or where do i run this script? Forgive me i'm new with Linux.Hirsutism
Can you give some more details on precisely what you want to do. So you have a file in a directory which you want to date stamp in this way. Is it just one file with the same name each time or could it be a number of files with a number of different name?Vilberg
I'm still not clear on what you want to do. It says the script should update the filename "everytime you want to save the file into a specific folder". What is saving the file into the folder? Where is it coming from?Vilberg
If you want to have the filename changed everytime somebody saves something to a certain directory, then you'll need some filesystem monitor. Have a look at inotify (man inotify); you should be able to build something that way.Cabriolet
Thank you so much. This helpedMarishamariska
S
25
cp somefile somefile_`date +%d%b%Y`
Swatch answered 25/11, 2009 at 9:29 Comment(1)
cp somefile somefile_$(date +%d%b%Y) #preferedStatfarad
K
14

You can add date next to a filename invoking date command in subshell.

date command with required formatting options invoked the braces of $() or between the backticks (`…`) is executed in a subshell and the output is then placed in the original command.

The $(...) is more preferred since in can be nested. So you can use command substitution inside another substitution.

Solutions for requests in questions

$ echo somefile_$(date +%d-%m-%Y).txt
somefile_28-10-2021.txt

$ echo somefile_$(date +%d%b%Y).txt
somefile_28Oct2021.txt

The date command comes with many formatting options that allow you to customize the date output according to the requirement.

  • %D – Display date in the format mm/dd/yy (e.g. : 10/28/21)
  • %Y – Year (e.g. : 2021)
  • %m – Month (e.g. : 10)
  • %B – Month name in the full string format (e.g. : October)
  • %b – Month name in the shortened string format (e.g. : Oct)
  • %d – Day of month (e.g. : 28)
  • %j – Day of year (e.g. : 301)
  • %u – Day of the week (e.g. : 4)
  • %A – Weekday in full string format (e.g. : Thursday)
  • %a – Weekday in shortened format (e.g. : Thu)
Kurzawa answered 28/10, 2021 at 4:8 Comment(0)
P
6

I use this script in bash:

#!/bin/bash

now=$(date +"%b%d-%Y-%H%M%S")
FILE="$1"
name="${FILE%.*}"
ext="${FILE##*.}"

cp -v $FILE $name-$now.$ext

This script copies filename.ext to filename-date.ext, there is another that moves filename.ext to filename-date.ext, you can download them from here. Hope you find them useful!!

Primipara answered 22/9, 2020 at 21:59 Comment(1)
This is exactly what I needed, and I like the GitHub repo with the complete examples!Knute
A
4

I use it in raspberry pi, and the first answer doesn't work for me, maybe because I typed wrong or something? I don't know. So I combined the above answers and came up with this:

now=$(date +'%Y-%m-%d')
geany "OptionalName-${now}.txt"

That if you want to use geany or anything else

enter image description here enter image description here

Argilliferous answered 16/6, 2021 at 3:37 Comment(0)
T
0

a bit more convoluted solution that fully matches your spec

echo `expr $FILENAME : '\(.*\)\.[^.]*'`_`date +%d-%m-%y`.`expr $FILENAME : '.*\.\([^.]*\)'`

where first 'expr' extracts file name without extension, second 'expr' extracts extension

Tarrsus answered 25/11, 2009 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.