How to move files older than 30 days
I am trying to move files older than 30 days without moving files in the lower directories. My file structure is as follows...
Dir1- |Dir2 |Dir3 I want to move all files older than 30days from Dir1 to Dir2. Here is what I have... find /Dir1 -type f -mtime +30 -exec mv {} Dir2 \; ( but this moves all files in other subdirectories.) Thanks in advance. -Gregg |
yeah, you could probably cron a bash script, but Im just getting into bash scripting myself. Dont know about the syntax hehe
|
see man find
find command searches in subdirectories also.
look man find to see whether any aoption to avoid subdirectories. |
You can also skip find in the start of your search
ls -F /dir1 | grep -v / #this will only return regular files not dirs from the dir1 then add | xargs -I {} find ./{} -mtime +30 | xargs -I {} mv {} /dir2 it is not pretty but I think this will work... a Perl script or ksh would be cleaner but I have not done many jobs like this in a long time... the only item that I am not sure on is the “./{}” please check on this… if it does not work like you want you might do a cd /dir1 then use ls ... find . that will make the ./{} easer to debug... -KJ |
hi
i think the (grep -v "^") will help you. for example: ( find / | grep -v "^/tmp" ) the result will be all files in "/" without the files that under "/tmp" Best Regard |
nice i always for get the ^
-KJ |
Thanks. It works without the '^' Is there a way to grep to ignore multipe directories. I am trying to avoid 2.
-Gregg |
Well pattern matching comes to mind. I think you need to research metacharacters
I think you are looking for: find / | grep -v "^/tmp|^/tm2|^/tmp3" but am not sure. There are so many variations, that it is to hard to know exactly. If the sub-dir's can vary you can not word match but will need to string ... bla bla also you can also always find / | grep ^/tmp | grep ^... as many times as you like... It is not as clean but always works if you test it thourally... |
hi,
To exclude more than one directory. for example: { find / | grep -v "^/tmp" | grep -v "^/tmp1" | grep -v "^/tmp2" } it work well. BR |
All times are GMT -5. The time now is 12:06 PM. |