Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. Your other thread is being marked for closure.
I would guess that find is finding the directory /home/username/temp and moving it.
That's going to move the /home/username/temp directory itself, and that's the very first thing it will do. You have to think very carefully about the consequences of possibly moving directories and then trying to recurse into those directories. You probably need to use "-mindepth 1" to avoid moving the /home/username/temp directory, and also using "-prune" to avoid trying to recurse into any directory you just moved. You'll still have to deal with the problem of moving any directory that already exists under /home/username/OK. Perhaps this(untested):
I think that should work. For an item that is not a directory, the "-prune" will do nothing. For a directory that is successfully moved, the "-prune" will prevent trying to recurse into the now nonexistent source directory. For a directory that already exists at the destination, the mv will fail and the "-prune" will not be executed, thus allowing recursion the consider any files within that directory.
Or perhaps this is totally wrong, and you can let me know.
@asttrogeek -- Sorry about the double post... I got thrown a major curveball with a lot of pressure and was supposed to take care of a critical system in a very short timeframe and I'm still in a panic so when I noticed the general thread I thought that it would be a better place to ask this question.
To answer your question, replacing with 'mv' with 'cp -r' seemed to produce the desired results in my tests...except, of course, that it was a cp -r and the problem is a lack of space at the source.
Quote:
Originally Posted by rknichols
That's going to move the /home/username/temp directory itself, and that's the very first thing it will do. You have to think very carefully about the consequences of possibly moving directories and then trying to recurse into those directories. You probably need to use "-mindepth 1" to avoid moving the /home/username/temp directory, and also using "-prune" to avoid trying to recurse into any directory you just moved. You'll still have to deal with the problem of moving any directory that already exists under /home/username/OK. Perhaps this(untested):
I think that should work. For an item that is not a directory, the "-prune" will do nothing. For a directory that is successfully moved, the "-prune" will prevent trying to recurse into the now nonexistent source directory. For a directory that already exists at the destination, the mv will fail and the "-prune" will not be executed, thus allowing recursion the consider any files within that directory.
Or perhaps this is totally wrong, and you can let me know.
@rknichols, this still left the originals in their source and performed what appeared to be a cp-r result...
Right now I've got to sleep on this. I've missed the deadline and there isn't any use in stressing out any more tonight... I'll be back on it in the morning.
Thanks again for your suggestions. I'll keep my eyes on the thread for more options and maybe in the AM I'll have a better idea of what is going on when I'm not 18 hours into the work day.
@syg00:
I'm afraid I don't see... when checking the man echo I don't see how...
The suggestion is to display the name of the file that was found, rather than moving it somewhere else. It's good practice, generally, to simulate the consequences of your actions before performing those actions. By displaying the file name, you know what will be moved and if necessary modify the find conditions.
Instead of the echo command, you could also use
Code:
ls -ld {}
This way, not only do you see the file name but also the timestamp and permissions of that file.
that is interesting: you wanted "to move files and directories older than 'x' days from source to destination". You wrote a solution, executed and worked. And now you missed a directory just I don't know why? It was older than x days therefore it was moved.
So I think you want to skip that dir. probably -mindepth 1 is your friend.
that is interesting: you wanted "to move files and directories older than 'x' days from source to destination". You wrote a solution, executed and worked. And now you missed a directory just I don't know why? It was older than x days therefore it was moved.
So I think you want to skip that dir. probably -mindepth 1 is your friend.
I think that you are correct. After so many hours of working I was exhausted... There are unique situations in the directory structure which are going to cause additional issues with -mindepth which I won't go into here in too much depth... The files that are being moved are call recordings from our phone system and end up with directories which have identical names (dates) so if I go to that -mindepth I will end up moving a few dozen with the same name... if I go one level up I move everything inside of it including the newer files and one deeper I think I will end up moving the files which meet the date criteria I am defining, all of which are uniquely named, but they will all be in the same location.
Testing now! Wish me luck and thanks, everyone, for your advice. I'll report back.
Thanks again everyone, mindepth -1 helped but due to the complexity of the file structure I've had to use a compound of all of the examples above.
Thanks for your help!
I am trying to move files and directories older than 'x' days from source to destination
Could you state the requirement more clearly? It is possible to have new files in old directories (for example, if the files were created long ago and then later modified). If you move the old directory, all of its contents, including new files, will move with it. Is that really what you need?
Edit: You can also have a new directory containing an old file. You can't move the old file without creating a new directory. As I said, figure out what you actually need, and we can find a way to accomplish it.
Another edit: If this script is to be run again from time to time, carefully consider how you want to handle the case where an even older file or directory of the same name already exists on the destination.
I'm thinking that if your requirement is to move old files into an identical directory structure, you could use find to generate a file list, and then use rsync to move those files. If you don't want to clobber pre-existing files, rsync can rename or relocate them. rsync can remove files from the source after the transfer, but I don't think it will remove directories from the source. If that is part of your requirement, you would have to find another way to do that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.