Last modified file in 2 or more directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Last modified file in 2 or more directories
# 1  
Old 07-15-2008
Last modified file in 2 or more directories

Hi,

Is there any simple way to get the last modified file in a set of 2 or more directories? This should return one file only (not 1 file per directory)

Thanks for your help
# 2  
Old 07-15-2008
Code:
ls -drt /dir1/* /dir2/* | tail -1

# 3  
Old 07-16-2008
Thanks Annihilanic. Would you know how to circumvent the ls: Arg list too long problem ?
# 4  
Old 07-16-2008
I'm presuming there are no subdirectories. It becomes difficult with large numbers of files because you need to use multiple ls commands, therefore you can no longer use its built-in sort-by-time capability. Instead you have to it yourself. This is a little easier if you can be sure that you are only interested in files that were modified in the last 12 months, so you can ignore files whose dates contain a year, e.g.

Code:
find dir1 dir2 -type f | xargs ls -l | awk '$8 ~ /:/' | sort -k 6,6M -k 7,7n | tail -1 | awk '{print $NF}'

Also hopefully your version of sort supports the M sort key (i.e. months), otherwise you have to fiddle some more.

Last edited by Annihilannic; 07-16-2008 at 05:23 AM.. Reason: forgot the tail bit
# 5  
Old 07-16-2008
This is good enough for me, at leat it works with my setup. Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with finding the latest modified version of a file within directories

I am trying to look into multiple directories and pluck out the latest version of a specific file, regardless of where it sits within the directory structure. Ex: The file is a .xls file and could have a depth within the directory of anywhere from 1-5 Working directory - Folder1... (6 Replies)
Discussion started by: co21ss
6 Replies

2. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

3. Shell Programming and Scripting

Bash to monitor Modified Files/Directories

Hi All , I have a directory called "/usr/local/apache/docs/" inside this docs i have below directories , bash-2.05# pwd /usr/local/apache/docs/ bash-2.05#ls -l | less 2 drw-r-xr-x 3 root root 512 Aug 8 2010 Form1 2 drw-r-xr-x 3 root other 512 Mar 8 ... (4 Replies)
Discussion started by: gnanasekar_beem
4 Replies

4. UNIX for Dummies Questions & Answers

Moving Directories Based on Modified date

Hi, How can I move directories (and all sub directories/files) from one directory to another based on the modified date of the directory? Currently the existing structure looks like this: /public_html/media/videos/tmb/34947/image1.jpg /public_html/media/videos/tmb/34947/image2.jpg ... (0 Replies)
Discussion started by: lbargers
0 Replies

5. Shell Programming and Scripting

Comparing the modified dates of files in two directories

Hi Is it possible to compare the modified dates of all the files in two directories using shell script? I would like to take a backup of a directory in production server regularly. Instead of copying all the files in the directory, is it possible to list only the files that are... (2 Replies)
Discussion started by: ashok.k
2 Replies

6. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies

7. Shell Programming and Scripting

How to get a filename modified by attaching modified timestamp

Hi, I want to modify a filename in AIX by attaching the last modified timestamp. I want the timestamp completely in numerical format (eg:200905081210. yr-2009, mnth - 05, date -08, hr - 12, mins - 10). For example if the filename is a.log and it was modified on April 6th 2008 at 21.00. I... (16 Replies)
Discussion started by: Ruks
16 Replies

8. UNIX for Dummies Questions & Answers

Find most recently modified directories

How do I do it? Simple answers preferred... using BASH.. the less code the better. I want to find out where Indesign is caching PDF tmp data ... I figure this is a good way to do it.. either way i wanna know how to do it. (2 Replies)
Discussion started by: glev2005
2 Replies

9. Shell Programming and Scripting

Identifying New and Modified Files/Directories

Hi. Our shop is migrating to a new UNIX server and our hope is to do a full migration of all files to the new server weeks in advance of the final migration. As a result we want to identify files on our SOLARIS 8 UNIX server that have changed or that were created after a specific date & time... (2 Replies)
Discussion started by: buechler66
2 Replies

10. UNIX for Dummies Questions & Answers

how to retrieve original contents of a modified file (modified using vi)

Made changes to a file using vi editor and saved those changes now realised that the changes are not required How can I get the previous version of the file.i.e the one which was there on which I had made changes (3 Replies)
Discussion started by: novice100
3 Replies
Login or Register to Ask a Question