1

I was wondering what would be the most concise way of sorting files by date and then copying every other to a new folder.

My problem: I've got files renamed such that they are called

file1 (101)
file2 (103)
file3 (110)
.
.
.

where the numbering is not indicative of the intrinsic file difference. The only thing that's really different between the two file types I want to sort is their alternating creation date, therefore my rather convoluted above problem statement.

lhcgeneva
  • 135

1 Answers1

1

The following script seems to be a good one but it will work only if you have "well behaving" filenames [1]:

#!/bin/bash
foo=0
for f in $(ls -rt) ; do 
  if [ $((foo%2)) -eq 0 ];
    then
       echo "even " "$f";  // maybe here copy
    else
       echo "odd" "$f" ;   // maybe here skip
  fi
  let foo++
done

So essentially no newlines, no tab, no spaces... as it seems in your case.
Please remember that is not safe to parse the output of ls [1] and doublecheck always.

If you are not in the safe area in which you can use ls, then you may consider to find a solution with find, maybe taking some inspiration from Gilles' answer [2].


Ps> In case of data corruption, even in a light case as yours, it always needed to check that the patch worked. Often is more convenient to start again from the beginning. If, as I guess, the data size is huge and you cannot transfer/download it again, it's always possible to do some check (e.g. md5sum[3]) on the original data and the patched one.

Hastur
  • 19,483
  • 9
  • 55
  • 99