Script to change date/time

Hi everybody!
I need to perform a task with a script but I have no idea how to do it, I hope someone could help me:

  • on my linux pc I have many folders with movies, tv shows, toons, ecc.
    They are shared by a dlna server to my panasonic tv where I can browse and see them.
    The problem is that they are sorted with date/time only and there's no way on the TV or on the dlna server to change the sort method to alphabetical.
    What I need is a script that changes date/time (no matter to when, but every file should be 1 min or 1 hour or one day more than the previous) according to the alphabetical order, within each subdirectory of a master path.
    Can you please help me?

Thanks in advance and best regards!

Alex

Sorted by date/time - creation, last change, ??

One thought, something like this:

create a list of files in alpha order and save to a temp file
while not <eof>
   do
   read $filename
   touch $filename
   sleep 10s
enddo < tempfile

Below code will change the time difference between the next files by 1 min

tm=201204261930
for i in *
do
tm=4((tm+1))

touch -t "${tm}" "${i}"
done

thanks
how can I loop it within each subfolder?

They'll be like this:
Movies/
Movies/Kubrick
Movies/Argento
Series/
Series/Friends
Series/X-Files
etc...

What have you tried?
What do you think would work?

try this:

 time=201201010500;for file in $(find pathOfYourFolder -type f -name "*" | sort);do time=$(($time+1));echo "$file";touch -t "${time}" "${file}";done

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:

#!/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.

1 Like