[ale] Bash script SOLVED
Ed Cashin
ecashin at noserose.net
Thu Apr 17 18:25:42 EDT 2025
I'm glad you found a solution you like.
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.
bash$ { echo one; echo two; echo three; } | sed -n -e 1p -e '$p'
one
three
bash$
On Wed, Apr 16, 2025 at 10:39 PM Ron via Ale <ale at ale.org> wrote:
> Boris Borisov via Ale wrote on 2025-04-12 14:42:
>
> > I'm trying to list all files on SSD disk (Windows) and to find the
> > oldest file and the newest file.
>
> This was way, *way*, WAY harder than expected.
>
>
> Here's how to find the newest and oldest files / directories across a
> disk - adjust the starting point to suit your mount point:
>
> find $mount -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0" \
> | sort --zero-terminated \
> | tr '\0' '\n' \
> | less
>
> Then, look at top and bottom lines of output.
>
> Switch `less` for `head -n 1` or `tail -n 1` for seeing single lines of
> output.
>
> Note, this method will handle files with new lines in the file name
> (that was a rabbit hole! What a terrible idea).
>
>
>
> So, a breakdown on what it's doing:
>
>
> > find $mount
>
> Will find all files and folders starting at $mount
>
> > -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0"
>
> Will print to the screen "YYYY-MM-DD HH:MM:SS filename[NULL]" format
> (NULL is important for line-feed filenames, unlikely on a Windows disk)
>
>
> > \
>
> Code continues on next line...
>
>
> > | sort --zero-terminated \
>
> Pipe to `sort` command, letting it know that lines are delimited with
> nulls (x00) instead of newlines
>
> > | tr '\0' '\n' \
>
> Translate those nulls back to newlines
>
> > | less
>
> Lets one view the results.
>
>
> Tested on ~/ which contains > 2,779,286 entries and returned results
> from 1927 (?!?) at the top through to today at the bottom.
>
>
>
> Bonus: to get just the newest and oldest items, this will work
> *sometimes* but fails sometimes, and I'm not sure why - if anyone can
> explain, I'd love to hear it:
>
>
> find $mount -printf "%TY-%Tm-%Td %TH:%TM:%TS %p\0" \
> | sort --zero-terminated \
> | tr '\0' '\n' \
> | tee >(head -n 1) >(tail -n 1) > /dev/null
>
> That final line will output everything, and send it to `head -n 1` and
> send it to `tail -n 1` then send the "everything" output from `tee` to
> /dev/null, leaving just the output from `head` and `tail`.
>
>
> An example where it does work:
>
> $ ll /tmp/test -trA
> total 0
> -rw-rw-r-- 1 uid1 uid1 0 1999-01-01 00:00 file1
> -rw-rw-r-- 1 uid1 uid1 0 2020-01-01 00:00 'file!'
> -rw-rw-r-- 1 uid1 uid1 0 2025-01-01 00:00 file2
>
> $ find /tmp/test/ -printf "%TY-%TM-%Td %TH:%Tm:%TS %p\0" \
> | sort --zero-terminated
> | tr '\0' '\n' \
> | tee >(head -n 1) >(tail -n 1) > /dev/null
> 1999-00-01 00:01:00.0000000000 /tmp/test/file1
> 2025-44-16 18:04:07.9633502140 /tmp/test/
>
>
>
> Let us know what you find...
>
>
> This was an interesting project: poorly documented `find` options,
> discovery of files on my disk with "\n" in the name, how to deal with
> "\n" in file names, brush-up on printf format options, first usage of
> `sort --zero-terminated`, first use of multiple sub-shells,...
>
>
> Probably couldn't have done it without AI assistance (https://you.com)
> but it was a bit niche even for that.
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> https://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
--
Ed Cashin <ecashin at noserose.net>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.ale.org/pipermail/ale/attachments/20250417/71129096/attachment.htm>
More information about the Ale
mailing list