Sponsored Content
Full Discussion: Script to change date/time
Top Forums Shell Programming and Scripting Script to change date/time Post 302915390 by Don Cragun on Tuesday 2nd of September 2014 04:55:05 PM
Old 09-02-2014
ShriniShoo and protocol,
The last two digits in the timestamps you're incrementing in your scripts represent minutes. There are 60 minutes per hour; not 100. Most implementations of the touch utility will happily accept a timestamp with a minute specified as 60 through 99 as minute 0 throughout 39 of the next hour. Others may report an error and refuse to use that malformed timestamp.

On systems that do accept 60 through 99, your loops will use the same timestamp for several pairs of files set using the timestamps (201201010560 and 201201010600, 201201010561 and 201201010601, ... through 20120101599 and 2012010639) as synonyms, so if more than 60 files are processed using protocomm's code (30 with ShriniShoo's code), the resulting list will not be sorted alphabetically as requested.

This slightly more complex script should do what was requested as long as the filesystem containing the files being processed supports at least 1 second timestamp resolution, and none of the pathnames being processed contain any <newline> characters:
Code:
#!/bin/ksh
# Initialize timestamp starting point
YYYY=1970	// Year >= 1970
MM=1		// 1 <= Month <= 12
DD=2		// 1 <= Day <= 28
hh=0		// 0 <= hour <= 23
mm=0		// 0 <= minute <= 59
ss=0		// 0 <= second <= 59

# Timestamp incrementing function...
NextSecond() {
	ss=$((ss + 1))
	if [ $ss -ge 60 ]
	then	ss=0
		mm=$((mm + 1))
		if [ $mm -ge 60 ]
		then	mm=0
			hh=$((hh + 1))
			if [ $hh -ge 24 ]
			then	hh=0
				DD=$((DD + 1))
				if [ $DD -ge 29 ]
				then	DD=1
					MM=$((MM + 1))
					if [ $MM -ge 13 ]
					then	MM=1
						YYYY=$((YYYY + 1))
					fi
				fi
			fi
		fi
	fi
}

# Main script...
# Find regular files to be processed and sort them by name...
find /master/path -type f | sort | while IFS= read -r path
do	# For each pathname found, increment the timestamp and touch the file...
	NextSecond
	ts=$(printf '%04d%02d%02d%02d%02d.%02d' $YYYY $MM $DD $hh $mm $ss)
	echo touch -t "$ts" "$path"
done

I used the Korn shell for testing, but this will work with any shell that supports POSIX standard shell arithmetic expansions (such as bash and ksh).

Change the /master/path in the script to the root directory containing the files you want to process. Once you have verified that the commands it prints would do what you want, remove the echo to have it actually run the touch commands.

Note that the NextSecond function only uses 28 days in a month to avoid the complexity of varying numbers of days in a month and leap year calculations. It starts using January 2, 1970 (instead of January 1st) to avoid using negative timestamps when running in timezones east of Greenwich. (Some systems allow negative timestamps for times before the Epoch; some systems use unsigned timestamps to allow a 32-bit time_t to get past the upcoming year 2037 problem.) Assuming you have enough space to sort a long list of pathnames, this script should easily handle more than a billion files without reusing any timestamps.
This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Temporarily change the date / time

Hi - I'd like to know if it's possible to change the time on a Unix box temporarily, do some stuff, and then set it back to the correct time. I'm testing some code that reads a time (in hours) from a config file, and does something at that time. I want to be able to run regression test... (5 Replies)
Discussion started by: samgdavies
5 Replies

2. UNIX for Dummies Questions & Answers

Date& Time change in linux beyond few days back

Hi, Could you please let me know the command to change my date /time beyond few days back. Currently when i am trying the below commands able to go beyond one day at max. -->date -->Mon Sep 29 19:31:58 EST 2008 -->export TZ=TMP40:00:00 (Changing the date beyond 40 hours) -->Sun Sep 28... (4 Replies)
Discussion started by: komala
4 Replies

3. Shell Programming and Scripting

need inputs on how i can change my script to reduce amount of time the script takes

HI , I have a list1 which consists of data that i have to search and a list2 which has the files that need to be searched .So basically i am using list1 on list2 to see if list1 data is present if found replace it .I have written the code using foreach loop for each list .This is taking the... (1 Reply)
Discussion started by: madhul2002
1 Replies

4. OS X (Apple)

how to change date and time format in menu bar

Can someone please tell me how to change the time and date format in the menu bar. I get to System Preferences -- Language and Text, format, customize, but I can't figure out what to do next. I want to change, say from Tue May 4 to 5 4 2010. How the heck do I do this? (5 Replies)
Discussion started by: Straitsfan
5 Replies

5. Shell Programming and Scripting

Date Time Change for a particular target Zone

Hi Summary: - Script will take 2 arguments - Argument 1: Date/Time value in a particular format - Argument 2: TimeZone value Examples: Argument 1: a. "May 11, 2012 08:00:00 AM" b. "Dec 21, 2012 12:21:12 PM" c. "Oct 2, 2012 05:00:00 PM" Argument 2: a. MT or MST or MDT b. ET c.... (4 Replies)
Discussion started by: anuragpgtgerman
4 Replies

6. Shell Programming and Scripting

Script to change name of a file with date

I have a file that contains todays date(for ex- test_08/30/2013) Now I want a script which will run and change the name of the file and put yesterday's date (for ex- test_08/29/2013) Please help. ---------- Post updated at 04:40 AM ---------- Previous update was at 04:31 AM ---------- I... (2 Replies)
Discussion started by: sv0081493
2 Replies

7. Shell Programming and Scripting

Change the date and time format in UNIX script.

Hi, I am extracting a date string from the source file like this : 06/05/2014 16:04:00 I want to change it to 05-JUN-14 04.05.00.000000000 PM I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date"... (8 Replies)
Discussion started by: Varshha
8 Replies

8. Solaris

How to change the date automatically on script?

Hello Experts, There is a log file which has date stamp, I just wanted to change the date automatically on daily basis when it runs. Tried the below, but no luck. grep -i error /var/bv1to1_var/logs/bv03.bectondickinson.com/bvlog.out.`date +\%Y\%m\%d` | tee error_bv03.doc I would highly... (10 Replies)
Discussion started by: seenuvasan1985
10 Replies

9. Shell Programming and Scripting

Change date time stamp of existing file

I have a file hello.txt which was created today (today's date timestamp) I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now. uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v Can you please suggest a easy way to do that ? (12 Replies)
Discussion started by: mohtashims
12 Replies

10. Solaris

Date and time change in global and non global zone

Hi, If I change date and time in global zone, then it will affect in non global zones. During this process what files will get affect in non global zones and which mechanism it's using to change. gloabl zone:Solaris 11.3 X86 TIA (1 Reply)
Discussion started by: Sumanthsv
1 Replies
All times are GMT -4. The time now is 02:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy