LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-23-2014, 08:51 PM   #1
AndrewNZ
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Rep: Reputation: Disabled
find and move files


How can I find files on a certain date and move them to a directory

eg
ls -l | grep 'Jun 20' - then move to a directory
I tried doing it with ls and find but have not been sucessful yet
 
Old 06-23-2014, 09:04 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

I guess you want to more that just move files with that particular date stamp. The commands "find" and "mv" are what you probably want here, not ls. See for example:

http://www.commandlinefu.com/command.../sort-by-votes

I think the last example on the first page of the above search results is pretty close to what you want.

Evo2.
 
Old 06-23-2014, 09:11 PM   #3
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,307
Blog Entries: 2

Rep: Reputation: Disabled
The "date" your talking about can take any one of three forms
modified uses -mtime
changed uses -ctime
accessed uses -atime

The difference between mtime and ctime is ctime includes modifying it's attributes as well as it's content, mtime only references contents.

So to find files whose contents were modified two days ago you'd use

#find / {whatever path you want to look down} -mtime 2
 
Old 06-23-2014, 09:19 PM   #4
AndrewNZ
LQ Newbie
 
Registered: Nov 2013
Posts: 16

Original Poster
Rep: Reputation: Disabled
ok thats good but can I do something like the following:

ls -l | grep 'Jun 20' < -- this part works
then copy to /folder/jun/20

I would then run the command again accept replace the 20 with another date etc
 
Old 06-23-2014, 09:29 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

you normally should not try to parse the output of 'ls -l' like that: there are too many things that could go wrong.

Here is a slightly less dangerous approach
Code:
for f in * ; do
  d=$(stat --printf %y  $f | cut -f1 -d' ' | tr '-' '/')
  dd=/path/to/somewhere/$d
  if [ ! -d $dd ] ; then
     mkdir -p $dd
  fi
  mv $f $dd
done
This loops over all the files in the directory and puts them in /path/to/somwhere/YYYY/MM/DD/. Where the Ys, Ms, and Ds correspond to the date. Note that this is untested.

HTH,

Evo2.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find files but move the parent directory instead? Springs Linux - General 4 12-21-2012 12:27 PM
[SOLVED] How to find and move files to a dir azstyx Linux - Newbie 5 09-29-2012 03:54 PM
Find and move files greater than 15GB buee Linux - Newbie 2 01-24-2010 07:01 PM
Find files by their contents than move them in 1 command Setya Linux - Newbie 4 11-20-2008 10:09 AM
find and move most recent files in dir backnine_99 Linux - Software 2 06-14-2005 01:03 PM


All times are GMT -5. The time now is 08:24 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration