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
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?
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:
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
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
Summary: hi, could anybody help me understand the basic requirements of FLEXIBILITY IN FILE NAMING SCHEMES FOR SINGLE USER AND MULTI USER ENVIRONMENTS IN UNIX. Please help...
Summary: I sometimes use dates in file naming, but I prefer sequential numbering since that is unique, and I still have the date stamp of the file to show me when it was created. When using dates, consider inc...
Summary: I need a UNIX command to work on OS X on a Mac. I have a client who upgraded from OS 9 to OS X on her mac. She has a huge number of files with "/" and other special characters. I need to remove ...