#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Find the newest file in two directories


    Hi i have two directories and files inside them

    Directory 1:
    song.mp3 2013-04-30 16:12:46
    work.txt 2011-10-23 10:53:22
    Directory 2:
    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:

    song.mp3 from Directory 1
    and
    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,396
    Rep Power
    1688
    Depending on what you want, try:
    Originally Posted by CONDITIONAL EXPRESSIONS from man sh
    file1 -nt file2

    True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    I tried that, but i don't enter filenames, it must compare ALL files in both directories in loop or something like that, and then print the newest from compared files.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,396
    Rep Power
    1688
    'All' you need do then is to loop over the files in both directories doing the comparison, maybe with suitable checking to see if a same-named file exists and doing something with the output to drop duplicate results.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    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 ;/
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,396
    Rep Power
    1688
    Check what $f1 and $f2 actually contain - I suspect that it may be just the filename and no path information.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    Ok, i solved this problem and now everything working fine
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,396
    Rep Power
    1688
    Good to hear!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo