0

I have a growing directory in my node system. In this directory, I save jpg and avi files from my CCTV DVR. I would like to send the most recently created jpg file to another Linux machine (last in first out method). Would you please advise me on this issue?

  • This seems similar https://unix.stackexchange.com/questions/67539/how-to-rsync-only-new-files –  Sep 15 '18 at 15:04

2 Answers2

1

If you want to do it your self using a script, something like this would do the job:

#!/bin/bash

lastFile="$(find -type f -iname '*.jpg' -printf '%T@ %p\n' | sort -k1 -n | tail -1 | awk '{print $2}')"

# Move it anyway you like, I use scp for demonstration
rsync -au "$lastFile"  me@192.168.200.10:/path/to/files/

# Then remove it, move it or anything else you want
rm -i "$lastFile"

However remember that this script is using modification time, because linux doesn't record the creation time.

find was taken from here.


  • find -type f -iname '*.jpg' -printf '%T@ %p\n' list jpg files with their modification time

  • sort -k1 -n sorts them based on modification time

  • tail -1 we grab the newest
  • awk '{print $2} only keep the file name
Ravexina
  • 2,638
  • 1
  • 19
  • 33
  • Sir, actually I need to send only jpg files, the directory having both jpg and avi files. is there is any way to filter this? – Vishnu v.nair Sep 15 '18 at 06:01
  • @Vishnuv.nair Updated the answer – Ravexina Sep 15 '18 at 06:05
  • Thank u for your help, sir .i have one more doubt if no new file created in the directory it will keep sending the last created files right? any way to override this problem ( i need to keep the files in the original folder after sending it also). – Vishnu v.nair Sep 15 '18 at 10:14
  • @Vishnuv.nair Every time you run it, it only sends the newest file... if there is nothing new then it sends the last one it already has sent. – Ravexina Sep 15 '18 at 10:19
  • any way to avoid sending the same files, if nothing new created? – Vishnu v.nair Sep 17 '18 at 04:27
  • @Vishnuv.nair You can use rsync -au instead of scp, (I updated the answer), now it only update files that are newer in the original directory. You can also accept my answer by clicking on the gray tick if you think it was hep full to you :) – Ravexina Sep 17 '18 at 07:42
0

With zsh:

scp ./**/*.jpg(D.om[1]) host:

om sorts by last-modification time, which could be seen as the creation time of the content of the file.

Some tools like to set the modification time of pictures to the time the picture was taken (when that information is available in the image metadata as put there by the camera for instance).

You can also use oc instead of om which will sort based on change time. That's one timestamp that cannot be set arbitrarily and updated every time anything (data or metadata) is changed about the file.

  • I have another doubt, I would like to save the last file created time in MySql DB of my another Linux machine (Act as a server ). How could I transfer this information to the DB without installing MySQL in my first node – Vishnu v.nair Feb 28 '19 at 14:15
  • @Vishnuv.nair, please ask a separate question for that. In any case, you'll need to use a mysql client, whether it's on this machine or the server to update the DB. – Stéphane Chazelas Feb 28 '19 at 14:32