<div dir="ltr">I'm glad you found a solution you like.<div><br></div><div>As someone who likes ed and sed, I wanted to mention that sed can do the "head" and "tail" jobs in one go if you turn off its "print by default" behavior with the "-n" option and then give it two commands, "1p" to print line one, and "$p" to print the last line.</div><div><br></div><div>bash$ { echo one; echo two; echo three; } | sed -n -e 1p -e '$p'<br>one<br>three<br>bash$</div><div> <br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Wed, Apr 16, 2025 at 10:39 PM Ron via Ale <<a href="mailto:ale@ale.org">ale@ale.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Boris Borisov via Ale wrote on 2025-04-12 14:42:<br>
<br>
> I'm trying to list all files on SSD disk (Windows) and to find the <br>
> oldest file and the newest file.<br>
<br>
This was way, *way*, WAY harder than expected.<br>
<br>
<br>
Here's how to find the newest and oldest files / directories across a <br>
disk - adjust the starting point to suit your mount point:<br>
<br>
find $mount -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0" \<br>
        | sort --zero-terminated                   \<br>
        | tr '\0' '\n'                             \<br>
        | less<br>
<br>
Then, look at top and bottom lines of output.<br>
<br>
Switch `less` for `head -n 1` or `tail -n 1` for seeing single lines of <br>
output.<br>
<br>
Note, this method will handle files with new lines in the file name <br>
(that was a rabbit hole! What a terrible idea).<br>
<br>
<br>
<br>
So, a breakdown on what it's doing:<br>
<br>
<br>
 > find $mount<br>
<br>
Will find all files and folders starting at $mount<br>
<br>
 > -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0"<br>
<br>
Will print to the screen "YYYY-MM-DD HH:MM:SS filename[NULL]" format <br>
(NULL is important for line-feed filenames, unlikely on a Windows disk)<br>
<br>
<br>
 > \<br>
<br>
Code continues on next line...<br>
<br>
<br>
 > | sort --zero-terminated \<br>
<br>
Pipe to `sort` command, letting it know that lines are delimited with <br>
nulls (x00) instead of newlines<br>
<br>
 > | tr '\0' '\n' \<br>
<br>
Translate those nulls back to newlines<br>
<br>
 > | less<br>
<br>
Lets one view the results.<br>
<br>
<br>
Tested on ~/ which contains > 2,779,286 entries and returned results <br>
from 1927 (?!?) at the top through to today at the bottom.<br>
<br>
<br>
<br>
Bonus: to get just the newest and oldest items, this will work <br>
*sometimes* but fails sometimes, and I'm not sure why - if anyone can <br>
explain, I'd love to hear it:<br>
<br>
<br>
find $mount -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0" \<br>
        | sort --zero-terminated \<br>
        | tr '\0' '\n' \<br>
        | tee >(head -n 1) >(tail -n 1) > /dev/null<br>
<br>
That final line will output everything, and send it to `head -n 1` and <br>
send it to `tail -n 1` then send the "everything" output from `tee` to <br>
/dev/null, leaving just the output from `head` and `tail`.<br>
<br>
<br>
An example where it does work:<br>
<br>
$ ll /tmp/test -trA<br>
total 0<br>
-rw-rw-r-- 1 uid1 uid1 0 1999-01-01 00:00  file1<br>
-rw-rw-r-- 1 uid1 uid1 0 2020-01-01 00:00 'file!'<br>
-rw-rw-r-- 1 uid1 uid1 0 2025-01-01 00:00  file2<br>
<br>
$ find /tmp/test/ -printf "%TY-%TM-%Td %TH:%Tm:%TS %p\0" \<br>
        | sort --zero-terminated<br>
        | tr '\0' '\n' \<br>
        | tee >(head -n 1) >(tail -n 1) > /dev/null<br>
1999-00-01 00:01:00.0000000000 /tmp/test/file1<br>
2025-44-16 18:04:07.9633502140 /tmp/test/<br>
<br>
<br>
<br>
Let us know what you find...<br>
<br>
<br>
This was an interesting project: poorly documented `find` options, <br>
discovery of files on my disk with "\n" in the name, how to deal with <br>
"\n" in file names, brush-up on printf format options, first usage of <br>
`sort --zero-terminated`, first use of multiple sub-shells,...<br>
<br>
<br>
Probably couldn't have done it without AI assistance (<a href="https://you.com" rel="noreferrer" target="_blank">https://you.com</a>) <br>
but it was a bit niche even for that.<br>
<br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org" target="_blank">Ale@ale.org</a><br>
<a href="https://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">https://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
</blockquote></div><div><br clear="all"></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr">  Ed Cashin <<a href="mailto:ecashin@noserose.net" target="_blank">ecashin@noserose.net</a>></div></div>