LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   move files on the basis of date range/name pattern and head count (https://www.linuxquestions.org/questions/linux-newbie-8/move-files-on-the-basis-of-date-range-name-pattern-and-head-count-4175571802/)

sunny singhal 02-09-2016 10:25 AM

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.

grail 02-09-2016 10:37 AM

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?

sunny singhal 02-09-2016 11:18 AM

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

sunny singhal 02-09-2016 12:19 PM

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.

grail 02-09-2016 12:19 PM

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

grail 02-09-2016 12:20 PM

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.

sunny singhal 02-09-2016 01:07 PM

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!!

grail 02-09-2016 02:19 PM

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


All times are GMT -5. The time now is 05:58 AM.