[Solved] Find the newest file in two directories


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Find the newest file in two directories
# 1  
Old 12-05-2013
[Solved] Find the newest file in two directories

Hi i have two directories and files inside them

Directory 1:
Quote:
song.mp3 2013-04-30 16:12:46
work.txt 2011-10-23 10:53:22
Directory 2:
Quote:
song.mp3 2010-05-20 23:23:54
work.txt 2012-01-22 15:42:22
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 and print the newest from both folders:

Quote:
song.mp3 from Directory 1
and
Quote:
work.txt form Directory 2
I've been tried to do this by find command, loop, and other but nothing works. Could someone help me with this ?
# 2  
Old 12-05-2013
So, do the same files exist in both directories?
Are you only looking at two directories to determine which is the most current?

If so, I am thinking to simply 'cut' the first column from file one to get a list of items to search.
Then, process through that list to determine which directory has the most recent file.
# 3  
Old 12-05-2013
Quote:
Originally Posted by joeyg
So, do the same files exist in both directories?
Are you only looking at two directories to determine which is the most current?

If so, I am thinking to simply 'cut' the first column from file one to get a list of items to search.
Then, process through that list to determine which directory has the most recent file.
yeap, there are the same files but different date of modification.

And i don't know what do you mean about cut out first column. Which column do you mean ?
I must compare files from Directory1 and Directory2 and then print the newest.
# 4  
Old 12-05-2013
OK, a couple things for you to play with:

Code:
ls -1

should simply give you a listing of filenames

And, this link
https://www.unix.com/showthread.php?t=109676
may give you some hints into how to put the date of file into a format that will be simpler to compare
This User Gave Thanks to joeyg For This Post:
# 5  
Old 12-06-2013
ok, thanks i have some ideas right now how to solve my problem, i'll tried Smilie

---------- Post updated 06-12-13 at 09:35 AM ---------- Previous update was 05-12-13 at 02:59 PM ----------

ok tried that

Code:
for f1 in directory1/*
do
    for f2 in directory2/*
    do
       if [ $f1 -nt $f2 ]
       then
       echo "File frome directory1 is newer"
       else
       echo "File frome directory2 is newer"
       fi
    done
done

but doesn't work ;/
# 6  
Old 12-06-2013
Why doesnt it work? ...
Because f1 may not be same as f2 to start with...
You need to compare same files ( names at least.. ) which is not the case here...

So start by that: Compare 2 strings - Are they the same?
If so now you can use -nt test
This User Gave Thanks to vbe For This Post:
# 7  
Old 12-06-2013
Aaa yea i see, ok so now i have that

Code:
for f1 in directory1/*
do
    for f2 in directory2/*
    do
       if [ $f1 = $f2 ] && [ $f1 -nt $f2 ]
       then
          echo "$f1 is newer than $f2"
       else
          echo "$f2 is newer than $f1"
       fi
    done
done

but $f1 show for example "directory1/song.mp3" and I need only "song.mp3". Now i must think how i can get only filename without directory. Or it's bad thinking ?

Last edited by vbe; 12-06-2013 at 12:09 PM.. Reason: indent
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

[Solved] Find entries from one file in another

Please help. I want to read a file line by line (only 1 column) and print all corresponding rows from a second file (2 columns) . Only first column of second file has to be matched with only column of first file. Order of lines in output doesn't matter. Simplified example Input 1 A B... (6 Replies)
Discussion started by: newbie83
6 Replies

3. UNIX for Dummies Questions & Answers

Getting directories resulted from Find in one file

Hi Need help for the following scenario. I am having two directories /tmp/a and /tmp/b. /tmp/a again has subdirectories /tmp/a/aa and /tmp/a/ab. I want to run a script from /tmp/b to search for a file user.lst in the folders /tmp/a/aa and /tmp/a/ab, if available, i want to make a file in... (3 Replies)
Discussion started by: sudeep.id
3 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. UNIX for Dummies Questions & Answers

How to sort output from find from oldest to newest?

Hi, How do I sort the output of find to provide a listing of files from oldest to newest? For example, if I do a find /tmp -type f -print I want the output to be sorted in the order of the oldest to the newest file. Thanks in advance. (2 Replies)
Discussion started by: newbie_01
2 Replies

6. Shell Programming and Scripting

How to find the recent file in many sub-directories?

Hi guys, Under my root directory there are many sub-directories which contains log file for every day of running. How can I find , in one command only, the recent log file in each sub-directory? For example, If I run the following: find . -name "exp_prod_*_*_yes_*_.log" -exec ls -ltr {} \;... (12 Replies)
Discussion started by: nir_s
12 Replies

7. Shell Programming and Scripting

Find Directories That Do Not Contain A Certain File

I'm having problems figuring out the process to find directories that DO NOT contain a certain file. I have a mp3 collection that all the album art is name "folder.jpg". Not all the albums have images. I need a way to find the albums/directories that do not contain "folder.jpg". I can find the... (2 Replies)
Discussion started by: subsonic
2 Replies

8. Shell Programming and Scripting

find directories that do NOT contain a certain file extension

for x in `find /vmfs/volumes/v01tstn01a01/ -type d`; do find $x -name '*.vmx' > /dev/null || echo $x; done; The goal of this is to find the subdirectories that do NOT contain a file with the extension of .vmx Any help would be great! (6 Replies)
Discussion started by: hodnov
6 Replies

9. Shell Programming and Scripting

Find a file in sub-directories.. o/p just the path

Hello All, I am somehow stumped with this ting. 'Find' will sure show me.. but I want only thepath of all the occurences of the file in any of the sub-dirs.. Any help will be sincerely appreciated. thanx! (3 Replies)
Discussion started by: pranavagarwal
3 Replies

10. Programming

to find a file in set of directories

Hi, what is the command in unix to find a file abc.c in a directory which had n number of sub-directories. thanx (3 Replies)
Discussion started by: jazz
3 Replies
Login or Register to Ask a Question