title: Take latest Log File & store in a variable
body: <p>I'm looking for a solution to grep the latest log file (say abc-20141028-005356.log), which was modified. I can do this with command "ls -1t  |head -1" , which gives solution, but there are other files which gets modified in the log folder too.. I tried doing with a head -2 which gives 2 files &amp; then grepped the file... Say "ls -1t | head -2 | grep abc-".</p>
  
  <p>The real problem is when we have the same filename with latest data &amp; new time &amp; date. How do i store the latest logfile to a variable which has the same duplicate file names &amp; tailing with date &amp; time.</p>
  
  <pre><code>PATH=`cd $1 ; ls -1t | head -1`
  LOGFILE=$1$PATH
  </code></pre>
  
  <p>$1=/opt/server1/logs/</p>
  
  <p>Kindly please advise on the same</p>
  
  <p>I'm not able to grep a unique file based o the time stamp, for an instance abc-20141028-005356.log is modified &amp; the next file is abc-20141028-005358.log, which is a time diff only.. but the complete file name remains same.</p>
  
  <p>Need a solution where in i can assign the latest file modified to a variable irrespective of the file name &amp; timestamp ( modified latest)</p>
  
  <pre><code>I have couple of files here
  
  total 0
  -rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005348.log
  -rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005358.log
  -rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005358.log
  -rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005360.log
  
  This is the latest file i get if i use the below
  
  [root@abcd RKP]# ls -1t | head -1
  abc-20141029-005360.log
  
  The above can be done when there is only files starting with abcd-***. Now i have the below files
  
  -rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005348.log
  -rw-r--r-- 1 root root 0 Nov  4 11:14 abc-20141028-005358.log
  -rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005358.log
  -rw-r--r-- 1 root root 0 Nov  4 11:19 abc-20141029-005360.log
  -rw-r--r-- 1 root root 0 Nov  4 11:21 EFG-20141028-005348.log
  -rw-r--r-- 1 root root 0 Nov  4 11:21 EFG-20141028-005358.log
  -rw-r--r-- 1 root root 0 Nov  4 11:21 EFG-20141029-005358.log
  
  When i do the above command 
  
  [root@abcd RKP]# ls -1t | head -1
  EFG-20141029-005358.log
  
  I want only files which are modified on abcd-XXXXX.XXXX based on the last modfied time.
  
  I'm sorry for making this so confused. Please let me know if you need anything more.
  
  The solution may be simple, but I'm not getting how to do this. !!!!
  </code></pre>
  
  <p>Thanks that worked....</p>
  
  <p>Thank you ... That worked. While running the above in my script i get this error                                                       line 30: [: : integer expression expected      </p>
  
  <p>Any idea what I'm  missing. The script for the same part is below.   </p>
  
  <pre><code> if [ -e $POSFile ]; then
  # Read last Position
  lastPosition=`cat $POSFile`
  fi
  fileLength=`stat -c %s $LOGFILE`
  
  if [ "$lastPosition" -gt "$fileLength" ]; then
  # Log file rolled
  lastPosition=0;
  fi
  
  difference=`expr $fileLength - $lastPosition`
  # New Spot  To POSFile
  
      enter code here
  
  echo $fileLength &gt; $POSFile      
  </code></pre>