LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-09-2016, 10:25 AM   #1
sunny singhal
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Rep: Reputation: Disabled
move files on the basis of date range/name pattern and head count


Hi,

I have to transfer files from one folder to another on the basis of date range/name pattern and head count(top 10 files), I want to capture the output as well. I am using below command for the same but its giving me error:



mv -v `find . -type f -newermt "2016-01-19 05:47:00" ! -newermt "2016-2-08 15:49:00" | xargs ls -l | grep 'mansi' | head -10` /tmp >> out.txt

mv: invalid option -- 'r'
Try `mv --help' for more information.

could you please help, the folder on which I am running this command contains only files not directories.
 
Old 02-09-2016, 10:37 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well, based on the error you will need to find out why your sub-expression is returning '-r'.

I would be guessing that using the very erroneous 'ls -l' would be your problem as it is returning the permissions of the files found.

Is there a reason you cannot use the -name option?
 
1 members found this post helpful.
Old 02-09-2016, 11:18 AM   #3
sunny singhal
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi Grail,

Thanks a lot for your reply, the issue is resolved after removing -l switch. But could you please advise on what basis head command pick files, I want to pick top 10 files as per there creation time but it seems its picking in random order.

Thanks
 
Old 02-09-2016, 12:19 PM   #4
sunny singhal
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi,

I am using below command for moving files from one folder to another on the basis of date/time/name/count pattern. My command works fine but only concern is head command picking random files, I want it to pick top 30 files on basis of creation date and time.

mv -v `find . -type f -newermt "2016-02-08 00:00:00" ! -newermt "2016-02-08 16:55:00" | xargs ls | grep 'INB' | head -n 30` /tmp >> out.txt

Can someone please help.
 
Old 02-09-2016, 12:19 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
I believe the issue will be that the find command will return files within the date range, but this in no way means they will be in date order ... hence head is doing the right
thing and returning the top 10 files returned but these may be in any date order.

Based on the complexity of the problem, I am not sure a simple one liner is up to the task. You may need to actually write a script to perform the necessary functionality.

One alternative might be to use find's -printf option and print the name and the modified time (%Tk, check man find for 'k' options) and then sort the data based on the time to give a more correct
order??
 
Old 02-09-2016, 12:20 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
If you are moving files you may also wish to look at something like rsync which has many features which may do the task for you.
 
Old 02-09-2016, 01:07 PM   #7
sunny singhal
LQ Newbie
 
Registered: Jan 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi Grail, Thanks for your reply. my problem is solved with 'ls -cr' command.

mv -v `find . -type f -newermt "2016-02-08 00:00:00" ! -newermt "2016-02-08 16:55:00" | xargs ls -cr | grep 'INB' | head -5` /tmp >> out.txt

Solved!!
 
Old 02-09-2016, 02:19 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
You may wish to check the validity of which files are chosen as your find is looking at the modified time information whereas the ls is looking at the change status time ... maybe they are similar
but it probably doesn't hurt to confirm you are still dealing with the correct files

I would add, $() is a much clearer way of performing substitution than `` and has the advantage of being able to be easily nested should the need arise (not an issue here of course)

Also, whilst we got there with a one liner, some times it can be clearer in the future to use a script which can be commented instead of relying on one's
memory to regurgitate difficult one liners ... just at thought

Last edited by grail; 02-09-2016 at 02:20 PM.
 
  


Reply



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
mv files with specific date/time and count sunny singhal Linux - Newbie 1 01-16-2016 09:09 AM
[SOLVED] find files in date range clcbluemont Linux - Newbie 2 05-31-2010 11:40 AM
Count files in dir then move that many files in a script? chuckycharms Linux - Newbie 3 12-18-2009 11:31 AM
Zipping files by a date range nixusr Linux - Newbie 3 06-10-2008 12:14 PM
deleting by date a specific range of files nooks12 Linux - Newbie 2 01-08-2008 02:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:06 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
Open Source Consulting | Domain Registration