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.

  • 1
    To sort the files by date you can use ls -t "-t sort by modification time, newest first". What do you mean with "copying every other"? PS> Do not parse the output of ls you can incur in problems. – Hastur Jan 20 '16 at 14:40
  • Thanks for that already! After sorting I want to copy every other file to a new folder, meaning that I take the first file, copy it, skip the second file, take the third one, copy it, skip the fourth and so on. – lhcgeneva Jan 20 '16 at 14:44
  • Have you considered that if someone (human or program) touches (or added) a file you will get a mess... :-) Even that if you generate(d) or copied those files really fast, they can be modified (=closed) in an asynchronous way? ... [Think weird because it sadly happens each day]. – Hastur Jan 20 '16 at 14:50
  • :D good point! Initially the files were named properly so I could distinguish the two 'classes' of files by name. Then I batch renamed on windows, as I assumed it would work similar to MacOs' finder. Not a good idea because it made 1600 files of the exact same name, only adding a number at the very end. Only thing I know is that the two file classes were created alternatingly. – lhcgeneva Jan 20 '16 at 14:56
  • Start again from the original files, if you can. You can do some tests maybe, but you cannot seriously work if you are not sure 100% about the data. Which kind of results will you obtain? Note that when you proceed lossy, relying on the sureness that a major failure will warn you if something is not correct... the Murphy's_law will show. If those files are of a different kind (check with file file1 file2) you can rely a little more. You can wipe out the doubts with md5sum on local and original copy... – Hastur Jan 20 '16 at 15:05

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

for f in $(ls -rt) ; do 
  if [ $((foo%2)) -eq 0 ];
       echo "even " "$f";  // maybe here copy
       echo "odd" "$f" ;   // maybe here skip
  let foo++

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.

  • That's pretty much exactly what I was looking for. – lhcgeneva Jan 20 '16 at 15:58
  • I knew but remember,remember,remember all the warning. :) – Hastur Jan 20 '16 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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