GoldenLee
GoldenLee

Reputation: 747

Get a sorted file list on a directory by using Linux bash script language

I need to list the files on a specified directory, and sort all available files on the basis of the date and time embedded in each file name, not by the file creation or modification date/time. My files have the following format: Before sort

RATMS-RNSCA_npp_d20131208_t0408392_e0417432_b00001_c20131223024038606000_all-_dev.h5
RATMS-RNSCA_npp_d20131208_t0547506_e0557586_b00001_c20131223021256522000_all-_dev.h5
RCRIS-RNSCA_npp_d20131208_t0408392_e0417432_b00001_c20131223024038506000_all-_dev.h5
RCRIS-RNSCA_npp_d20131208_t0548226_e0557586_b00001_c20131223021256270000_all-_dev.h5
RNSCA-ROLPS_npp_d20131208_t0408334_e0417550_b00001_c20131223024038619000_all-_dev.h5
RNSCA-ROLPS_npp_d20131208_t0548233_e0558223_b00001_c20131223021256591000_all-_dev.h5
RNSCA-RONPS_npp_d20131208_t0408543_e0417005_b00001_c20131223024038636000_all-_dev.h5
RNSCA-RONPS_npp_d20131208_t0548391_e0558002_b00001_c20131223021256616000_all-_dev.h5
RNSCA-ROTCS_npp_d20131208_t0408168_e0417380_b00001_c20131223024038627000_all-_dev.h5
RNSCA-ROTCS_npp_d20131208_t0548017_e0558002_b00001_c20131223021256603000_all-_dev.h5
RNSCA-RVIRS_npp_d20131208_t0407405_e0417380_b00001_c20131223024038167000_all-_dev.h5
RNSCA-RVIRS_npp_d20131208_t0547150_e0558377_b00001_c20131223021256099000_all-_dev.h5

After sort (my expected results)

RATMS-RNSCA_npp_d20131208_t0408392_e0417432_b00001_c20131223024038606000_all-_dev.h5
RCRIS-RNSCA_npp_d20131208_t0408392_e0417432_b00001_c20131223024038506000_all-_dev.h5
RNSCA-ROLPS_npp_d20131208_t0408334_e0417550_b00001_c20131223024038619000_all-_dev.h5
RNSCA-RONPS_npp_d20131208_t0408543_e0417005_b00001_c20131223024038636000_all-_dev.h5
RNSCA-ROTCS_npp_d20131208_t0408168_e0417380_b00001_c20131223024038627000_all-_dev.h5
RNSCA-RVIRS_npp_d20131208_t0407405_e0417380_b00001_c20131223024038167000_all-_dev.h5

RATMS-RNSCA_npp_d20131208_t0547506_e0557586_b00001_c20131223021256522000_all-_dev.h5
RCRIS-RNSCA_npp_d20131208_t0548226_e0557586_b00001_c20131223021256270000_all-_dev.h5
RNSCA-ROLPS_npp_d20131208_t0548233_e0558223_b00001_c20131223021256591000_all-_dev.h5
RNSCA-RONPS_npp_d20131208_t0548391_e0558002_b00001_c20131223021256616000_all-_dev.h5
RNSCA-ROTCS_npp_d20131208_t0548017_e0558002_b00001_c20131223021256603000_all-_dev.h5
RNSCA-RVIRS_npp_d20131208_t0547150_e0558377_b00001_c20131223021256099000_all-_dev.h5

Please pay attention the 3rd (dYYYYMMdd) and 4th (thhmmssS) fields in each aforesaid file name. Prefix letter 'd' means date, and the prefix 't' means time.

NOTE: ‘YYYYMMDD’ represents the date of the start of the swath (YYYY: 4 digit year; MM: month; DD: day of month). The first and the second ‘hhmmssS’ represent the start and end of swath, respectively (hh -hour; mm: minutes; ss: seconds; S: 10th of a second).

I think my needs can be met to have the file list to be sorted by using "YYYYMMdd_thh" combination. How could I do that by using the Linux bash script language?

Thank you.

GoldenLee

Upvotes: 1

Views: 849

Answers (1)

Noctua
Noctua

Reputation: 5208

You will love bash. Just using the sort command, you can pass a "field delimiter" and select fields on which to sort.

From the man-pages:

-t, --field-separator=SEP use SEP instead of non-blank to blank transition

-k, --key=KEYDEF sort via a key; KEYDEF gives location and type

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field; both are origin 1, and the stop position defaults to the line's end. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-let‐ ter ordering options [bdfgiMhnRrV], which override global ordering options for that key. If no key is given, use the entire line as the key.

The needed parameters would become:

sort -t '_' -k 3,4 you_data_file

So we've divided your data into fields on underscores, and sorted on first the 3th field (the date) and then the fourth (the time). Because you were so kind to have a format for date and time with increasing precision, just alphabetic sorting works out.

Upvotes: 2

Related Questions