Get the newest file in a directory.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the newest file in a directory.
# 1  
Old 08-16-2010
Get the newest file in a directory.

I am new to shell scripting so i need some help need how to go about with this problem.

I have a directory which contains files in the following format. The files are in a diretory called /incoming/external/data

Code:
    AA_20100806.dat
    AA_20100807.dat
    AA_20100808.dat
    AA_20100809.dat
    AA_20100810.dat
    AA_20100811.dat
    AA_20100812.dat

As you can see the filename of the file includes a timestamp. i.e. [RANGE]_[YYYYMMDD].dat

What i need to do is find out which of these files has the latest date using the timestamp that is part of the filename not the system timestamp and move it to another directory and move the rest to a different directory.
# 2  
Old 08-16-2010
Assuming your RANGE is always AA

ls -1| tail -1 will give you the latest file.
vshar
# 3  
Old 08-17-2010
Hi thanks for that. It seems to work but i cant work out how.
I looked at the manual for ls and it says that -1 "Prints one entry per line of output".

At what point does the sorting occur?
# 4  
Old 08-17-2010
Quote:
Originally Posted by ziggy25
Hi thanks for that. It seems to work but i cant work out how.
I looked at the manual for ls and it says that -1 "Prints one entry per line of output".

At what point does the sorting occur?
Hi.

The sort order of ls is alphabetical by default.

By using -1 it does print one entry per line, then tail -1 will take the last one (i.e. the newest by timestamp).

But you don't even need the -1:

Code:
$ touch AA_20100806.dat AA_20100807.dat AA_20100808.dat AA_20100809.dat AA_20100810.dat AA_20100811.dat AA_20100812.dat
$ ls
AA_20100806.dat	AA_20100807.dat	AA_20100808.dat	AA_20100809.dat	AA_20100810.dat	AA_20100811.dat	AA_20100812.dat
$ ls | tail -1
AA_20100812.dat

# 5  
Old 08-17-2010
Oh ok i get it now. Thanks for you help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all but newest of given file name type

I use this to make it easier to see when a backup script ran. touch /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/$( date '+%m-%d-%Y_%I:%M-%p' ) I would like a script that would delete all but the newest file ONLY of this type 08-20-2018_01:24-PM (4 Replies)
Discussion started by: drew77
4 Replies

2. Shell Programming and Scripting

Find file by filename or with newest modified date

Hi, I have a directory that has numerous files in it, and there is two which are named "filerec_ddmmyyHH24MMSS" by the time they are created so "filerec_010615012250" was created at 01:22:50 on 1st June 2015. I need to find the most recently created of those 2 files and get the contents of... (4 Replies)
Discussion started by: finn
4 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Find the newest file in two directories

Hi i have two directories and files inside them Directory 1: Directory 2: These files are the same but song.mp3 in Directory 1 is newer than song.mp3 in Directory 2, and work.txt in Directory 2 is newer than work.txt in Directory 1. Now is my question. How i can compare these files... (10 Replies)
Discussion started by: Falstaff
10 Replies

4. Shell Programming and Scripting

Grabbing the newest file, cleaner method?

Greetings, I'm doing a process whereby I need to search for all filenames containing a given bit of text and grab the newest file from what may be 20 results. In a script I'm writing, i've got a monster line to do the sort as follows: find /opt/work/reports/input -name "*$searchtarget*" |... (4 Replies)
Discussion started by: Karunamon
4 Replies

5. Shell Programming and Scripting

Writing Script to Copy Newest Directory

I am trying to write a script that once executed it will search within a directory and copy only the newest directory that has not been copied before to a new location. Kind of like what ROBOCOPY /M does in windows? The directories are not left in the new location so using a sync action won't... (2 Replies)
Discussion started by: Keriderf
2 Replies

6. Shell Programming and Scripting

Script to check for the newest file mutiple times a day and SCP it to another server.

Hi, I need a sample of a script that will check a specific directory multiple times throughout the day, and scp the newest file to another server. Example: current file is misc_file.txt_02272011 (the last part is the date), once that has been secure copied, another one may come in later the... (1 Reply)
Discussion started by: richasmi
1 Replies

7. Shell Programming and Scripting

Mail file size of newest file in directory

I have been a long time lurker, and have learned a lot from these forums, thank you to everyone. I am using Zoneminder to record a security camera feed. No motion detection, just 24 hour recording. I then have a script that checks Mysql for events dated the day before, and throws them at... (4 Replies)
Discussion started by: iamVERYhungry
4 Replies

8. Shell Programming and Scripting

finding the 5 newest files in a directory

I have a folder that has new files created everyday, is there a way i can filter them so that only the 5 newest files are displayed? I'm hoping this can be done by a one liner without a script. for example my directory contains -rw-r--r-- 1 root root 0 Jun 24 08:34 file112 -rw-r--r-- 1 root... (2 Replies)
Discussion started by: zerofire123
2 Replies

9. Homework & Coursework Questions

Newest file changed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The program should search all files in current directory and it's subdirectories and list out the newest file by... (8 Replies)
Discussion started by: petel1
8 Replies

10. Shell Programming and Scripting

Script to cd into newest (dated) FTP directory

I have written the following simple bash script. It logs onto an FTP site and then CDs into a directory that corresponds to the last business day. So for example the directory I would access today is 20060110 (2006 jan 10). I am currently taking today's date and subtracting 1, putting this... (3 Replies)
Discussion started by: steved
3 Replies
Login or Register to Ask a Question