Move file based time stamp

Hi all,

I've already tired to try to solved this problem. Also search in Internet didn't find anything solution
I have a directory like this :

[fedora@netmanripper juventini]# pwd
/opt/projects/juventini
[fedora@netmanripper juventini]# ls -al | more
total 3627460
drwxr-xr-x   2 app apps 12472320 Sep 24 14:59 .
drwxr-xr-x  11 app apps     4096 Jun 16 04:45 ..
-rw-r--r--   1 app apps     6892 Apr 23 04:20 1.txt
-rw-r--r--   1 app apps     2381 Apr 23 04:20 2.txt
-rw-r--r--   1 app apps    11837 Apr 23 04:20 10.txt
-rw-r--r--   1 app apps    11837 Apr 25 04:20 12719.txt
-rw-r--r--   1 app apps    11837 Apr 25 04:21 wanna.txt
-rw-r--r--   1 app apps    11837 May 23 04:20 1271.txt

I want to move each file into directory /opt/projects/backup/"yearmonthday".

for example, I want to move all file which has time stamp Apr 23.

-rw-r--r--   1 app apps     6892 Apr 23 04:20 1.txt
-rw-r--r--   1 app apps     2381 Apr 23 04:20 2.txt
-rw-r--r--   1 app apps    11837 Apr 23 04:20 10.txt

so, 1.txt,2.txt and 10.txt will move to /opt/projects/20100423.

pliz....help me!!!

selecting the files to move is easy: e.g. your request for Apr 23:

ls -lrt .|grep " Apr 23 "|awk '{print $9}'

I let you sort the rest...

yes vbe. But it just example. .
There are many files with different time stamp. Total 3627460 files in my directory and it all have different time stamp, randomly, without pattern (not just Apr 23), from Apr 23 until now

[fedora@netmanripper juventini]# pwd
/opt/projects/juventini
[fedora@netmanripper juventini]# ls -al | more
total 3627460
drwxr-xr-x   2 app apps 12472320 Sep 24 14:59 .
drwxr-xr-x  11 app apps     4096 Jun 16 04:45 ..
-rw-r--r--   1 app apps     6892 Apr 23 04:20 1.txt
-rw-r--r--   1 app apps     2381 Apr 23 04:20 2.txt
-rw-r--r--   1 app apps    11837 Apr 23 04:20 10.txt
-rw-r--r--   1 app apps    11837 Apr 25 04:20 12719.txt
-rw-r--r--   1 app apps    11837 Apr 25 04:21 wanna.txt
-rw-r--r--   1 app apps    11837 May 23 04:20 1271.txt
.........................................................................

so, I want to make script to move file to /opt/projects/backup/'yearmonthday'

1.txt,2.txt,10.txt to /opt/projects/backup/20100423
12719.txt, wanna.txt to /opt/projects/backup/20100425
1271.txt to /opt/projects/backup/20100523
and etc

In my opinion it can, using

'month' from ls -al | awk '{print $6}
'day' from ls -al | awk '{print $7}

so before move the file, firstly the script will create directory /opt/projects/backup/'yearmonthday' based on files' timestamp. Then move each file into /opt/projects/backup/'yearmonthday'.

Anybody can help me to make this script?

I ve tried to move some file in March with :

-create directory, for example /opt/projects/backup/201003XX 
-find opt/projects/juventini -mtime X -exec mv "{}" /opt/projects/backup/201003XX \;

But as you know it's waste my time and my energy to do that.

Again...I'm expecting your help..

Would not just grouping by month suffice?
what about this case:

drwxrwxr-x   4 root       bin           2048 Jul  4  2007 depot
drwxrwxr-x   3 root       bin           2048 Jun 22 12:47 doc
drwxrwxr-x   3 root       bin             96 Jul  6 11:18 export

Have you looked into the options for the FIND command? It is not completely clear what environment you are workign in, but between date and find you should be able to loop through and move the files on that basis. Shell is actually the difficult way to do this. It would, I believe, be easier in a PERL script.

There is much variation in date-related commands in unix and Linux. Please state what Operating System and version you have and what Shell you use.

Btw. You do not have Total 3627460 files in your directory. That figure is a size not a count.
If you want to count the files:

find /directory_name/ -type f -follow -print | wc -l

Now, because the format of a date stamp changes for older files, do you have any files more than say eleven months old?

This problem is very difficult if you start from the output from "ls -la".
Do you have the "stat" command and know how to get a file timestamp from that command?

@All...thanks for your respond...

Here is information about my environment.

[fedora@netmanripper ~]# cat /etc/issue
CentOS release 4.6 (Final)
Kernel \r on an \m
[fedora@netmanripper ~]# echo $SHELL
/bin/bash

@wpeckham...I just have a few Perl background...
or maybe you can show me the example

@methyl
thanks a lot.
Till now, i've never use stat command...

---------- Post updated at 03:14 AM ---------- Previous update was at 02:29 AM ----------

Til now here is my progress, I tried in another PC,

[sunardo@userdocs latihan]$ ls -ltr
total 40
-rw-rw-r--  1 sunardo sunardo  158 Feb 16  2010 hello.pl
-rw-rw-r--  1 sunardo sunardo  106 Feb 16  2010 while.pl
-rw-rw-r--  1 sunardo sunardo  123 Feb 17  2010 getpwen.pl
-rw-rw-r--  1 sunardo sunardo  196 Feb 17  2010 cekperm.pl
-rw-rw-r--  1 sunardo sunardo  226 Feb 18  2010 pss.pl
-rw-rw-r--  1 sunardo sunardo  385 Mar  1  2010 ceksisop.pl
drwxrwxr-x  5 sunardo sunardo 4096 Mar 11  2010 latpython
-rw-rw-r--  1 sunardo sunardo 4806 Mar 12  2010 save_cf_updtartis_reg_done.jsp
drwxrwxr-x  2 sunardo sunardo 4096 Sep 25 14:01 script
...........................................................................

So, firstly generate the directory based on files timestamp

[sunardo@userdocs latihan]$ find -type f -name '*pl' -printf "mkdir /opt/projects/backup/%TY%Tm%Td\n" | sort | uniq
mkdir /opt/projects/backup/20100216
mkdir /opt/projects/backup/20100217
mkdir /opt/projects/backup/20100218
mkdir /opt/projects/backup/20100301

and dump(redirect) it's output to a file and make it executeable.

[sunardo@userdocs latihan]$ find -type f -name '*pl' -printf "mkdir /opt/projects/backup/%TY%Tm%Td\n" | sort | uniq > move.sh
[sunardo@userdocs latihan]$  chmod +x move.sh

Another simpler way is pipe it to another shell instance

[sunardo@userdocs latihan]$ find -type f -name '*pl' -printf "mkdir /opt/projects/backup/%TY%Tm%Td\n" | sh

The second, move the file into the directory created before

[sunardo@userdocs latihan]$ find -type f -name '*pl' -printf "mv %h/%f /opt/projects/backup/%TY%Tm%Td/%f\n"
mv ./while.pl /opt/projects/backup/20100216/while.pl
mv ./getpwen.pl /opt/projects/backup/20100217/getpwen.pl
mv ./hello.pl /opt/projects/backup/20100216/hello.pl
mv ./ceksisop.pl /opt/projects/backup/20100301/ceksisop.pl
mv ./cekperm.pl /opt/projects/backup/20100217/cekperm.pl
mv ./pss.pl /opt/projects/backup/20100218/pss.pl

[sunardo@userdocs latihan]$ find -type f -name '*pl' -printf "mv %h/%f /opt/projects/backup/%TY%Tm%Td/%f\n" | sh

Plis, CMIIW with my script.

Is there another simplest way?

#!/usr/bin/env ruby  -Ku
require 'fileutils'
back=File.join("/opt","projects","backup")
Dir["*"].each do |file|
  timestamp=File.new(file).mtime
  yr,mth,day=timestamp.year,timestamp.month, timestamp.day
  bak=File.join(back,yr.to_s+mth.to_s+day.to_s)
  Dir.mkdir(bak) if not test(?d,bak)
  FileUtils.cp(file, bak, :verbose=>true) if File.file?(file)
end

Thanks kurumi...
But I' never use Ruby before. In addition, Ruby is not installed on Server...
Overall....thanks for your code...:b: