3

I have some files with their names starting with 00, 01, ..., but there can be multiple filename with the same number i.e. 00_filename1, 00_filename2. I want to list them sorted by their names, but if there are more files with the same starting number, I want them to be sorted by the date of modification (in descending order) too.

So instead of this:

Oct 31 19:05 00_trash
Oct 31 19:14 00_trash_00
Oct 31 18:57 01_creating_dirs.py
Oct 31 19:05 01_trash
Oct 31 16:20 02_creating_csv.py
Oct 31 19:22 02_renaming_files.sh
Oct 31 16:20 03_creating_db.py
Oct 31 19:06 03_trash
Oct 31 16:21 04_importing_csv_to_db.py
Oct 31 16:20 05_some_xl.py
Oct 31 16:22 06_regex.py

I want then to be sorted like this:

Oct 31 19:14 00_trash_00
Oct 31 19:05 00_trash
Oct 31 19:05 01_trash
Oct 31 18:57 01_creating_dirs.py
Oct 31 19:22 02_renaming_files.sh
Oct 31 16:20 02_creating_csv.py
Oct 31 19:06 03_trash
Oct 31 16:20 03_creating_db.py
Oct 31 16:21 04_importing_csv_to_db.py
Oct 31 16:20 05_some_xl.py
Oct 31 16:22 06_regex.py

I have tried combining ls (using arguments -t and -r) with sort (using arguments -kn,m - where n,m specify the range of columns - and -r), but after hours of searching and trying I did not success.

1
  • 2
    You could do it with zsh I think - using glob qualifiers like print -RC1 *(oe:'REPLY=${REPLY%%_*}':om) Commented Oct 31, 2020 at 22:22

1 Answer 1

5

With GNU utils,

ls -1t | sort -nst '_' -k 1,1
  • ls -1t sorts by modification time, outputing one file per line.

  • sort:

    • -n: Do a numerical sort,
    • -t '_': Use underscore as field separator,
    • -k 1,1: Sort the first field (i.e., the two digits before the first underscore),
    • -s: Do a stable sort, i.e., don't use the following fields for sorting in case two lines have the same 1st field.

If you prefer the long listing format output,

ls -lt | sort -nsk 9.2

The 9.2 key means: Only sort the first two characters of the 9th field.

I'm assuming this is for visualization only, as parsing ls should not be part of any serious script.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .