Moving files based on file creation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files based on file creation
# 1  
Old 12-22-2012
IBM Moving files based on file creation

Hi,

I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir).

Simply, if file is olderthan one month move it from source1 dir to BKP1 dir.

My file names doesn't have any time stamps in their names, so we have to move based on the file creation date.

System: AIX - Korn shell.
# 2  
Old 12-22-2012
Code:
find /path/to/source1 -type f -mtime +30 -exec mv {} /path/to/BKP1 \;

Try that
# 3  
Old 12-22-2012
BTW if you don't want to move files in sub-directories, then use -prune option:-
Code:
find . -mtime +30 ! -name . -prune -type f -exec mv {} /path/to/BKP1/ \;


Last edited by Yoda; 12-22-2012 at 01:08 PM.. Reason: syntax
# 4  
Old 12-22-2012
IBM

Will these two work, if I have spaces in between the file name ?
Eg:
Code:
2012 cust Info.txt

# 5  
Old 12-22-2012
Not as given

Code:
find /path/to/source1 -type f -mtime +30 -exec mv "{}" /path/to/BKP1 \;

Note the change from {} to "{}"
This User Gave Thanks to jim mcnamara For This Post:
# 6  
Old 12-22-2012
IBM

Quote:
Originally Posted by jim mcnamara
Not as given

Code:
find /path/to/source1 -type f -mtime +30 -exec mv "{}" /path/to/BKP1 \;

Note the change from {} to "{}"
Thanks for your reply, adding one constraint to this. Please don't mind.

I have one text file (say dont_move.txt), where some file names are written into that in a line by line manner. So now I need to perform the above code if the name is not in the file (dont_move.txt).

If a file is olderthan 30 days and that name is entered into dont_move.txt, then we need to skip that.

Eg: dont_move.txt
Code:
cust data.txt
2012 cust Info.Z
2012_abc.dat
2011_abc.txt

Thanks,
# 7  
Old 12-22-2012
Try
Code:
find /path/to/source1 -type f -mtime +30 | grep -vf dont_move.txt| xargs -I{} echo mv -t /path/to/BKP1 \"{}\"

As your file names contain spaces, you will need to fiddle around with the escaped double quotes around the final {}. Remove echo as you see command fit your needs.

Last edited by RudiC; 12-22-2012 at 04:09 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bourne returning files based on creation date

I'm wanting to write a bourne shell script that takes in two command line arguments - a directory and a file. With this I want to return a list of files within the directory that are older (based on creation date) than the given file, and print the number of files that have not been listed (they... (4 Replies)
Discussion started by: britty4
4 Replies

2. Shell Programming and Scripting

Moving old files based on pattern

Hi all I am trying to loop through a directory of files using a given search pattern. some of the files will be duplicated due to the pattern, but of the duplicate files i wanted to move the older files to another location. Is there any straightforward way of doing this ? One of ways I... (1 Reply)
Discussion started by: sthapa
1 Replies

3. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

4. UNIX for Dummies Questions & Answers

Script moving files based on date

Hi, I need a script that moves files based on date to a folder. The folder should be created based on file date. Example is : Date file name ----- -------- Oct 08 07:39 10112012_073952.xls Oct 09 07:39 10112012_073952.xls Oct 10 07:39 ... (6 Replies)
Discussion started by: rockingvj
6 Replies

5. UNIX for Advanced & Expert Users

Moving multiple files based on the pattern

I want to search for a particular file name patterns and move them to a specific folder, is it possible to do it with awk or sed? (1 Reply)
Discussion started by: rudoraj
1 Replies

6. UNIX for Dummies Questions & Answers

moving files based on condition

hi i have to move files and send an email and attached the bad files to inform the developer about that. #!/bin/ksh BASE_DIR=/data/SrcFiles cd $BASE_DIR ## finding the files from work directory which are changed in 1 day find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt ##... (14 Replies)
Discussion started by: awais290
14 Replies

7. Shell Programming and Scripting

copy files based on creation timestamp

Dear friends.. I have the below listing of files under a directory in unix -rw-r--r-- 1 abc abc 263349631 Jun 1 11:18 CDLD_20110603032055.xml -rw-r--r-- 1 abc abc 267918241 Jun 1 11:21 CDLD_20110603032104.xml -rw-r--r-- 1 abc abc 257672513 Jun 3 10:41... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

8. Shell Programming and Scripting

Move files based on year of creation

Hi All, I have a directory which has crores of files since from 2003 till now. I want to move only the 2003 files to another directory. Please help. Thanks (2 Replies)
Discussion started by: IHK
2 Replies

9. UNIX for Dummies Questions & Answers

Moving files based on creation date

Howdy, I'm trying to figure out how to move multiple files based on their creation date. If anyone can enlighten me it would be most appreciated!! Thanks! :D (1 Reply)
Discussion started by: dgoyea
1 Replies
Login or Register to Ask a Question