Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to open the last folder irrespective of name on Bash

I’m currently working with a program that creates a folder with a time stamp and puts some files into it.

I want to write a script to automate retrieval of the file from last or second last folder by alphabetical order.

How do I do this on bash?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Image for reference. I want to retrieve a file from the 18-38 folder or the 17-45 folder.

enter image description here

>Solution :

I want to write a script to automate retrieval of the file from last or second last folder by alphabetical order.

cd path/to/02-March-2022

last_dir=$(printf '%s\n' */ | sort | tail -n 1)
second_last_dir=$(printf '%s\n' */ | sort -r | sed '2!d; 2q'
  • You can remove or add sort’s -r to switch between Nth newest and Nth last.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading