in reply to Re: Re: Re: Re: Unix - Keep the last modified file in the directory and move/copy the rest of the files.
in thread Unix - Keep the last modified file in the directory and move/copy the rest of the files.

What if I want to keep the last 2 or 3 or n number of modified file in the directory and move/copy the rest of the files.

well, then i would do something completely different...

$ ls -t whacl-* whacl-20030608223002.gz whacl-20030607223001.gz whacl-20030606223002.gz whacl-20030605223003.gz $ ls -t whacl-* | sed "1,1d" whacl-20030607223001.gz whacl-20030606223002.gz whacl-20030605223003.gz $ ls -t whacl-* | sed "1,2d" whacl-20030606223002.gz whacl-20030605223003.gz $ ls -t whacl-* | sed "1,3d" whacl-20030605223003.gz $ ls -t whacl-* | sed "1,2d" | while read file; do echo 'cp' $file /de +v/null; done cp whacl-20030606223002.gz /dev/null cp whacl-20030605223003.gz /dev/null $ ls -t whacl-* | sed "1,2d" | while read file; do echo 'cp' $file /de +v/null; echo 'rm' $file; done | sh $ ls -t whacl-* whacl-20030608223002.gz whacl-20030607223001.gz
  • Comment on Re^5: Unix - Keep the last modified file in the directory and move/copy the rest of the files.
  • Download Code

Replies are listed 'Best First'.
Re: Re^5: Unix - Keep the last modified file in the directory and move/copy the rest of the files.
by hyliau (Initiate) on Jun 09, 2003 at 08:41 UTC
    hi zengargoyle, Very sorry for the interruption again. Here is the query from my colleague again. What if I want to keep the last 5 hour modified file in the directory and move/copy the rest of the files. That is, those last modified files which dated within the last 5 hour will be keep. Hope to hear you soon. Thanks. rgds, hyliau
      Here is the query from my colleague again. What if I want to keep the last 5 hour modified file in the directory and move/copy the rest of the files.

      you should tell your colleague to stop changing the requirements. things are now complicated enough that a perl solution would be better than a shell solution. see the other responses in the thread for pointers in that direction.

        hi zengargoyle, Sorry for the irritating and complexity request. I will look at other responses in the thread for pointers. At least I knew that to keep track last modified file base on time will be good to use perl script. Million Thanks. rgds, hyliau