LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add current date to filename (https://www.linuxquestions.org/questions/linux-newbie-8/add-current-date-to-filename-4175595477/)

trickydba 12-15-2016 06:58 AM

Add current date to filename
 
Let's say I have a file named AUDITS_JULY.xlsx, and I want to append the current date to it so it will show AUDITS_JULY_12152016.xlsx. How is this possible.

I know how to append the current time BUT I have to leave out the extension. This is my code that I've used before in another project, but this is a different project and the filename will already have an extension:

file_name=DOD_CUSTOMER_
current_time=$(date "+%Y%m%d.DAT")
new_fileName=$file_name$current_time
cp $file_name $new_fileName
rm $file_name

All help is highly appreciated!!!

michaelk 12-15-2016 07:16 AM

My preference would be to append the date to the beginning but it depends on your circumstances.

There are many ways to perform string functions in bash. The easiest to understand might be the basename function which will extract the filename from the extension.

basename $filename .xlsx

syg00 12-15-2016 07:16 AM

There are any number of ways to do this. What have you tried - and what were the problems you encountered ?

trickydba 12-15-2016 07:18 AM

The only way I know how to do it is the code I have listed above, I know no other way and it would take too much to create the file without the extension

Turbocapitalist 12-15-2016 07:28 AM

If you have the perl-based version of rename then you can try something like this:

Code:

rename -n "s/^([[:alpha:]_]*)\./\$1\_$(date +'%d%m%Y')./" AUDITS_*.xlsx
With all the quoting it is a little hard to read if you are not familiar with the pieces as it mixes shell with perl.
Because it is wrapped in double quotes, everything is processed by the shell first. That means the $1 for perl has to be escaped as \$1 and that the command substitution $( ... ) runs date and uses the output. That is passed to perl which then does a substitution s/// on the file name(s) given to rename

I too have reservations about how the date is represented. If you put the date at the beginning of the name, it will sort better, especially if you use +"%Y%m%d" (aka +"%F") as the format.

michaelk 12-15-2016 07:31 AM

Why do you have to leave out the extension?

new_fileName=$file_name$current_time.xlsx (or whatever the extension might be)

trickydba 12-15-2016 08:21 AM

@michaelk................So all I have to do is plug your one line of code in? No variables have to be defined? I was saying I had to leave out the extension of the file for another project I was doing but this time the extension will be left in. So when t his file is renamed, it will not show as 'filename.xlsx.xlsx using your suggested code? Cause I tried this without any results;

new_fileName=$TEST_FILE.xlsx$current_time.xlsx

trickydba 12-15-2016 08:53 AM

I figured it would be easier to not append the date, just send the file(s) with no date, move them first then after they are in the folder I want them to be THEN append the date to all files ending with .xlsx.

michaelk 12-15-2016 08:58 AM

Not exactly.
Code:

old_filename=AUDITS_JULY.xlsx
new_filename=$( basename $old_filename .xlsx )$(date "+%Y%m%d").xlsx


trickydba 12-15-2016 09:02 AM

@michaelk..............It would be better to just append the current date to all files with an extension of ".xlsx" in this format: AUDITS_JULY_MM_DD_YYYY.xlsx.

How is this possible?

trickydba 12-15-2016 09:07 AM

Maybe mv TheFile.xlsx TheFile `date +"%d-%m-%Y.xlsx` UGH! No!

michaelk 12-15-2016 09:13 AM

Look at the rename example above.

trickydba 12-15-2016 09:28 AM

Tried that with no results:

rename -n "s/^([[:alpha:]_]*)\./\$1\_$(date +'%d%m%Y')./" *.xlsx

I hate to specify exact names. I mean it will not be a problem to do that now BUT in the future many more files will be generated!

Turbocapitalist 12-15-2016 12:59 PM

Quote:

Originally Posted by trickydba (Post 5642425)
Tried that with no results:

Which rename do you have? You can tell by looking at the manual page with man rename

This one won't work:

Code:

RENAME(1)                        User Commands                      RENAME(1)

NAME
      rename - rename files
...

But only if your manual page shows this:

Code:

RENAME(1)              Perl Programmers Reference Guide              RENAME(1)

NAME
      rename - renames multiple files
...

then it will work.

trickydba 12-15-2016 01:04 PM

I'm actually appending dates to specific files BEFORE moving:

file1=AUDITS.xlsx; cfile1=AUDITS; current_time=$(date "+_%Y%m%d.xlsx"); newfile1=$cfile1$current_time; cp $file1 $newfile1

Yes, it's clunky, yes it's sloppy and redundant but it gets the job done till I figure out better.
Because a file looks like this after the code is ran(AUDITS_20161215.xlsx, now I need to figure out how to send files with appended dates, which of course as you know change daily. I'll open a new thread for this!


All times are GMT -5. The time now is 07:07 PM.