In a "backup" folder, where most subfolders are named with a date, like in the following example:


I want to list all folders whose name are dates greater than, let's say, "20190415".

The point here is that the naming format allows sorting names in alphanumeric order to get a date sorting, so I guess something should be done with sorting order and criteria, but what?

Thank you!

  • With this date format it doesn't matter: lexicographic and numeric comparisons will give the same result.
    – AFH
    Apr 29, 2019 at 12:51
  • Sure. However, I'm looking to find a command to compare filenames together. 20190415 < 20190429
    – Ninj
    Apr 29, 2019 at 12:59
  • The if command will do this: the precise syntax will depend on the OS and shell.
    – AFH
    Apr 29, 2019 at 13:01
  • I'm on Ubuntu with bash. I tried with if [ ${name1} -gt ${name2} ] with no result
    – Ninj
    Apr 29, 2019 at 13:05
  • What do you mean by "with no result"? There is always a result, true or false. If there are other characters in the directory (such as trailing /) then you'll need to use lexicographic comparison if [ "${name1}" ">" "${name2}" ]. The quotes round the > are needed for correct parsing, but it is good practice to use them in variable expansions.
    – AFH
    Apr 29, 2019 at 13:23


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.