LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-20-2016, 10:51 AM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Date not appending to filename


I'm trying to append he current date to a file that is generated everyday. I tried using the DATE command with no success.The filename format is:
ddd_ddddddddddd_monthdayyear.DAT

When I use DATE, it creates 2 files, but the one that does what I want it to do, the file size is 0. Like for example, if the filename is ess_inventoryhub_DAT with a filesize of 43kb, when I use the DATE command it will create 2 files:

ess_inventoryhub_.DAT filesize 43kb
ess_inventoryhub_111816.DAT filesize 0

Any help is highly appreciated!!
 
Old 11-20-2016, 10:58 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You would need to show the commands you are using, otherwise how are we to help?

Please use [code][/code] tags around code and data
 
1 members found this post helpful.
Old 11-20-2016, 01:01 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
what does your code look like?

Last edited by BW-userx; 11-20-2016 at 01:04 PM.
 
1 members found this post helpful.
Old 11-20-2016, 02:51 PM   #4
genogebot
Member
 
Registered: Jun 2010
Location: Brisbane, Australia
Distribution: Xubuntu 16.04.1 / Linux Mint 18 XFCE / Linux Mint 18 Mate / Ubuntu Server 16.04.1 / Lubuntu 16.04.1
Posts: 146

Rep: Reputation: 20
Assuming you are using a shell script, you could do something like:

Code:
f="ess_inventoryhub_$(date +"%m%d%y").DAT"

Last edited by genogebot; 11-20-2016 at 03:14 PM.
 
1 members found this post helpful.
Old 11-20-2016, 09:49 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As above, show us your code.
Also, I'd recommend the format YYYYMMDD as this sorts naturally/correctly/easily.
It also means you can do comparisons easily ie without having to re-format to eg epoch seconds
 
1 members found this post helpful.
Old 11-21-2016, 06:53 AM   #6
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
I appreciate all of the help, I have solved it. Similar to the code above but I had to add some extra lines of code. Appreciate you all!!!!
 
Old 11-21-2016, 07:31 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You should show your solution for future people who find your code, if nothing else than to show an alternative.

Please remember to mark as SOLVED.
 
1 members found this post helpful.
Old 11-21-2016, 07:59 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
$(date -R)
gives a nice output ,, just in case you want to tweek your code

don't forget to mark solved.
 
1 members found this post helpful.
Old 11-21-2016, 09:40 AM   #9
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
The code I used for the solution:


file_name=TESTFILE <------ notice no extension, it was added in the next line
current_time=$(date "+%Y_%m_%d.DAT") <------ added the extension
new_fileName=$file_name$current_time
cp $file_name $new_fileName
rm $file_name <------ since two files was created, had to delete the initial file
 
Old 11-21-2016, 11:03 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Quote:
rm $file_name <------ since two files was created, had to delete the initial file
I have to say, I am unable to reproduce your results, however, you do realise that cp copies your file to its new name and does not replace it? You would need to use mv if you did not want the original file
to still exist.

On a side note, some prefer to use cp and then remove the original after they have confirmed the copy has worked.
 
1 members found this post helpful.
Old 11-21-2016, 11:16 AM   #11
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
And I wouldn't do that with the date variable either. Its confusing.

Code:
file_name=TESTFILE
current_time=$(date "+%Y_%m_%d")
new_fileName=$file_name$current_time.DAT
mv $file_name $new_fileName
Really all you should need.
 
1 members found this post helpful.
  


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] perl- appending date in file name wont work. PoleStar Programming 6 10-19-2011 02:37 PM
[SOLVED] automatically appending date to filenames of photos imported using shotwell or f-spot adityavpratap Linux - General 9 12-30-2010 06:17 AM
Date in a filename. Garp Linux - General 7 12-09-2005 01:57 PM
Appending current date and time to a file frankietomatoes Linux - General 5 11-18-2002 02:09 PM

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

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

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