Visit Our UNIX and Linux User Community

Read the latest file

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Read the latest file
# 1  
Old 02-09-2008
Read the latest file

I need to read the contents of the latest file. Below is a sample. I can use the tail command but can this be put into a script?

I just need to read the latest TWSMERGE.log

-rwxr--r-- 1 tws unison 431581 Feb 06 05:01 20080205_TWSMERGE.log
-rwxr--r-- 1 tws unison 5353 Feb 07 04:59 20080206_NETMAN.log
-rwxr--r-- 1 tws unison 431089 Feb 07 05:01 20080206_TWSMERGE.log
-rwxr--r-- 1 tws unison 5353 Feb 08 04:59 20080207_NETMAN.log
-rwxr--r-- 1 tws unison 430095 Feb 08 05:03 20080207_TWSMERGE.log
-rwxr--r-- 1 tws unison 5353 Feb 09 04:59 20080208_NETMAN.log
-rwxr--r-- 1 tws unison 431690 Feb 09 05:01 20080208_TWSMERGE.log
-rwxr--r-- 1 tws unison 6307 Feb 09 16:25 20080209_NETMAN.log
-rwxr--r-- 1 tws unison 351434 Feb 09 18:00 20080209_TWSMERGE.log
# 2  
Old 02-09-2008
Something like this?

cat `ls -ctur *_TWSMERGE.log | tail -1`

# 3  
Old 02-10-2008
Thanks, HPAVC

This is what I have. Your command works but I just want to read the Lates/current file so I put *0209_TWSMERGE.log which works but I will probably have to change it again tomorrow?

cat `ls -ctur *0209_TWSMERGE.log | tail -l` > /usr/Tivoli/TWS/USERS/t0571897/report1
more $MYHOME/report1 |grep "SUCCESSFULLY" > $MYHOME/report2
# 4  
Old 02-10-2008
Originally Posted by wereyou
Thanks, HPAVC

This is what I have. Your command works but I just want to read the Lates/current file so I put *0209_TWSMERGE.log which works but I will probably have to change it again tomorrow?
I might be way to sleepy to understand the problem, i assume you have a directory full of log files and every so often a new one is made.

"ls -ctr PATTERN" should list the contents

-t sort by modification time
-r reverse order while sorting
-c just one column

so if the files where


ls -ctr *.txt would list them


the tail -1 just lists the last file (the newly modifed one)

so "cat `ls -ctr *.txt | tail -1`" gives cat the newest modified file.

if the issue is that you want some sexy 'look busy' dedicated window that always has the tail -f LATESTFILE going. yeah some sort of loop would be needed or a sexy tail. I dont know the tail command you have but ...

# this will try and get a PID from something and when that PID dies it will
# 'untail', then re-tail
while true; do
PID=`cat /var/run/`
tail --pid=$PID --follow `ls -ctr *_TWSMERGE.log | tail -1`
sleep 5

# this will keep tailing until the file goes sour (100 attempts looking
# at it with the no changes) and then it will get a new one
while true; do
tail --max-unchanged-stats=100 --follow `ls -ctr *_TWSMERGE.log | tail -1`
sleep 5

if you have a 'runfor' like command then obviously you could do that for "runfor 3600 tail ..." and it would run it for an hour and then retail every hour.

now for sleep

Previous Thread | Next Thread
Test Your Knowledge in Computers #162
Difficulty: Easy
Early versions of MINIX were created by Andrew S. Tanenbaum for commercial purposes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read latest log files and perform database insert

Hi Experts, I have a situation where I need to write a shell script to continuously monitor a log directory with multiple log files and perform following: 1. Read the latest log file continuously and grep "Success" OR "Failure" 2. As it capture either Success or Failure, it has to perform a... (1 Reply)
Discussion started by: rish_max
1 Replies

2. Shell Programming and Scripting

Picking the latest file based on a timestamp for a Dynamic file name

Hi , I did the initial search but could not find what I was expecting for. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

3. Shell Programming and Scripting

Read latest file name with a date and time and then download from FTP

Hi All, Please help. I have requirement to read the file / folder based on the latest date and download the file and folder. There will be files and folders in the location like 20140630-144422 20140630-144422.csv 20140707-182653 20140707-182653.csv 20140710-183153... (7 Replies)
Discussion started by: Praveen Pandit
7 Replies

4. Shell Programming and Scripting

Shell script to get the latest file from the file list and move

Hi, Anybody help me to write a Shell Script Get the latest file from the file list based on created and then move to the target directory. Tried with the following script: got error. A=$(ls -1dt $(find "cveit/local_ftp/reflash-parts" -type f -daystart -mtime -$dateoffset) | head... (2 Replies)
Discussion started by: saravan_an
2 Replies

5. AIX

How to read the latest file name in adirectory.

Hello all, In a shell script, I need to get the name of the latest file which was created in a directory. i.e. I have the following directory. /tmp/public/logs/ In this directory, I want to get the latest file that was created. Can you give me a hint as how can I get the latest file,... (2 Replies)
Discussion started by: haroon_a
2 Replies

6. Shell Programming and Scripting

To get the latest file

Hi Experts Team, I wish to store the latest file name of partcular pattern in the remote server in a variable. i tried this LATEST_FILE=`ssh ${USER_ID}@${REMOTE_HOSTNAME} 'ls -t ${SOURCE_DIRECTORY}/${SOURCE_FILEPATTERN}'` but its nt working..pls guide me.. Regards, Kanda (2 Replies)
Discussion started by: spkandy
2 Replies

7. Shell Programming and Scripting

get latest file

i have the following in my directory x: 3 files with the word "LIST" inside the files 2 files without the word "LIST" 1 folder (sudirectory) i want to get the filename of the latest file (timestamp) with the word "LIST". by the way the script and the list of files are in seperate... (4 Replies)
Discussion started by: inquirer
4 Replies

8. Shell Programming and Scripting

how to get the latest file

I am trying to scp the latest file which ends with "_abc.log". Can some help me figure out how can do that? (3 Replies)
Discussion started by: shehzad_m
3 Replies

9. Shell Programming and Scripting

How do I get the name of latest file?

1) How do I get the name of latest file in a variable? 2) Is it safe to delete all files from a dir I am doing cd $dir_name if return_code > 0 rm * fi what are other alternates to delete all files from a dir in a shell script? :) (5 Replies)
Discussion started by: Hangman2
5 Replies

10. UNIX for Dummies Questions & Answers

Getting latest file from ftp

Hi, i have multile JAMA01.DAT.* files in my ftp. how can i get the latest file in from the ftp by executing the script :rolleyes:? Regards, Arun S (3 Replies)
Discussion started by: arunavlp
3 Replies

Featured Tech Videos