The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get date and time for past 1 hour from current date spch2o Shell Programming and Scripting 5 08-29-2008 04:32 AM
how to check whether the given file is 5 weeks older than current date risshanth Shell Programming and Scripting 1 10-29-2007 04:53 AM
Moving multiple files and renaming them on the fly daemongk Shell Programming and Scripting 1 06-08-2007 01:36 PM
moving and renaming multiple files rocinante Shell Programming and Scripting 1 06-07-2007 08:20 PM
Renaming files to have date/time in filename wayneb UNIX for Dummies Questions & Answers 5 01-19-2005 10:49 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
ragavhere ragavhere is offline
Registered User
  
 

Join Date: Apr 2008
Location: Chennai,India
Posts: 79
Unhappy 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

Last edited by ragavhere; 11-20-2008 at 11:03 AM..
  #2 (permalink)  
Old 11-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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
Code:
touch -t yyyymmmddhhmmss dummy
find /path/to/files ! -newer dummy -type f
  #3 (permalink)  
Old 11-20-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Code:
INCORRECT. See newer post below.
What goes in get_filetime can vary. One solution is:
Code:
find . -name "$1" -printf "%T@\n"

Last edited by otheus; 11-21-2008 at 05:26 AM.. Reason: incorrect code
  #4 (permalink)  
Old 11-20-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Quote:
Originally Posted by jim mcnamara View Post
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.
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.)
  #5 (permalink)  
Old 11-20-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
printf "%T" is ksh93 isn't it?
  #6 (permalink)  
Old 11-20-2008
ragavhere ragavhere is offline
Registered User
  
 

Join Date: Apr 2008
Location: Chennai,India
Posts: 79
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.

Last edited by ragavhere; 11-20-2008 at 01:12 PM..
  #7 (permalink)  
Old 11-21-2008
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
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:
Code:
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__$$
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 03:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0