Moving old files based on pattern

Hi all

I am trying to loop through a directory of files using a given search pattern. some of the files will be duplicated due to the pattern, but of the duplicate files i wanted to move the older files to another location.

Is there any straightforward way of doing this ?

One of ways I thought of is using

ls -ltr IGL* | head - 1 <> ls -ltr IGL* | tail -1

This should give give me only the older files, but im not sure how to put this in a loop

Any help appreciated

Thanks

Welcome sthapa

Firstly, please wrap code and data input/output in CODE tags, like this:-

to produce the following (fixed character width, space respected):-

This is my code

Not only does it make posts far easier to read, but CODE and ICODE sections respect multiple space and have fixed width characters, which is important for easily seeing input/output requirements.

Before looking for a solution, I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks and there are several type of loop depending what the actual need is, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Robin