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!!! |
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 |
There are any number of ways to do this. What have you tried - and what were the problems you encountered ?
|
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
|
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 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. |
Why do you have to leave out the extension?
new_fileName=$file_name$current_time.xlsx (or whatever the extension might be) |
@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 |
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.
|
Not exactly.
Code:
old_filename=AUDITS_JULY.xlsx |
@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? |
Maybe mv TheFile.xlsx TheFile `date +"%d-%m-%Y.xlsx` UGH! No!
|
Look at the rename example above.
|
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! |
Quote:
This one won't work: Code:
RENAME(1) User Commands RENAME(1) Code:
RENAME(1) Perl Programmers Reference Guide RENAME(1) |
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. |