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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding files older than the current date and time and renaming and moving
# 1  
Old 11-20-2008
Data 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  
Old 11-20-2008
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  
Old 11-20-2008
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  
Old 11-20-2008
Quote:
Originally Posted by jim mcnamara
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  
Old 11-20-2008
printf "%T" is ksh93 isn't it?
# 6  
Old 11-20-2008
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  
Old 11-21-2008
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__$$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Subscribers with Date 90 days older than current date

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8) -lt... (1 Reply)
Discussion started by: dia
1 Replies

2. UNIX for Dummies Questions & Answers

Moving and renaming files

I have a directory full of directories, say called A B C D E .... In each of these directories there are files called 1.dsp 2.dsp 3.dsp ..... along with others (with different extensions) I need to go through each of these directories and move the dsp file to another folder, but with the name now... (6 Replies)
Discussion started by: claire.a
6 Replies

3. Shell Programming and Scripting

WHM Crontab and Folder Renaming to current date

Hello, i am trying to rename a folder named 'cpbackup' to current date in our WHM Root via setting up a cronetab entry. but the folder does not get renamed on the scheduled time :confused: */1 * * * * timestamp=$(date +%m%d%Y);/bin/mv /ac_backups/cpbackup /ac_backups/$timestamp (4 Replies)
Discussion started by: kandyjet
4 Replies

4. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

5. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

6. Shell Programming and Scripting

Complex renaming then moving files

I am a biologist and using an program on a computer cluster that generates a lot of data. The program creates a directory named ExperimentX (where X is a number) that contains files "out.pdb" and "log.txt". I would like to create a script that renames the out.pdb file to out_ExperimentX.pdb (or... (1 Reply)
Discussion started by: yaledocker
1 Replies

7. Shell Programming and Scripting

Renaming of multiple files with current date

Hi, I have a fixed 4 files in each different directory. The total 17 directories are there each one having 4 files inside it. I need rename all of them with current date. The files formates will be as below: Folder1: abc_NOR_xyz_ddmmyyyy.txt abc_NOR_ghij_ddmmyyyy.txt Folder2:... (5 Replies)
Discussion started by: rjanardhan83
5 Replies

8. Shell Programming and Scripting

how to check whether the given file is 5 weeks older than current date

HI, I need to check whether the given file is 5 weeks older than current date ?? Can anyone give me the script for this ?? (1 Reply)
Discussion started by: risshanth
1 Replies

9. Shell Programming and Scripting

moving and renaming multiple files

Greetings, I know i can use the mv command to move and rename one file. How can I do this with multiple files? example pic01.bmp to pic0001.bmp how can i perform this function on an entire directory of sequential files and keep them in sequence? Hints, suggestions are most welcome:) ... (1 Reply)
Discussion started by: rocinante
1 Replies

10. UNIX for Dummies Questions & Answers

Renaming files to have date/time in filename

I have a program that will export my data to a single file, but it assigns a file name that is overridden every time I run the program. I need to change the file name to have a sequential number in the filename. How do I rename a file so that the filename contains the system date and time. I want... (5 Replies)
Discussion started by: wayneb
5 Replies
Login or Register to Ask a Question