LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-31-2011, 01:01 PM   #1
devUnix
Member
 
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: 549

Rep: Reputation: 46
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:

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



System Info:

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

Last edited by devUnix; 01-31-2011 at 01:12 PM.
 
Old 01-31-2011, 02:20 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux
Posts: 2,851
Blog Entries: 1

Rep: Reputation: 901Reputation: 901Reputation: 901Reputation: 901Reputation: 901Reputation: 901Reputation: 901Reputation: 901
find . -exec ls -alh '{}' >> tmp.out \;
grep 2008 tmp.out
 
Old 01-31-2011, 02:30 PM   #3
devUnix
Member
 
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: 549

Original Poster
Rep: Reputation: 46
Quote:
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:

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

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

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.
 
  


Reply


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 10:12 PM
How to - list files that were created aftre certain date kaprasanna Linux - Newbie 2 12-10-2007 03:50 AM
Is there a way to find recently created/edited files without using find? BrianK Linux - General 2 10-15-2007 09:41 PM
list all files created by another user christina_rules Linux - General 6 05-23-2006 09:08 PM
how to list all the files in /tmp directory that have been created or changed. y2k6summi Linux - General 1 03-29-2006 10:21 AM


All times are GMT -5. The time now is 12:21 PM.

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