LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-15-2016, 07:58 AM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
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!!!
 
Old 12-15-2016, 08:16 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,086

Rep: Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441
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
 
Old 12-15-2016, 08:16 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,062

Rep: Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672Reputation: 3672
There are any number of ways to do this. What have you tried - and what were the problems you encountered ?
 
Old 12-15-2016, 08:18 AM   #4
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
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
 
Old 12-15-2016, 08:28 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,974
Blog Entries: 3

Rep: Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103
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.

Last edited by Turbocapitalist; 12-15-2016 at 08:35 AM. Reason: grammar
 
1 members found this post helpful.
Old 12-15-2016, 08:31 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,086

Rep: Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441
Why do you have to leave out the extension?

new_fileName=$file_name$current_time.xlsx (or whatever the extension might be)
 
1 members found this post helpful.
Old 12-15-2016, 09:21 AM   #7
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@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

Last edited by trickydba; 12-15-2016 at 09:24 AM.
 
Old 12-15-2016, 09:53 AM   #8
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
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.

Last edited by trickydba; 12-15-2016 at 09:54 AM.
 
Old 12-15-2016, 09:58 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,086

Rep: Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441
Not exactly.
Code:
old_filename=AUDITS_JULY.xlsx
new_filename=$( basename $old_filename .xlsx )$(date "+%Y%m%d").xlsx
 
1 members found this post helpful.
Old 12-15-2016, 10:02 AM   #10
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@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?
 
Old 12-15-2016, 10:07 AM   #11
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Maybe mv TheFile.xlsx TheFile `date +"%d-%m-%Y.xlsx` UGH! No!

Last edited by trickydba; 12-15-2016 at 10:10 AM.
 
Old 12-15-2016, 10:13 AM   #12
michaelk
Moderator
 
Registered: Aug 2002
Posts: 22,086

Rep: Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441Reputation: 4441
Look at the rename example above.
 
Old 12-15-2016, 10:28 AM   #13
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
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!

Last edited by trickydba; 12-15-2016 at 10:59 AM.
 
Old 12-15-2016, 01:59 PM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,974
Blog Entries: 3

Rep: Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103Reputation: 3103
Quote:
Originally Posted by trickydba View Post
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.
 
1 members found this post helpful.
Old 12-15-2016, 02:04 PM   #15
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
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!

Last edited by trickydba; 12-15-2016 at 02:21 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] LInux Script to add date to a filename? your_shadow03 Linux - Newbie 2 10-20-2009 12:36 AM
Add Date (day) to Filename greengrass Programming 1 01-15-2007 07:48 PM
How do I add the current date to a filename? sammysrefuge Linux - Newbie 2 03-18-2005 03:43 PM
Script add date to filename amphion Linux - Newbie 2 06-02-2004 08:12 AM
how to add date to filename x2000koh Programming 1 07-08-2003 10:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration