How to move the files older than x days with similar directory structure?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to move the files older than x days with similar directory structure?
# 1  
Old 09-18-2014
How to move the files older than x days with similar directory structure?

Hello,

I need to move all the files inside /XYZ (has multi-depth sub directories) that are older than 14 days to/ABC directory but with retaining the SAME directory structure.

for example:
/XYZ/1/2/3/A/b.txt should be moved as /ABC/1/2/3/A/b.txt

I know about find /XYZ -type f -mtime +14 -exec mv {} /ABC \; but it would NOT retain the directory structure.

I thought of backing up whole /XYZ to /ABC and then delete recent 14 days' files from /ABC and all files older than 14 days from /XYZ but I find it very inefficient.

Appreciate any help. Thanks!
# 2  
Old 09-18-2014
Code:
for file1 in `find /XYZ -type f -mtime +14`
do
  file2=${file1/XYZ/ABC} # You may use your own filters to formulate file2/dir2
  dir2=${file2%/*}
  mkdir -p $dir2
  cp $file1 $file2
done


Last edited by balajesuri; 09-18-2014 at 01:02 PM..
# 3  
Old 09-18-2014
Hmm. try this -

Code:
cd /XYZ
mkdir /ABC
find . -type f -mtime +14 |
while read fname 
do
   tar cf - $fname
done | (cd /ABC && tar xf -)

These 2 Users Gave Thanks to jim mcnamara For This Post:
# 4  
Old 09-18-2014
Thanks Jim.

It worked but I had to modify a bit like pushing done to the end.

The worked one:

Code:
cd /XYZ
mkdir -p /ABC
find . -type f -mtime +14 |
while read fname 
do
   tar cf - "${fname}" | (cd /ABC && tar xf -)
done

Thanks again Jim Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

Finding files older than x days within directory with spaces

Hi, I am trying to run a command that finds all files over x amount of days, issue is one of the directories has spaces within it. find /files/target directory/*/* -type f -mtime +60 When running the above the usual error message is thrown back + find '/files/target\' 'directory/*/*' -type... (1 Reply)
Discussion started by: Ads89
1 Replies

2. Shell Programming and Scripting

How can i move folders and its content if folder is older than 1,5 days and keep subdirs in bash?

Hello all, do you know any way i can i move folders and its content if folder is older than 1,5 days in bash? I tried: find /home/xyz/DATA/* -type d -ctime +1.5 -exec mv "{}" /home/xyz/move_data_here/ \;All i got was that Files from DATA /home/xyz/DATA/* ended messed up in... (1 Reply)
Discussion started by: ZerO13
1 Replies

3. UNIX for Advanced & Expert Users

HPUX move files older than 30 days

Hello, I have a script which finds files in a directory that are older than 30 days and moves them to the specified directory. The problem is I don't know why it works the way it does? Code: find . -name '*.sql' ! -mtime -30 -exec mv '{}' /dataload/archivelogs \; I was under the... (4 Replies)
Discussion started by: pure_jax
4 Replies

4. AIX

Want to delete directory, subdirectories and all files which are older than 7 days

how do i remove sub directories of a directory and all files which are older than 7 days by a single command in AIX. pls help me. I am using command as #find /gpfs1/home/vinod/hpc/ -depth -type d -mtime +7 -exec rm -rf {} \; so i want to delete all sub directories and all files from the... (1 Reply)
Discussion started by: vinodkmpal
1 Replies

5. Shell Programming and Scripting

Delete files older than 10 Days in a directory

Hi All I want to remove the files with name like data*.csv from the directory older than 10 days. If there is no files exists to remove older than 10 days, It should not do anything. Thanks Jo (9 Replies)
Discussion started by: rajeshjohney
9 Replies

6. UNIX for Dummies Questions & Answers

move files older than 2 days to another folder

Hi I am facing problem in using this command, mv `find /export/june/PURGEDATA*.txt -mtime +2 -exec ls {} \;` june/archive/ mv: Insufficient arguments (1) Usage: mv f1 f2 mv f1 ... fn d1 mv d1 d2 Thank you in advance (2 Replies)
Discussion started by: vishwakar
2 Replies

7. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

8. Shell Programming and Scripting

List directory 7 days older

Say folder archive/ contains many folder each created on a day. this folder may contain files. i want to write a script to delete all the folder inside archive/ which are 7 days older. i used the below script for the reason. find archive -mtime +7 -type d -exec rm -r {} \; pls suggest me if... (3 Replies)
Discussion started by: krishnarao
3 Replies

9. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies
Login or Register to Ask a Question