Move file based time stamp

 
Thread Tools Search this Thread
Operating Systems Linux Fedora Move file based time stamp
# 1  
Old 09-24-2010
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 :
Code:
[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.
Code:
-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!!!

Last edited by sunardo; 09-24-2010 at 10:48 AM.. Reason: change "yearmonthdate" to "yearmonthday"
# 2  
Old 09-24-2010
selecting the files to move is easy: e.g. your request for Apr 23:
Code:
ls -lrt .|grep " Apr 23 "|awk '{print $9}'

I let you sort the rest...
# 3  
Old 09-24-2010
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


Code:
[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'
Code:
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
Code:
'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 :
Code:
-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..

Last edited by sunardo; 09-24-2010 at 10:47 AM..
# 4  
Old 09-24-2010
Would not just grouping by month suffice?
what about this case:
Code:
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

# 5  
Old 09-24-2010
sorting files by date

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.
# 6  
Old 09-24-2010
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:
Code:
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?
# 7  
Old 09-25-2010
@All...thanks for your respond...

Here is information about my environment.
Code:
[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,

Code:
[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
Code:
[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.
Code:
[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
Code:
[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
Code:
[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?

Last edited by sunardo; 09-25-2010 at 05:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search Files on a given path based on latest time stamp

find /app/data -name "Availability" - Below is the output now i need to filter based on latest modified timestamp. I know 3 is the latest modified time stamp but i tried different options but only filtering docs and not on headnote..Can any one tell me how to do that.. ... (2 Replies)
Discussion started by: vkiyv05
2 Replies

2. Shell Programming and Scripting

file time stamp

Hi All, I am facing small problem. i want to print file time stamp on which date file has placed in the server. i have given some code but its not giving the year. any help appreciated. regards rajesh. (4 Replies)
Discussion started by: rajesh_pola
4 Replies

3. Programming

How to search a file based on a time stamp backwards 10 seconds

Hi all, I'm after some help with this small issue which i'm struggling to work out a fix for. I have a file that contains records that all have a time stamp for each individual record, i need to search the file for a specific time stamp and then search back 10 seconds to see if the number... (2 Replies)
Discussion started by: sp3arsy
2 Replies

4. UNIX for Dummies Questions & Answers

How to move files based on filetype and time created?

Hi, I'm trying to improve my Unix skills and I'm wondering what is the best way to move some files based on filetype and attributes like time created? For instance, lets suppose I have a directory with many different files in it and I'd like to move all the jpgs that were created between May... (6 Replies)
Discussion started by: LuckyTommy
6 Replies

5. Shell Programming and Scripting

Identify log files based on time stamp,zip and then copy..HELP

Hi All, PFB is a requirement. I am new to shell scripting. So plz help. It would be highly appreciated. 1. choose all the log files based on a particular date (files location is '/test/domain')--i.e,we should choose all the files that are modified on 29th November, neither 28th nor 30th 2.... (3 Replies)
Discussion started by: skdas_niladri
3 Replies

6. Shell Programming and Scripting

Change time stamp of a file

Hi, As i know , we can change the time stamp of a file by touch command, i did change in a file and it is looking as given # ls -l abcd -rw-r--r-- 1 batsoqa sicusers 0 Feb 17 2010 abcd actually i want to see the output like this -rw-r--r-- 1 batsoqa sicusers ... (3 Replies)
Discussion started by: apskaushik
3 Replies

7. UNIX for Dummies Questions & Answers

move files between file systems with privileges, time stamp

Hi I have to move files between file systems but files in new file system must have the same attributes as in old one (privileges, time stamp etc). Which tool is best : - ufsdump / ufsrestore - tar - cpio - pax - dd - mv Or maybe there is sth else, you suggest to use. Thx for help (5 Replies)
Discussion started by: presul
5 Replies

8. UNIX for Dummies Questions & Answers

How to search for files based on the time stamp

Hi All, I know the timestamp of a file. Now i would like to list all the files in the with the same time stamp in the same file. Any help would be appreciated. Thanks. sunny (1 Reply)
Discussion started by: sunny_03
1 Replies

9. UNIX for Dummies Questions & Answers

Need to delete the files based on the time stamp of the file

Hi Everyone, I want to delete some files in a path based on the time stamp of the file that is i want to delete the file once in a month. Can any one help me on this? Thanks in advance (2 Replies)
Discussion started by: samudha
2 Replies

10. Shell Programming and Scripting

how do i put a time stamp in a file name

i want to copy a filea.dat to a file name in the format of filea_yyyymmdd_hhmi.dat using something like DTSTAMP=$(date "+%Y%m%d"), which puts it in format filea_yyyymmdd.dat (5 Replies)
Discussion started by: jhamm
5 Replies
Login or Register to Ask a Question