LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2008, 05:21 AM   #1
ginda
Member
 
Registered: Mar 2004
Distribution: SUSE8.2, 9.2, Knoppix
Posts: 323

Rep: Reputation: 31
Moving files with certain datestamp?


Hi Guys

Is there a way you can move all files in a directory that have a specific date and time stamp?

Thanks in advance
 
Old 03-28-2008, 05:46 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You mean an exact timestamp with date, hour and minute? I cannot figure out why you need this, but one way could be
Code:
#!/bin/bash
time="2008-03-28 07:30"
ls -l --time-style +"%Y-%m-%d %H:%M" /path/to/dir | while read line
do
   filetime=$(echo $line | gawk '{print $6,$7}')
   file=$(echo $line | gawk '{print $NF}')
   test "$filetime" = "$time" && echo $file
done
that is you generate a list of files in the long format using a customized timestamp, then from each line extract the timestamp and the filename and compare the timestamp with a specific time of your choice. Finally you move the file to another location. In the example above I put an echo statement for testing purposes: when you're confident with the results you can substitute it with the mv command. If you want to do this recursively, use the find command as in:
Code:
find /path/to/dir -type f -exec ls -l --time-style +"%Y-%m-%d %H:%M" {} \; | while read line
 
Old 03-28-2008, 05:51 AM   #3
ginda
Member
 
Registered: Mar 2004
Distribution: SUSE8.2, 9.2, Knoppix
Posts: 323

Original Poster
Rep: Reputation: 31
Thats sounds good, is there a way where i could specify a range i.e. file time and date stamps from 2008 march 01 to 2008 march 31

thanks again
 
Old 03-28-2008, 06:36 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Yes. You can specify the two dates and modify the test like this:
Code:
#!/bin/bash
initime="2008-03-01 00:00"
endtime="2008-03-31 23:59"
find /path/to/dir -type f -exec ls -l --time-style +"%Y-%m-%d %H:%M" {} \; | while read line
do
   filetime=$(echo $line | gawk '{print $6,$7}')
   file=$(echo $line | gawk '{print $NF}')
   test "$filetime" \> "$initime" -a "$filetime" \< "$endtime" && echo $file
done
Here I've used the version with the find command because I noticed that the output of ls -l gives an extra line with the total. Furthermore it lists also the directories and would have moved them as well. Anyway, if you don't want to recurse inside directories, simply add the option "-maxdepth 1" to the find command.

Note that the test does a string comparison, but dates in the format YYYYMMDD HHMM are ordered either alphabetically and numerically. So you can safely do comparison in this way. If you had a format like MM/DD/YYYY this would not be valid anymore!

Last edited by colucix; 03-28-2008 at 06:39 AM.
 
Old 03-28-2008, 04:27 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
There are easier ways to do this using the magic of the find command itself. For example,
Code:
touch -t 200803010000 initfile
touch -t 200803312359 finifile

find -type f -newer initfile ! -newer finifile -exec mv '{}' dest ';'

rm initfile finifile
If you are in an interactive shell, you might have to escape the ! sign (i.e., ‘\!’).

Last edited by osor; 03-29-2008 at 12:45 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving files madwac Linux - Newbie 15 08-22-2007 09:28 AM
renaming file to include datestamp - script PirateJack Linux - Newbie 3 03-20-2006 07:30 AM
moving files Baghdad Linux - Newbie 5 01-22-2006 01:39 AM
moving files pantera Programming 2 08-23-2005 05:58 AM
Remove Files by Datestamp seanloftus Linux - General 1 07-28-2003 05:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration