Home > Software design >  linux find files by created in last X minutes sort does not return expected list
linux find files by created in last X minutes sort does not return expected list

Time:02-06

I have a folder with many jpg files, where each file name presents the creation time in the form of: "20220204T160311.746....jpg" ("..." represent additional data). I'm trying to get all files created on the last X minutes. There are tons of results for such query if one asks Google, nevertheless, I'm getting weird results when trying the following:

find . -cmin -50 \( ! -iname "*.csv" ! -iname ".*" \) -ls | sort -n

the results do not start ok ("now" is 16:26):

 14466561   2636 -rw-r--r--   1 root     root      2695349 Feb  4 16:03 ./20220204T160348.526-cam4-791-AREASbw-32358.jpg
 14466569   2208 -rw-r--r--   1 root     root      2257356 Feb  4 16:03 ./20220204T160354.069-cam4-614.jpg
 14466571   1044 -rw-r--r--   1 root     root      1068758 Feb  4 16:03 ./20220204T160354.069-cam4-614__chng_182.jpg
 14466579   2208 -rw-r--r--   1 root     root      2258697 Feb  4 16:03 ./20220204T160359.296-cam4-352.jpg
 14466581   1048 -rw-r--r--   1 root     root      1070111 Feb  4 16:03 ./20220204T160359.296-cam4-352__chng_175.jpg
 14466589   2216 -rw-r--r--   1 root     root      2268675 Feb  4 16:04 ./20220204T160404.682-cam4-179.jpg
 14466591   1048 -rw-r--r--   1 root     root      1070053 Feb  4 16:04 ./20220204T160404.682-cam4-179__chng_273.jpg
 14466597   2204 -rw-r--r--   1 root     root      2253706 Feb  4 16:04 ./20220204T160409.957-cam4-571.jpg
 14466599   1048 -rw-r--r--   1 root     root      1070335 Feb  4 16:04 ./20220204T160409.957-cam4-571__chng_154.jpg
 14466600   1044 -rw-r--r--   1 root     root      1068675 Feb  4 16:04 ./20220204T160409.957-cam4-571__chng_309.jpg
 14466601   2652 -rw-r--r--   1 root     root      2712638 Feb  4 16:04 ./20220204T160409.957-cam4-571-AREASbw-31696.jpg
 14466610   2208 -rw-r--r--   1 root     root      2259408 Feb  4 16:04 ./20220204T160415.421-cam4-959.jpg
 14466611   1048 -rw-r--r--   1 root     root      1070365 Feb  4 16:04 ./20220204T160415.421-cam4-959__chng_272.jpg
 14466620   2212 -rw-r--r--   1 root     root      2264606 Feb  4 16:04 ./20220204T160420.666-cam4-742.jpg
 14466621   1048 -rw-r--r--   1 root     root      1070276 Feb  4 16:04 ./20220204T160420.666-cam4-742__chng_173.jpg

and when I continue to scroll up, it arrives to a part where the files are wrongly sorted (like what supposed to be the start of the list):

14457644   2240 -rw-r--r--   1 root     root      2293209 Feb  4 16:26 ./20220204T162633.985-cam4-188.jpg
 14457645   1056 -rw-r--r--   1 root     root      1080805 Feb  4 16:26 ./20220204T162633.985-cam4-188__chng_171.jpg
 14457658   2236 -rw-r--r--   1 root     root      2286664 Feb  4 16:26 ./20220204T162639.299-cam4-801.jpg
 14457659   1056 -rw-r--r--   1 root     root      1080042 Feb  4 16:26 ./20220204T162639.299-cam4-801__chng_158.jpg
 14462048   2200 -rw-r--r--   1 root     root      2252735 Feb  4 15:49 ./20220204T154927.284-cam4-095.jpg
 14462049   1044 -rw-r--r--   1 root     root      1065249 Feb  4 15:49 ./20220204T154927.284-cam4-095__chng_134.jpg
 14462088   2204 -rw-r--r--   1 root     root      2255657 Feb  4 15:49 ./20220204T154932.490-cam4-571.jpg
 14462089   1044 -rw-r--r--   1 root     root      1066449 Feb  4 15:49 ./20220204T154932.490-cam4-571__chng_228.jpg
 14462118   2204 -rw-r--r--   1 root     root      2254481 Feb  4 15:49 ./20220204T154937.767-cam4-237.jpg
 14462127   1044 -rw-r--r--   1 root     root      1066700 Feb  4 15:49 ./20220204T154937.767-cam4-237__chng_79.jpg

How come in the middle it goes back from 15:49 to 16:26? It happens on every query I'm running, unless it is on a short term like 10 min.

CodePudding user response:

By default sort uses the first column. Use -k to specify column number to sort by.

find . -cmin -50 \( ! -iname "*.csv" ! -iname ".*" \) -ls | sort -n -k 7 
  •  Tags:  
  • Related