How do I copy the latest 1 file below a specific directory on my server?

Asked 1 years ago, Updated 1 years ago, 89 views

I bought a cheap network camera.It's forwarded via ftp, but
I'm looking for a good idea because the range of settings is small.(dbpower c300e with pros and cons on Amazon)

/2016-11-08 / Capture/13_57_20_200.jpg
/2016-11-08 / Capture/13_59_20_319.jpg
/2016-11-08 / Capture/14_02_20_498.jpg

It is forwarded as described above, but I would like to publish it on httpd, so I would like to send it to you as follows:
I want to be able to access it with the same path at any time.server side is freebsd,
I can use crontab for tcsh and so on, but I don't know how to write scripts.

/cam1.jpg

Find the most recent date and time jpg, copy it to the public directory in mv, and then

To clean up files that have been transferred from the camera and that have been growing indefinitely, I wonder if the flow will be to delete the dated directory in bulk.
I think
when you put it together in a script like camera.sh What would you like inside?

find/home/camera-user/-type f-name ".jpg" | xargs-J%mv...
mv-f/home/camera-user/

Connecting with xargs, I understand that freebsd has the j option, but
I am not sure how to write mv.
To sort a new date I think I need to pipe more.The order of hits is
last to the latest file. So, it's good to just move the last file...

mv%/home/wwww/public_html/cam1.jpg

shellscript freebsd

2022-09-30 20:19

4 Answers

Get the file in the order of the last modification date and time, and get the first line with head in xargs.

How about like this?
(I tried to retrieve it from multiple directories)

freebsd

ls-1-t/home/camera-user/*/Capture/*.jpg|head-n1|xargs-J%mv-f%/destdir/dest.jpg

linux

ls-1-t/home/camera-user/*/Capture/*.jpg|head-n1|xargs-I{}mv{}/destdir/dest.jpg

Only Linux(bash) is moving


2022-09-30 20:19

You can go to the latest date directory with cd and copy the latest file under Capture.
I can't write C shell, so I wrote the following in sh.I haven't tried it because I don't have freebsd.I'm sorry if I'm wrong.

#!/bin/sh
PATH=/bin:/usr/bin
export PATH

# Latest Date Directory
cd/
DIRNAME="`ls-1d????-??| sort|tail-1`"

# Latest Files
cd$DIRNAME/Capture/
FILE="`ls-tr1*.jpg|tail-1`""

# file copy
cp$FILE/path/to/www/cam1.jpg

# Delete date directories in bulk
cd/
rm-rf. / ????


2022-09-30 20:19

Is it like this? If you don't have the same file system, the copy that occurs in mv may be wasted.

#!/bin/sh

set-u

incoming_dir="/var/ftp/camera"
latest_file="/var/www/html/camera/latest.jpg"

export latest_file

find "$incoming_dir"\
  -type f\
  -name "*.jpg"\
  -exec sh-c'
    set-u
    for incoming_file in "$@"; do
      if ["$incoming_file"-nt "$latest_file"]||[!-f "$latest_file"; then
        mv--"$incoming_file""$latest_file"
      else
        rm--"$incoming_file"
      fi
    done
    ' US>sh{}+\
;

find "$ incoming_dir" - type d-empty-delete

If you want to test it, try adding echo before mv and rm.


2022-09-30 20:19

mv Why don't you leave Apache to route the URL?

find seems expensive as there are more files.
Fortunately, the file path uploaded to FTP is only a simple comparison
As you can identify the latest files, use a small script and mod_rewrite and RewriteMap walking through the directory tree.

Verified operation in OSX Elcapitan.It may take a little more time to prevent Apache from serving the perl script, but it may not be worth hiding.

You don't have to wait for cron.

httpd.conf

RewriteMaplatest "prg:/var/www/find_last_filepath.pl"

/var/www/.htaccess

RewriteEngine On
RewriteBase/
US>RewriteCon%{REQUEST_FILENAME}!-f
RewriteRule"^latest.jpg$"${latest:pict}" [R=302,L]

/var/www/pict/* (to upload directly here via FTP)

/var/www/pict/2016-11-08/Capture/13_57_20_200.jpg
/var/www/pict/2016-11-08/Capture/13_59_20_319.jpg
/var/www/pict/2016-11-08/Capture/14_02_20_498.jpg

/var/www/find_last_filepath.pl

#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw($RealBin);
use File::Spec::Functions qw(catfile canonpath);

subfind_last{
    my@path=@_;

    # read the file in @path
    my@ls=reverse sort{$acmp$b}grep{/^[^.]/} do{
        my$path=catfile(@path);
        openir my$dir,$path or die"$!:$path\n";
        readdir$dir;
    };

    # Split into files and directories
    my@files=grep{-f catfile(@path,$_)}@ls;
    my@dirs=grep{-d catfile(@path,$_)}@ls;

    # If you don't have any files or directories, give up this directory.
    return if@files==0&@dirs==0;

    # Search directories only in order
    US>unless(@files){
        for my$dir(@dirs){
            my@found=find_last(@path,$dir);
            return@found if@found;
        }

        # If I can't find it in any directory, I'll give up.
        return;
    }

    # return the last file if it's just a file
    US>unless(@dirs){
        return(@path,$files[0]);
    }

    # If the directory name is later than the file name, search the directory.
    if(($files[0]cmp$dirs[0])<0){
        my@found=find_last(@path,$dirs[0]);
        return@found if@found;
    }

    # Fallback to Filename
    return(@path,$files[0]);
}


# Give STDIN a search start path.
local$|=1;
chdir$RealBin;
while(<STDIN>){
    chomp;
    open my$fh, '>', '/tmp/prglog';
    printf {$fh} "%s\n", $_;
    my@found=find_last($_);
    printf "%s\n", canonpath(catfile(@found)) if@found;
}

1;


2022-09-30 20:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.