Computing.Net > Forums > Unix > date in file name

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

date in file name

Reply to Message Icon

Name: pat smith
Date: November 3, 2003 at 14:00:02 Pacific
OS: bourne shell
CPU/Ram: alot
Comment:

I am trying to add to the end of a script a copy of files that I just created and I want to use the nameing convention of
xref."YYYYMM".data.s in other words part of it would be hard coded and in the middle the current year then month.
Can anyone help me?



Sponsored Link
Ads by Google

Response Number 1
Name: Anagram
Date: November 3, 2003 at 14:13:49 Pacific
Reply:

I assume that you do not want the double-quotes to appear in the final form. If that is so, the following commandline should give you what you want. The magic is the "back-tick." Anything appearing between them is executed in a subshell (in this case the date command) and the stdout is returned to the very point of the commandline in which the back-tick quoting occurred.

cp sourcefilename xref.`date '+%Y%m'`.data.s



0

Response Number 2
Name: pat smith
Date: November 3, 2003 at 14:23:08 Pacific
Reply:

Excuse my ignorance but where do I get a back-tic from?


0

Response Number 3
Name: pat smith
Date: November 3, 2003 at 14:37:38 Pacific
Reply:

Never mind I got it


0

Response Number 4
Name: WilliamRobertson
Date: November 3, 2003 at 15:43:56 Pacific
Reply:

You can also use the $( ) syntax in ksh and bash. Advantages are that it can be easier to find the matching bracket in editors such as vi/vim, and it nests better, e.g:

grep -il PHONE $(grep -il CUSTOMER $(grep -il INSERT *.sql))


0

Response Number 5
Name: pat smith
Date: November 4, 2003 at 07:56:57 Pacific
Reply:

I got the Back-tic now is there any way I can use the first date statement and do a -1 to use the previous month ie
xref.`date '+%Y%m'`.zip5.s will return

xref.200311.zip5.s
is there an easy was to get

xref.200310.zip5.s
and will it work in the turn of a year?


0

Related Posts

See More



Response Number 6
Name: WilliamRobertson
Date: November 4, 2003 at 10:50:32 Pacific
Reply:

Assuming you are using ksh or bash, you can define a function within the script and then use it like a normal command:

function last_month
{
integer mm=$(date +%m)
integer yyyy=$(date +%Y)

(( mm -= 1 ))

if (( mm == 0 ))
then
(( mm = 12 ))
(( yyyy -= 1 ))
fi

print ${yyyy}${mm}
}

oldfilename=$(last_month).zip


0

Response Number 7
Name: pat smith
Date: November 4, 2003 at 11:00:54 Pacific
Reply:

Thank you


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: date in file name

flexibility in file naming schemes?? www.computing.net/answers/unix/flexibility-in-file-naming-schemes/570.html

date-renaming files www.computing.net/answers/unix/daterenaming-files/2549.html

Replace characters in file names www.computing.net/answers/unix/replace-characters-in-file-names/6079.html