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


 
Thread Tools Search this Thread
Top Forums 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?
# 1  
Old 01-27-2017
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:
Code:
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 /home/xyz/move_data_here/ , i need to keep folders

example hot it should works, so like i move

Code:
/home/xyz/DATA/subdir/subsubdir/files.rar

into
Code:
/home/xyz/move_data_here/subdir/subsubdir/files.rar

THX!


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-27-2017 at 06:23 AM.. Reason: Added CODE tags.
# 2  
Old 01-29-2017
Most find implementations I know of only support integer values for -ctime days creation time.

If you have GNU find you can use -cmin instead and specify 1.5 days in minutes using -cmin 720

When you move a folder all sub-folders will automatically be moved so I used a awk program to skip subfolders already moved.

Code:
DEST=/home/xyz/move_data_here
cd /home/xyz/DATA/
find . -type d -cmin +720 -print | awk '
  {
     l=split($0,D,"/")
     m=D[1]
     for(i=2;i<=l;i++) {
        if(m in done) next
        m=m"/"D[i]
     }
     done[m]
     print m
}' | while read folder
do
   PARENT=${folder%/*}
   [ -d "$DEST/$PARENT" ] || echo mkdir -p "$DEST/$PARENT"
   echo mv "$folder" "$DEST/$folder"
done

Remove echos above (in red) once you are sure it is doing what you require. Backup you files before running for real.

Last edited by bakunin; 01-29-2017 at 07:19 PM.. Reason: typo code->icode
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move multiple files 4rm Source to different target folders based on a series num in the file content

Dear Experts my scenario is as follows... I have one source folder "Source" and 2 target folders "Target_123456" & "Target_789101". I have 2 series of files. 123456 series and 789101 series. Each series has got 3 types of fiels "Debit", "Refund", "Claims". All files are getting... (17 Replies)
Discussion started by: phani333
17 Replies

2. Shell Programming and Scripting

Compress folders older than x days

hello everyone. in /opt/abc every night there is a new folder created. in that folder there is aseries of files created for that day. i would like to run a script every Sunday night at 02:00 to compress each file separately (preserving its name) who is older than 2 days. i have found this... (2 Replies)
Discussion started by: atux
2 Replies

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: prvnrk
3 Replies

4. Shell Programming and Scripting

Script to delete files in a folder older than 2 days

hi i need a script to delete the files older than 2 days... if my input is say in a folder versions A_14122012.txt A_15122012.txt A_16122012.txt A_17122012.txt i want my output to be A_16122012.txt A_17122012.txt thanks in advance hemanth saikumar. (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

5. 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

6. UNIX for Dummies Questions & Answers

List files older that 7 days in a dir, excluding all subdirs

Hi, I would like to list all files, older than 7 days, in a directory, but exclude all subdirectories in the find command. If I use find . -type f -mtime +7 all files in the subdirs are also included. How can I exclude them? Regards, JW (6 Replies)
Discussion started by: jwbijl
6 Replies

7. 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

8. Shell Programming and Scripting

Move all files but not folders to a new folder

Hi, I have a sub directory with a number of files and folders. What i want is a subdirectory with just folders and not files for cleanliness sake. So I want to move the files into the new folder but keep the folders in the same place. Move all files (but not folders) to new folder. I am... (4 Replies)
Discussion started by: Hopper_no1
4 Replies

9. Shell Programming and Scripting

Delete folders older than 30 days

Dear all, i use incremental backup my data with .zip to my hard drive. what i need is i don't want the old .zip file older than 30 days. how to write a shell script automatically remove my external hard disc zip backup folders older than 30 days? Regards, (2 Replies)
Discussion started by: joneggk
2 Replies

10. Shell Programming and Scripting

delete files and folders older than 3 days

find /basedirectory -type f -mtime +3 >> /tmp/tempfile find /basedirectory -type d -mtime +3 >> /tmp/tempfile mailx -s "List of removed files and folders" myemail@domain.com < /tmp/te mpfile rm /tmp/tempfile find /basedirectory -type f -mtime +3 -exec rm {} \; find /basedirectory -type d... (7 Replies)
Discussion started by: melanie_pfefer
7 Replies
Login or Register to Ask a Question