How do i read only last 5 files records from a directory.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do i read only last 5 files records from a directory.?
# 1  
Old 10-26-2017
How do i read only last 5 files records from a directory.?

Hello team,

I have a directory containing huge number of files.
I need to go select last 5 files and read it to do a calculation.

Kindly guide, how can i achieve it.


Regards,
Sadique
# 2  
Old 10-26-2017
That highly depends how you define "the last 5" - sorted alphabetically? by date? Other?
# 3  
Old 10-26-2017
The directory keep geeting files continously.
So if i do ls -l | tail -5
I get that latest 5 files,
My main concern is i need to read each files in loop
Then awk few data from the files comeout and do some calculation.

For all the files inside a directory it os working, but i want to implement it only last 5 files of the directory.

---------- Post updated at 04:17 PM ---------- Previous update was at 09:29 AM ----------

here is my code:
Code:
#!/bin/sh
DATE=`date +"%d-%m-%Y-%H:%M"`
stream=IUCS
FLAG=LAST
path=/log/INVESTIG/TEST
for  files in $path/*
        do
        for f in $files
        do
        #echo $files
        read -r line || [ -n "$line"];
        TTHEX=`awk -F ',' 'END{print $4}'`
                done < $files
                TIMESTAMP=$( date +'%H:%M:%S' -r $files)
                TRANS_TIME=$(date -d @$(expr `printf "%d" 0x$TTHEX` / 1000) | awk '{print $4}')
                TIME_LAG=$(date +%H:%M:%S -ud @$((`expr $(date -u -d "$TIMESTAMP" +"%s") - $(date -u -d "$TRANS_TIME" +"%s")`)))
 
        echo "${DATE} ${stream} ${FLAG} $(ls -l $files | awk '{print $9}'| cut -d '/' -f5) ${TIMESTAMP} ${TRANS_TIME} ${TIME_LAG}"
        done



Output:
Code:
26-10-2017-15:24 IUCS LAST TDR-IU-8-139 11:12:23 10:14:59 00:57:24
26-10-2017-15:24 IUCS LAST TDR-IU-8-127 15:22:33	15:14:59 00:07:34
26-10-2017-15:24 IUCS LAST TDR-IU-8-140 15:22:33 15:14:59 00:07:34
26-10-2017-15:24 IUCS LAST TDR-IU-8-59   15:22:33 15:14:59 00:07:34

My script is reading all the files of the directory.
for files in $path/*

instead of it i want to read only last 5 files of the directory

kindly help.

Last edited by MadeInGermany; 10-26-2017 at 08:07 PM.. Reason: added code tags
# 4  
Old 10-26-2017
Code:
echo `ls | tail -5`

Looks okay?
Then replace the echo with the awk command.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 10-26-2017
Output:

Code:
26-10-2017-15:24 	IUCS 	LAST 	TDR-IU-8-139 	11:12:23 		10:14:59 		00:57:24
26-10-2017-15:24 	IUCS 	LAST 	TDR-IU-8-127 	15:22:33	 	15:14:59 		00:07:34
26-10-2017-15:24 	IUCS 	LAST 	TDR-IU-8-140 	15:22:33 	15:14:59 		00:07:34
26-10-2017-15:24 	IUCS 	LAST 	TDR-IU-8-59 		15:22:33 	15:14:59 		00:07:34

---------- Post updated at 04:24 PM ---------- Previous update was at 04:21 PM ----------

Code:
for file in $path/*

its taking all the file in for loop, i need last five files of the directory to do the later calculation

How can i do that, kindly help.

Last edited by Scott; 10-26-2017 at 10:42 PM.. Reason: Please use code tags...
# 6  
Old 10-27-2017
Do exactly what MadeInGermany suggested...

Change:
Code:
for  files in $path/*

to:
Code:
for  files in `ls $path|tail -5`

Although I must admit that I do not understand why you have the inner for loop in your script??? In what way would the output be different if that entire loop was replaced by:
Code:
                TTHEX=`awk -F ',' 'END{print $4}' $files`

# 7  
Old 10-27-2017
Attention: ls omits the path,
so in the loop you must prepend it to the loop variable like "$path/$file"
Or you do
Code:
for  files in `printf "%s\n" $path/* | tail -5`

--
Yes, the inner loop looks odd - certainly not yet ready...
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to fetch matched records from files between two different directory?

awk 'NR==FNR{arr;next} $0 in arr' /tmp/Data_mismatch.sh /prd/HK/ACCTCARD_20160115.txt edit by bakunin: seems that one CODE-tag got lost somewhere. i corrected that, but please check your posts more carefully. Thank you. (5 Replies)
Discussion started by: suresh_target
5 Replies

2. Shell Programming and Scripting

How to read a particular records from a file?

Hi All Can anybody let me know the code to read a particular record from a file For e.g. File Name: File.txt File content: Script_path=/abc/def/script/ File_path=/xyz/data/ Business Date=19990905 SERVER_NAME=Server DATABASE_NAME=Database Login=NewUser Password=NewPassword ... (3 Replies)
Discussion started by: Siddhartha9833
3 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. UNIX for Dummies Questions & Answers

Read all files in a directory for a unix command

Hello, Below, I have a unix command, which can be executable for single file. cat input.txt | sort -k3,3 > output.txt I have 100 input files in a directory. It is hectic and time taking to run the above command for all the 100 files for 100 times. Now, I want to execute the above unix... (2 Replies)
Discussion started by: koneru_18
2 Replies

5. Shell Programming and Scripting

How to read and append certain files form directory

Hi ,i have a question ,if I am in sertain directory and I have 4 files *.c how can I read and append this files in file backup.bac Thanks find ./ -name "*.csh" | wc -l (2 Replies)
Discussion started by: lio123
2 Replies

6. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

7. Shell Programming and Scripting

how to read next records

Hello friends, I am newbie in programing. I am facing some problems in awk. Please help me. I have a file with many data sets. Each data set is separated by an empty line. For example Col1 Col2 Col3 Col4 Col5 0.85 0.07 Fre 42:86 25 0.73 0.03 frp 21:10 28 0.64 0.04 Fre 42:86 63 0.47 0.08... (2 Replies)
Discussion started by: ubeejani
2 Replies

8. Shell Programming and Scripting

read files from directory

hi i have a directory /tmp/Satya,it contains 5.FILE 6.FILE 7.FILE i need to read each file , and read its content line by line please help thanks Satya (2 Replies)
Discussion started by: Satyak
2 Replies

9. UNIX for Dummies Questions & Answers

Read Files from a Directory

Hi, I have a requirement where I have get the files from FTP server then delete the files in the FTP server and then store the file name within the file where there is "TR". i.e Filename|TR|20071231|.... Once I finish loading my file I have to archive the files. Is there any way to do it... (2 Replies)
Discussion started by: kiran_418
2 Replies

10. Shell Programming and Scripting

Read from fileList.txt, copy files from directory tree

Hi, hopefully this is a fairly simple Q&A. I have a clean file list of approximately 180 filenames with no directory or slashes in front of the filename nor any extension or dot ".". I would like to read from this list, find these files recursively down through directory trees, copy the files... (1 Reply)
Discussion started by: fxvisions
1 Replies
Login or Register to Ask a Question

Featured Tech Videos