0

I have a directory in which I will have some folders with date format (YYYYMMDD) as shown below -

david@machineX:/database/batch/snapshot$ ls -lt
drwxr-xr-x 2 app kyte 86016 Oct 25 05:19 20141023
drwxr-xr-x 2 app kyte 73728 Oct 18 00:21 20141016
drwxr-xr-x 2 app kyte 73728 Oct  9 22:23 20141009
drwxr-xr-x 2 app kyte 81920 Oct  4 03:11 20141002

Now I need to extract latest date folder from the /database/batch/snapshot directory and then construct the command in my shell script like this -

./file_checker --directory /database/batch/snapshot/20141023/  --regex ".*.data" > shardfile_20141023.log

Below is my shell script -

#!/bin/bash

./file_checker --directory /database/batch/snapshot/20141023/ --regex ".*.data" > shardfile_20141023.log

# now I need to grep shardfile_20141023.log after above command is executed

How do I find the latest date folder and construct above command in a shell script?

john
  • 11,311
  • 40
  • 131
  • 251
  • `How do I find the latest date folder:`? What if: `$(ls -1t | head -n 1)`? –  Oct 28 '14 at 04:37
  • possible duplicate of [Get the newest directory in bash to a variables](http://stackoverflow.com/questions/9275964/get-the-newest-directory-in-bash-to-a-variables) – tripleee Oct 28 '14 at 04:38
  • and it has to be in date format along with most recent – john Oct 28 '14 at 04:42
  • @skwllsp how do I make sure that I should get only date folder which is most recent? – john Oct 28 '14 at 04:54
  • 1
    What does the most recent folder mean for you? Modification time of the folder (then ls -t shows it) or modification time of files in subfolder? Or something else? –  Oct 28 '14 at 04:58
  • modification time of the folder but there might be other folders as well which are not in date format so I need to make sure I get the most recent date folder – john Oct 28 '14 at 04:59
  • Look, this is one of approaches, just grep only folders that have 8 digits: `$(ls -t1 | grep -P -e "\d{8}" | head -1)`. Or `$(ls -t1 | grep -E -e "[0-9]{8}" | head -1)` –  Oct 28 '14 at 05:10

3 Answers3

1

Look, this is one of approaches, just grep only folders that have 8 digits:

ls -t1 | grep -P -e "\d{8}" | head -1

Or

ls -t1 | grep -E -e "[0-9]{8}" | head -1
0

You could try the following in your script:

pushd /database/batch/snapshot

LATESTDATE=`ls -d * | sort -n | tail -1`

popd

./file_checker --directory /database/batch/snapshot/${LATESTDATE}/ --regex ".*.data" > shardfile_${LATESTDATE}.log

seshasai
  • 11
  • 1
0

See BashFAQ#099 aka "How can I get the newest (or oldest) file from a directory?".

That being said, if you don't care for actual modification time and just want to find the most recent directory based on name you can use an array and globbing (note: the sort order with globbing is subject to LC_COLLATE):

$ find
.
./20141002
./20141009
./20141016
./20141023
$ foo=( * )
$ echo "${foo[${#foo[@]}-1]}"
20141023
Adrian Frühwirth
  • 42,970
  • 10
  • 60
  • 71