0

I need a solution(s) on how to change to the latest directory that has similar naming conventions as others. For example:

Folder names are as follows...
parent_directory ---> folder100.000.200
----------------------------folder100.000.201
----------------------------folder100.000.202
----------------------------folder100.000.203

Using linux shell, I'm trying to navigate to the parent_directory then to the latest folder (folder100.000.203) without having to change the script every single day (folders are added regularly).

The old solution was:

FOLDER=folder100.000.$1
cd parent_folder/${FOLDER}

But this no longer works. I understand that $1 is a positional parameter, but fail to understand why it worked previously and not now. Any help with this would be greatly appreciated.

EDIT: So I figured out the old script was passing a value into the script, which is where $1 was coming from. When I ran the script, I forgot I had to pass in the version number, but is there a way around me having to specify this?

./script.sh 203

This is the current working solution, but I still want to automate finding the number and passing it in.

  • See http://stackoverflow.com/questions/9275964/get-the-newest-directory-in-bash-to-a-variables – sgrg Apr 20 '17 at 21:06

2 Answers2

0

If the folder you want is the most recently created (regardless of its name) then you can use ls -c to sort by ctime descending. So, perhaps something like:

cd $(ls -c | head -1)
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • that might work, but it would have to be a combination of other things, as well. The directory contains a bunch of different files and folders, each named different things and updated at different times. Think multiple products and versions with multiple updates per day. – Justin Simmons Apr 20 '17 at 23:25
0

I think I have found a solution, but I wanted more eyes on it to verify.

cd $(ls -dr */ | grep 'folder100.000' | head -1

This solution returns the directory (-d */) that has 'folder100.000' (grep 'folder100.000') in it's name and sorts it in reverse order (-r), which returns the highest build number (head -1). So this code returns:

folder100.000.203

Since the only thing that changes is the final 3 numbers, is this a proper solution or should I find another way of finding the latest version folder?

Side note: These commands will be running in ftp