get current system date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers get current system date
# 1  
Old 10-24-2005
get current system date

How do I create a shell script that would get the current system date in format of yyyymmdd and use that result to concatenate it to a filename that it would look like this: sample20051024.dat

Code:
example
--------
current system date: 24-Oct-2005
filename: sample.dat

when the shell script run it now becomes sample20051024.dat

Thanks.
# 2  
Old 10-24-2005
Did you check the FAQ section? I believe this has been answered there.
https://www.unix.com/answers-to-frequently-asked-questions/
# 3  
Old 10-24-2005
Assuming that you are accepting the system date and the filename from the user, you can use this:
Code:
#!/bin/ksh

oldIFS=$IFS
IFS=$IFS.-
echo "Enter system date: \c"
read year month day
echo "Enter filename: \c"
read fname ext

echo $fname$year$month$day.$ext

IFS=$oldIFS

# 4  
Old 06-17-2009
date-ification script

#!/bin/sh -f
if [ "${1}" = "" ]; then
echo " Usage: $0 <filename>"
exit 1
elif [ ! -e "${1}" ]; then
echo "File ${1} not found"
exit 2
fi

year=`date +%Y`
mon=`date +%m`
mon3=`date +%b`
day=`date +%d`
base=`echo ${1} | cut -d'.' -f1`
suf=`echo ${1} | cut -d'.' -f2`
newfile=${base}${year}${mon}${day}.${suf}

echo "current system date: ${day}-${mon3}-${year}"
echo "filename: ${1}"
echo "new filename: ${newfile}"
mv ${1} ${newfile}
# 5  
Old 06-18-2009
Quote:
Originally Posted by cdeanwhitaker
year=`date +%Y`
mon=`date +%m`
mon3=`date +%b`
day=`date +%d`
base=`echo ${1} | cut -d'.' -f1`
suf=`echo ${1} | cut -d'.' -f2`
newfile=${base}${year}${mon}${day}.${suf}

echo "current system date: ${day}-${mon3}-${year}"
echo "filename: ${1}"
echo "new filename: ${newfile}"
mv ${1} ${newfile}
Hi!

Just one tiny comment: you can use one single date command in order to form the filename:
Code:
date "${base}%Y%m%d.${suf}"

Also, echo command does not require sitation marks.

Regards,

pen
# 6  
Old 07-30-2009
thank you all.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

[Solved] How to tar data along with current system date and time.?

Hi all, Following is my small script:- #!/bin/ksh for i in `cat /users/jack/mainfile-dr.txt` do sudo cp -r $i /users/jack/DR01/. done cd /users/jack/DR01/ sudo tar cvf system1-DR.tar * scp system1-DR.tar backupserver:/DRFiles/system1 sudo rm -rf system1-DR.tar In this script I... (10 Replies)
Discussion started by: manalisharmabe
10 Replies

4. UNIX for Dummies Questions & Answers

Comparing Output Date to Current System Date

Hi Guys, Anyone who knows how to compare the current date with the a file containing a date, say for example I have a file that looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server6 Fri Dec 16... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

5. Shell Programming and Scripting

csv file field needs to be changed current system date with awk

HI, I have csv file with records as shown below. 4102,Bangalore,G10,21,08/17/2011 09:28:33:188,99,08/17/2011 09:27:33:881,08/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

6. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

7. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

8. Shell Programming and Scripting

how to get what date was 28 days ago of the current system date IN UNIX

Hi, Anybody knows how to get what date was 28 days ago of the current system date through UNIX script. Ex : - If today is 28th Mar 2010 then I have to delete the files which arrived on 1st Mar 2010, (15 Replies)
Discussion started by: kandi.reddy
15 Replies

9. UNIX for Dummies Questions & Answers

Current system date in terms of seconds

Hello Friends, I've been struggling with extreme nagios passive service checks. In order to trigger a nagios passive service im going to write an easy shell script like below and will run it in crontab. As im working on Solaris 10 servers i used "S" instead of lowercase "s" below #!/bin/sh... (2 Replies)
Discussion started by: EAGL€
2 Replies

10. UNIX for Dummies Questions & Answers

I want to get day from the current system date

Hi, I want to check what day is today (like mon,Tue,wed) When i checked the syntax, i dont see there is a format specifier for getting the day. Let me know how to get the same. I am very new to unix and so I am asking some basic questions. cheers, gops (2 Replies)
Discussion started by: gopskrish
2 Replies
Login or Register to Ask a Question