Finding files older than the current date and time and renaming and moving

Hi,

I have a very urgent requirement here. I have to find all files in the specified directory but not in the sub directories(The directory name is stored in a variable) which are older than the current date as well as current time and rename it as filename_yyyymmddhhmmss.ext and move it into a different folder(which is also stored in a variable). I am using solaris 5.8.

Here "yyyymmddhhmmss" refers to the date and time the corresponding file was created.

And when i try to use "stat filename" i get Ksh: not found error

Are you sure you stated the requirement correctly. By definition, any file that exists in a directory has to be older than "now", even if it is still open for writing. But here is a way to find files older then a certain time/date - use

touch -t yyyymmmddhhmmss dummy
find /path/to/files ! -newer dummy -type f
INCORRECT. See newer post below.

What goes in get_filetime can vary. One solution is:

find . -name "$1" -printf "%T@\n"

Normally, yes, Jim. But the directory can also be populated in numerous ways in which the timestamp of the incoming file is changed to match that of the original, for instance. (Why the original files would be dated in the future I can only imagine. Perhaps they are batch files meant to run once their timestamp is valid.)

printf "%T" is ksh93 isn't it?

Whenever i run the code whatever file is in the specified directory should get renamed and moved to the specified folder provided the file created date and time is older than the current date and time.

Looking at your question again, I had it in reverse. My code was moving all files create AFTER the one in question. Does "created" mean also "modified". In UNIX, you don't actually know when a file was "created". You just know when the data was modified and when the meta-file info ("inode") was modified. If you change the file's permissions or ownership, you will change the meta-file info.

My code also renamed the files with the number of seconds since 1970, rather than in the format you wanted. So, improving on my original version:

get_filetime() 
{ 
  find . -name "$1" -printf "%TY%Tm%Td%TH%TM%TS\n"
}

cd $TARGET_DIR
touch __stop__$$
ls -lt | grep '^-' | awk '/__stop__'$$'$/ { start=1 } start,0' |
while read file; do
  filetime=`get_filetime $file`
  mv $file $NEWDIR/$file_$filetime
done
rm -f __stop__$$

It's in pdksh, but you need to provide it some soft of date/time string. As you know, ls prints out partial date/time strings, making it difficult to use in scripts.