Bash, getting the latest folder based on its name which is a date


Can anyone tell me how to get the name of the latest folder based on its name which is formatted as a date, using bash. For example:

20161121/ 20161128/ 20161205/ 20161212/

The output should be: 20161212


Just use GNU sort with -nr flags for based on reverse numerical sort.

find . ! -path . -type d | sort -nr | head -1

An example structure, I have a list of following folders in my current path,

find . ! -path . -type d ./20161121 ./20161128 ./20161205 ./20161212

See how the sort picks up the folder you need,

find . ! -path . -type d | sort -nr ./20161212 ./20161205 ./20161128 ./20161121

and head -1 for first entry alone,

find . ! -path . -type d | sort -nr | head -1 ./20161212

to store it in a variable, use command-substitution $() as

myLatestFolder=$(find . ! -path . -type d | sort -nr | head -1)


Sorting everything seems like extra work if all you want is a single entry. It could especially be problematic if you need to sort a very large number of entries. Plus, you should note that find-based solutions will by default traverse subdirectories, which might or might not be what you're after.

$ shopt -s extglob $ mkdir 20160110 20160612 20160614 20161120 $ printf '%d\n' 20+([0-9]) | awk '$1>d{d=$1} END{print d}' 20161120 $

While the pattern 20+([0-9]) doesn't <em>precisely</em> match dates (it's hard to validate dates without at least a couple of lines of code), we've at least got a bit of input validation via printf, and a simple "print the highest" awk one-liner to parse printf's results.

Oh, also, this handles any directory entries that are named appropriately, and does not validate that they are themselves directories. That too would require either an extra test or a different tool.

One method to require items to be directories would be the use of a trailing slash:

$ touch 20161201 $ printf '%s\n' 20+([0-9])/ | awk '$1>d{d=$1} END{print d}' 20161120/

But that loses the input validation (the %d format for printf).

If you felt like it, you could build a full pattern for your directory names though:

$ dates='20[01][0-9][01][0-9][0-3][0-9]' $ printf '%s\n' $dates/ | awk '$1>d{d=$1} END{print d}' 20161120/


  • Pass information from one layer to another
  • Google app script copy document page
  • How can I suppress the prompt to “Unify Duplicated Vertices” when opening several meshes from regula
  • How can I make Eclipse remember the Android SDK location?
  • Object array clone with subset of properties
  • Getting SerializeObject to use JsonProperty “name” defined inside interface
  • is uninitialized_copy/fill(In first, In last, For dest, A &a) an oversight in the c++ standard?
  • How to find the number of equal elements in javascript array
  • How to Convert Cordova Image Picker Results to Base64 format?
  • Does Swing come with Calendar and time field components?
  • Can't stop while loop
  • Git cherry-Pick a batch of commits from a file
  • functions and when to use brackets/parenthesis
  • Powerpoint VBA Macro Save As Dialog File Filter
  • How do I know what reference to include to import a specific .NET namespace?
  • SSRS 2008 - Sorting within a group
  • Let a function return any type in C++ class
  • What's the syntax to inherit documentation from another indexer?
  • Why the SequenceFile is truncated?
  • DIV instruction jumping to random location?
  • bad substitution shell- trying to use variable as name of array
  • htaccess add www if not subdomain, if subdomain remove www
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • JSON response opens as a file, but I can't access it with JavaScript
  • Use of this Javascript
  • MongoDB in PHP using aggregate to group by _id is null not working
  • How do I change content of ComboFieldEditor?
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • Accessing IRQ description array within a module and displaying action names
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • htaccess rewriting URLs with multiple forward slashes
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Do I've to free mysql result after storing it?
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to format a variable of double type
  • SQL merge duplicate rows and join values that are different
  • How to Embed XSL into XML