How to retrieve the latest files from the directory.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to retrieve the latest files from the directory.?
# 1  
Old 05-08-2013
How to retrieve the latest files from the directory.?

hi,

i have some file in a directory say

Code:
p1.txt.201305051200.lst
p1.txt.201305051300.lst
p1.txt.201306051200.lst
p1.txt.201306051300.lst
p2.txt.201306051200.lst
p2.txt.201306051300.lst

i am using p* pattern to retrieve these file

Code:
ls -1 p*

the files in red color are the latest file of p1 and p2

i want to take the latest filenames of both p1 and p2 and write it to a file say filelist.txt.

can anyone give a code to retrieve the latest files of p1 and p2.
# 2  
Old 05-08-2013
Latest by file arrived time? Or latest by ordering?
ls -1 wont list files as per file name sorting..
# 3  
Old 05-08-2013
latest by arrived time.

---------- Post updated at 01:32 PM ---------- Previous update was at 01:30 PM ----------

but i want latest of both p1 and p2.
# 4  
Old 05-08-2013
Try below..

Code:
 
ls -tr1 p*|awk -F"." '{A[$1]=$0}END{for(i in A){print A[i]}}'

This User Gave Thanks to vidyadhar85 For This Post:
# 5  
Old 05-08-2013
ya it worked.
can you explain me the awk part of the command.
# 6  
Old 05-08-2013
Quote:
Originally Posted by Little
ya it worked.
can you explain me the awk part of the command.
I am storing the file name as per p1,p2,p3 so on..

A[p1]=first file start with p1
if it finds second file with p1 it will overwrtie the content so u always left with latest filename as we are feeding ls -lt1 feed to awk. the same applies to A[p2], A[p3] etc...
# 7  
Old 05-08-2013
will the above command work if i am using *.lst instead of p*

Code:
ls -tr1 *.lst|awk -F"." '{A[$1]=$0}END{for(i in A){print A[i]}}'

---------- Post updated at 01:54 PM ---------- Previous update was at 01:45 PM ----------

in the above code , you are dividing the file name in 4 fields
Code:
$1=p1
$2=txt
$3=201305051200
$4=lst

is it correct?

and then you are using

Code:
A[p1]=$0

, here $0 represents the full line if i am not wrong. i.e.

Code:
A[p1]=p1.txt.201305051200.lst

why in END used?

---------- Post updated at 02:14 PM ---------- Previous update was at 01:54 PM ----------

can you use full file name as index like p1.txt instead of just p1.
because if there are file like

Code:
p1.txt.201306051200.lst
p1.201306051200.lst

now there are two different files name one is p1.txt and the other is only p1.
so the above code just picks only one of the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Retrieve the Latest file in a folder using SFTP

HI Guys, Can anyone help me to retrieve the latest file from below example using SFTP I have below list of files in a folder v403t.lstprgms.sortin1 val027.d099.fwest.oct2711 xcelrptd.d1400sqp.dec1713.t040459.@02del xcelrptd.d1400sqp.dec1713.t073308.@02del... (3 Replies)
Discussion started by: heye18
3 Replies

2. Shell Programming and Scripting

Find the latest directory and loop through the files and pick the error messages

Hi, I am new to unix and shell scripting,can anybody help me in sctipting a requirement. my requirement is to get the latest directory the name of the directory will be like CSB.monthdate_time stamp like CSB.Sep29_11:16 and CSB.Oct01_16:21. i need to pick the latest directory. in the... (15 Replies)
Discussion started by: sudhir_83k
15 Replies

3. Shell Programming and Scripting

find the latest files in multiple directory

I want to get the latest files from multiple directories, d1, d2,d3 and d4 under the parent dierectoy d. can anyone help out with this? thx (3 Replies)
Discussion started by: shyork2001
3 Replies

4. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

5. UNIX for Dummies Questions & Answers

User cannot retrieve email once his /home directory was moved

Hi, We run an IMAP server at work. I had to move the home directory of one user to another partition. I updated his account in /etc/passwd. For some reason his Microsoft Outlook account cannot rertrieve his new emails. I check /var/spool/mail and his emails are there... Any advice? (1 Reply)
Discussion started by: mojoman
1 Replies

6. Shell Programming and Scripting

Copying a directory structure with the latest versions of files

Hello I have three directory structures for code releases. Each directory structure looks like this: bash-3.00$ ls -R | more .: Test_Release_1 Test_Release_2 Test_Release_3 ./Test_Release_1/dbcode: rp_online_import_srdp.pkb-1 srdp_ar_validation.pkb-1... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

7. Shell Programming and Scripting

How to retrieve all the linked script files/ctl files/sql files?

Hi I am going to migrate our datawarehouse system from HP Tru 64 Unix to the Red Hat Linux. Inside the box, it is running around 40 cron jobs; inside each cron job, it is calling other shell script files, and the shell script files may again call other shell script files or ctl files(for... (1 Reply)
Discussion started by: franksubramania
1 Replies

8. UNIX for Dummies Questions & Answers

how to retrieve sub directorires under a specified directory

Hello, i need to retrieve ALL subdirectories under a specified directroy in my C++ program. Could anyone please tell me how to do this. Thanks (2 Replies)
Discussion started by: cy163
2 Replies

9. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies

10. Shell Programming and Scripting

ksh: How to get latest file from a list of files in a directory

Hi, I need to get the latest file from a list of files in a particular directory. Please could anyone help me out to get the file. Thank you, - Jay. (1 Reply)
Discussion started by: Jayathirtha
1 Replies
Login or Register to Ask a Question