2

If I have a list of files under a directory

ullman-000.pbm
ullman-001.pbm
...
ullman-098.pbm
ullman-099.pbm
ullman-1000.pbm
ullman-1001.pbm
ullman-1002.pbm
ullman-1003.pbm
ullman-1004.pbm
ullman-1005.pbm
ullman-1006.pbm
ullman-1007.pbm
ullman-1008.pbm
ullman-1009.pbm
ullman-100.pbm
ullman-1010.pbm
ullman-1011.pbm
ullman-1012.pbm
ullman-1013.pbm
ullman-1014.pbm
ullman-1015.pbm
ullman-1016.pbm
...

When using ullman*.pbm to refer to them and pass it to command convert ullman*.pbm ullman.pdf as input files, how can I make them in the order of

ullman-000.pbm
ullman-001.pbm
...
ullman-098.pbm
ullman-099.pbm
ullman-100.pbm
...
ullman-999.pbm
ullman-1000.pbm
ullman-1001.pbm
ullman-1002.pbm
ullman-1003.pbm
ullman-1004.pbm
ullman-1005.pbm
ullman-1006.pbm
ullman-1007.pbm
ullman-1008.pbm
ullman-1009.pbm
ullman-1010.pbm
ullman-1011.pbm
ullman-1012.pbm
ullman-1013.pbm
ullman-1014.pbm
ullman-1015.pbm
ullman-1016.pbm
...

Thanks?

  • Are you sure that's not the default behaviour ? – Gilles Quenot Dec 7 '14 at 15:25
  • @sputnick The default behaviour is lexicographic. OP wants numeric. – muru Dec 7 '14 at 15:27
  • If you can, try renaming all the files so that they have four digits (padded with zeros). – muru Dec 7 '14 at 15:28
  • I don't have this problem with bash 4 pastie.org/9766149 – Gilles Quenot Dec 7 '14 at 15:29
  • @sputnick Your three digit numbers conveniently begin with 0. – muru Dec 7 '14 at 15:33
2

GNU sort and xargs might do the trick

printf '%s\0' ullman*.pbm | sort -z -k2,2n -t'-' | xargs -0 convert 

First check this works by listing files without calling convert

printf '%s\0' ullman*.pbm | sort -z -k2,2n -t'-' | xargs -0 printf '%s\n'
ullman-000.pbm
ullman-001.pbm
ullman-098.pbm
ullman-099.pbm
ullman-100.pbm
ullman-1000.pbm
...

Whereas

printf '%s\0' ullman*.pbm  | xargs -0 printf '%s\n'                                                              
ullman-000.pbm
ullman-001.pbm
ullman-098.pbm
ullman-099.pbm
ullman-1000.pbm
..
..
1

One way would be to rename the files that have three digit numbers to four digit ones, padded with a zero. If you have perl-rename (installed by default on Ubuntu) you can try:

rename -n 's/-(\d{3}\.)/-0$1/' *.pbm

Once you're satisfied with the result, run again without the -n.

Or see other options in Padding a number in a filename to a fixed length.

  • Thanks, It also adds a 0 infront of those with 4 digits already. Damage has been done. How can I revert those files original with 4 digits? – Tim Dec 7 '14 at 15:45
  • @Tim i had a an error, forgot to to escape the ., which I corrected soon. Did you run it without the -n? If so, you can change the 3 to 4 and run the corrected version. – muru Dec 7 '14 at 15:46

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.