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

maybe you should try and do it using something you're familiar with. in this case i would probably just use a simple shell script.

#!/bin/sh ls -t $1 | ( read keepthisone while read movethisone do echo $2 $movethisone $3 done )

very simple, serves multiple purposes.

$ ls -1 whacl-* whacl-20030601223002.gz whacl-20030602223002.gz whacl-20030603223003.gz whacl-20030604223003.gz whacl-20030605223003.gz $ ./movem.sh 'whacl-*' whacl-20030604223003.gz whacl-20030603223003.gz whacl-20030602223002.gz whacl-20030601223002.gz $ ./movem.sh 'whacl-*' cp /dev/null cp whacl-20030604223003.gz /dev/null cp whacl-20030603223003.gz /dev/null cp whacl-20030602223002.gz /dev/null cp whacl-20030601223002.gz /dev/null $ ./movem.sh 'whacl-*' mv /path/to/foreverstore mv whacl-20030604223003.gz /path/to/foreverstore mv whacl-20030603223003.gz /path/to/foreverstore mv whacl-20030602223002.gz /path/to/foreverstore mv whacl-20030601223002.gz /path/to/foreverstore $ ./movem.sh 'whacl-*' rm rm whacl-20030604223003.gz rm whacl-20030603223003.gz rm whacl-20030602223002.gz rm whacl-20030601223002.gz $ ./movem.sh 'whacl-*' rm | sh $ ls whacl-* whacl-20030605223003.gz

take out the 'echo' if you don't want to check your commands before doing them.

  • Comment on Re: Unix - Keep the last modified file in the directory and move/copy the rest of the files.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: 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 05:41 UTC
    hi zengargoyle, Thanks for your suggestion. I am not familiar with Shell Script as well. But roughly I know what is that about. May I know what are those variables use for & how to use it. 1. $1 , $2 , $3 - what should this variable use for? when to use it? is this a parameter to be pass in when we run the ./movem.sh? 2. keepthisone - what should this variable use for? when to use it? is this a parameter to be pass in when we run the ./movem.sh? or I need to put in the value here? 3. movethisone - what should this variable use for? when to use it? is this a parameter to be pass in when we run the ./movem.sh? or I need to put in the value here? 4. $movethisone - how about this again? Hope to hear from you soon as I am rushing for this script. Thanks. rgds, hyliau
      1. $1 , $2 , $3 - what should this variable use for? when to use it? is this a parameter to be pass in when we run the ./movem.sh?

      yes, they are the parameters, pretend i gave them names.

      2. keepthisone - what should this variable use for? when to use it? is this a parameter to be pass in when we run the ./movem.sh? or I need to put in the value here?

      pretend i called this variable 'the_most_recent_file'.

      3. movethisone - what should this variable use for? when to use it? is this a parameter to be pass in when we run the ./movem.sh? or I need to put in the value here?

      pretend i called this variable 'not_the_most_recent_file'

      #!/bin/sh files_to_check=$1 command_to_run=$2 the_destination=$3 echo "# looking for files matching $files_to_check" # ls -t sorts files by last modification time, most recent to oldest ls -t $files_to_check | ( # the first line will be the most recent file read the_most_recent_file echo "# do nothing with the most recent file: $the_most_recent_file" # the rest of the lines will not be the most recent file echo "# do something with the rest of the files" while read not_the_most_recent_file do echo $command_to_run $not_the_most_recent_file $the_destination done )
        hi zengargoyle, One more question. 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. Hope to hear you soon. Thanks. rgds, hyliau