1

I am trying to find a way of getting my cmd script to look through my folder, find the older versions of the pdf docs in the folder and move them to another folder called older versions.

Problem I am having is the name of the file changes with the version. IE DE-321-DLS-R04.pdf has become DE-321-DLS-R05.pdf

I am not to sure where to start. I have seen scripts that use the date but filenames are different.

Please help.

closed as off-topic by slm, Anthon, jasonwryan, Renan, rahmu Aug 19 '13 at 12:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – slm, Anthon, jasonwryan, Renan, rahmu
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I guess the main problem here is how to tell if two files are two versions of the same document or two independent documents. Do you have some file name convention? e.g. all versions are named by appending -Rxx. – Wu Yongzheng Aug 15 '13 at 7:01
  • 1
    Is the first part of the name always the same for one document and were the old versions created before the new versions were added? – Raphael Ahrens Aug 15 '13 at 7:58
2

Maybe something like this:

find . -name 'DE-321-DLS-R*.pdf' | sort -r | sed '1 d' | xargs mv -t "older versions"

Explanation:

  • find . -name 'DE-321-DLS-R*.pdf' lists all PDF files in the current directory that begin with DE-321-DLS-R.

  • sort -r sorts the list of files in reverse order.

  • sed '1 d' outputs all lines of input except the first one.

  • xargs mv -t "older versions" invokes mv with the output of the previous sed command as arguments, and tells mv to move all arguments to the directory older versions.

Example:

$ ls -1
DE-321-DLS-R00.pdf
DE-321-DLS-R01.pdf
DE-321-DLS-R02.pdf
DE-321-DLS-R03.pdf
DE-321-DLS-R04.pdf
DE-321-DLS-R05.pdf
$ mkdir "older versions"
$ find . -name 'DE-321-DLS-R*.pdf' | sort -r | sed '1 d' |
xargs mv -t "older versions"
$ ls -1
DE-321-DLS-R05.pdf
older versions
$ ls -1 "older versions"
DE-321-DLS-R00.pdf
DE-321-DLS-R01.pdf
DE-321-DLS-R02.pdf
DE-321-DLS-R03.pdf
DE-321-DLS-R04.pdf
  • nice explanation – damphat Aug 15 '13 at 8:56
  • Hi Thomas. That looks like it would work will, I will give it a go when I get back to work in the morning. Thanks very much. – Simon Aug 15 '13 at 10:36
  • I cant seem to get this to work. – Simon Aug 15 '13 at 23:38
  • @Simon Can you describe the issues you are having with the oneliner in more detail? – Thomas Nyman Aug 16 '13 at 4:27
  • Thomas. I am writing this in a script and have the files on my K drive. I have got the dir K:\ "find de-321-dls" /i | /r but the rest does not seem to work. – Simon Aug 17 '13 at 10:48
0

Generate some documents for testing:

$ mkdir temp
$ cd temp
$ touch aaa-R00.pdf  aaa-R01.pdf aaa-R02.pdf  bbb-R19.pdf bbb-R20.pdf
$ ls | sort -r

bbb-R20.pdf
bbb-R19.pdf
aaa-R02.pdf
aaa-R01.pdf
aaa-R00.pdf

Look! bbb-R20.pdf and aaa-R02.pdf are lastest versions, other should be moved.

Copy paste this script to your terminal, that mark outdate document as 'outdate'

for doc in $(ls | grep 'R[0-9][0-9].pdf$' | sort -r); do
  name=${doc%%[0-9][0-9].pdf}

  if [[ "$name" != "$last" ]]; then
    echo $doc latest
  else
    echo $doc outdate
  fi
  last=$name
done

result:

bbb-R20.pdf latest
bbb-R19.pdf outdate
aaa-R02.pdf latest
aaa-R01.pdf outdate
aaa-R00.pdf outdate

For your question, you can replace "echo $doc outdate" to "mv $doc path/to/outdate/"

Not the answer you're looking for? Browse other questions tagged or ask your own question.