Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This 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.


  Search this Thread
Old 01-31-2011, 02:01 PM   #1
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Rep: Reputation: 48
Find and List Files Created in a Particular Year

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:

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)?

System Info:

SunOS 5.8 Generic_117350-06 sun4u sparc SUNW,Ultra-Enterprise

Last edited by devUnix; 01-31-2011 at 02:12 PM.
Old 01-31-2011, 03:20 PM   #2
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,004

Rep: Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462Reputation: 1462
find . -exec ls -alh '{}' >> tmp.out \;
grep 2008 tmp.out
Old 01-31-2011, 03:30 PM   #3
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 576

Original Poster
Rep: Reputation: 48
Originally Posted by szboardstretcher View Post
find . -exec ls -alh '{}' >> tmp.out \;
grep 2008 tmp.out
On this SunOS 5.8 Production Server there seems to be less options available to the ls command:

bash-2.03$ ls -lh
ls: illegal option -- h
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]

By the way, are those certifications you have done? What is SSC(2)?
Old 02-01-2011, 08:32 AM   #4
Senior Member
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,191

Rep: Reputation: 105Reputation: 105
(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:

find . -mtime -1 | cpio -oa 2>/dev/null | ( cd ${ADIR} && cpio -imd )

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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I list recently created/modified files? khvatov Linux - Newbie 3 03-08-2009 11:12 PM
How to - list files that were created aftre certain date kaprasanna Linux - Newbie 2 12-10-2007 04:50 AM
Is there a way to find recently created/edited files without using find? BrianK Linux - General 2 10-15-2007 10:41 PM
list all files created by another user christina_rules Linux - General 6 05-23-2006 10:08 PM
how to list all the files in /tmp directory that have been created or changed. y2k6summi Linux - General 1 03-29-2006 11:21 AM

All times are GMT -5. The time now is 11:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration