Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then 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.
Ho to find and list files and directories present the current directory which were created in, say, years 2005, 2006, and 2009 and then move them to some other location, for example, /backup.
Yes, I need to list them and move simultaneously.
We can use:
Code:
find . -mtime n {};
but that n is troublesome for me to figure out files/directories created in years 2005, 2006, and 2009, for instance.
Is there any way to match exactly by Year Value rather than calulating the "n" (days * 24 Hours)?
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
(Bunch of thinking out loud here).
So, if it is a file, you expect to move it; and, if it is a directory, you expect to move it and its contents?
I would use "ld" as the options on the ls for Solaris. "h" is an option that gives "human readable" file sizes (using K, M, G as appropriate). I don't have any Solaris 8 systems any more to look at, but that option shows up in 9 and 10. If there are files with the create date you are asking for, the find will turn them up, so the "a" and the lack of "d" on the ls is just going to create a lot more noise in your output.
find . -exec ls -ld {} \; | grep 2008
will give you a listing. If it produces what you want, then call it back and do it again with "> tmp.out" at the end. Then edit tmp.out with your favorite editor. If you replace '^.* ' with 'mv ', it should eat everything up to the last space (before the file path/name) on each line and leave you with "mv path/name". Then replace '$' with ' path/to/2008/directory/', whatever that might be. Now, every line is a mv command grabbing one of those things and moving it to the directory you want. Now you can `chmod +x tmp.out` and source the file to execute it.
There are a couple of hangups with the result. You may get errors trying to move files that already got moved because their containing directory was moved. You also will lose some directory structure, because the move will grab things out of context and just move them over.
I have a local disk to disk backup script that uses the following line that has been in operation for years and doesn't seem to have any issues:
If you created the file of file/directory paths as with the first find command above, you could then cat that list of paths and pipe it to cpio along the lines of this second command.
The added complications are because the `grep 2008` requires a full ls, adding clutter that has to be removed. If you instead played with the mtime in find and came up with the proper combination of greater than ... but less than ... corresponding to 2008, you could do it cleanly as in the last find command above piping to cpio. Do a proof of the find by first doing it with an `-exec ls -ld {} \;`. When you are happy with it, remove the exec and pipe it to cpio.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.