Using ls and stat [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have written this script, however the output is not correct. It returns stat cannot stat no such file or directory. The file format is Living Room-20180418-0955588134.jpg



Any help will be appreciated.



#!/bin/sh

LASTFILE=$(cd /volume1/surveillance/@Snapshot && ls *.jpg | tail -1)



# Input file

# How many seconds before file is deemed "older"
OLDTIME=3600
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat "$LASTFILE" -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then

echo "No Movement Dectected in Last Hour" ;
exit 1
fi






share|improve this question













closed as off-topic by Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher Apr 18 at 18:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    You are getting filenames from a different directory than what you run stat in. Also don't parse the output from ls.
    – Kusalananda
    Apr 18 at 9:45














up vote
0
down vote

favorite












I have written this script, however the output is not correct. It returns stat cannot stat no such file or directory. The file format is Living Room-20180418-0955588134.jpg



Any help will be appreciated.



#!/bin/sh

LASTFILE=$(cd /volume1/surveillance/@Snapshot && ls *.jpg | tail -1)



# Input file

# How many seconds before file is deemed "older"
OLDTIME=3600
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat "$LASTFILE" -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then

echo "No Movement Dectected in Last Hour" ;
exit 1
fi






share|improve this question













closed as off-topic by Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher Apr 18 at 18:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    You are getting filenames from a different directory than what you run stat in. Also don't parse the output from ls.
    – Kusalananda
    Apr 18 at 9:45












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have written this script, however the output is not correct. It returns stat cannot stat no such file or directory. The file format is Living Room-20180418-0955588134.jpg



Any help will be appreciated.



#!/bin/sh

LASTFILE=$(cd /volume1/surveillance/@Snapshot && ls *.jpg | tail -1)



# Input file

# How many seconds before file is deemed "older"
OLDTIME=3600
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat "$LASTFILE" -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then

echo "No Movement Dectected in Last Hour" ;
exit 1
fi






share|improve this question













I have written this script, however the output is not correct. It returns stat cannot stat no such file or directory. The file format is Living Room-20180418-0955588134.jpg



Any help will be appreciated.



#!/bin/sh

LASTFILE=$(cd /volume1/surveillance/@Snapshot && ls *.jpg | tail -1)



# Input file

# How many seconds before file is deemed "older"
OLDTIME=3600
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat "$LASTFILE" -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then

echo "No Movement Dectected in Last Hour" ;
exit 1
fi








share|improve this question












share|improve this question




share|improve this question








edited Apr 18 at 9:53
























asked Apr 18 at 9:40









Colin Davis

41




41




closed as off-topic by Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher Apr 18 at 18:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher Apr 18 at 18:54


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Kusalananda, Stephen Kitt, Jeff Schaller, Timothy Martin, Christopher
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 3




    You are getting filenames from a different directory than what you run stat in. Also don't parse the output from ls.
    – Kusalananda
    Apr 18 at 9:45












  • 3




    You are getting filenames from a different directory than what you run stat in. Also don't parse the output from ls.
    – Kusalananda
    Apr 18 at 9:45







3




3




You are getting filenames from a different directory than what you run stat in. Also don't parse the output from ls.
– Kusalananda
Apr 18 at 9:45




You are getting filenames from a different directory than what you run stat in. Also don't parse the output from ls.
– Kusalananda
Apr 18 at 9:45










2 Answers
2






active

oldest

votes

















up vote
1
down vote













With GNU find or compatible:



if
! find /volume1/surveillance/@Snapshot -name '*.jpg' -mmin -60 |
grep -q '^'
then
echo No movement detected in the last hour
exit 1
fi


Or with zsh:



last_hour=(/volume1/surveillance/@Snapshot/*.jpg(Nmh-1))
if (($#last_hour = 0)); then
echo No movement detected in the last hour
exit 1
fi





share|improve this answer




























    up vote
    0
    down vote













    The reason is because "stat" doesn't see the full path "/volume1/surveillance/@Snapshot/". It just sees the filename. So you need to modify the
    script.



    #!/bin/sh
    DIR=/volume1/surveillance/@Snapshot
    LASTFILE=$(cd $DIR && ls *.jpg | tail -1)

    # Input file

    # How many seconds before file is deemed "older"
    OLDTIME=3600
    # Get current and file times
    CURTIME=$(date +%s)
    FILETIME=$(stat $DIR/$LASTFILE -c %Y)
    TIMEDIFF=$(expr $CURTIME - $FILETIME)

    # Check if file older
    if [ $TIMEDIFF -gt $OLDTIME ]; then

    echo "No Movement Dectected in Last Hour" ;
    exit 1
    fi





    share|improve this answer





















    • Why not replace the whole script with find dir -type f -mtime +1 or something similar?
      – Kusalananda
      Apr 18 at 12:37










    • Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
      – Buddika
      Apr 18 at 12:43

















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    With GNU find or compatible:



    if
    ! find /volume1/surveillance/@Snapshot -name '*.jpg' -mmin -60 |
    grep -q '^'
    then
    echo No movement detected in the last hour
    exit 1
    fi


    Or with zsh:



    last_hour=(/volume1/surveillance/@Snapshot/*.jpg(Nmh-1))
    if (($#last_hour = 0)); then
    echo No movement detected in the last hour
    exit 1
    fi





    share|improve this answer

























      up vote
      1
      down vote













      With GNU find or compatible:



      if
      ! find /volume1/surveillance/@Snapshot -name '*.jpg' -mmin -60 |
      grep -q '^'
      then
      echo No movement detected in the last hour
      exit 1
      fi


      Or with zsh:



      last_hour=(/volume1/surveillance/@Snapshot/*.jpg(Nmh-1))
      if (($#last_hour = 0)); then
      echo No movement detected in the last hour
      exit 1
      fi





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        With GNU find or compatible:



        if
        ! find /volume1/surveillance/@Snapshot -name '*.jpg' -mmin -60 |
        grep -q '^'
        then
        echo No movement detected in the last hour
        exit 1
        fi


        Or with zsh:



        last_hour=(/volume1/surveillance/@Snapshot/*.jpg(Nmh-1))
        if (($#last_hour = 0)); then
        echo No movement detected in the last hour
        exit 1
        fi





        share|improve this answer













        With GNU find or compatible:



        if
        ! find /volume1/surveillance/@Snapshot -name '*.jpg' -mmin -60 |
        grep -q '^'
        then
        echo No movement detected in the last hour
        exit 1
        fi


        Or with zsh:



        last_hour=(/volume1/surveillance/@Snapshot/*.jpg(Nmh-1))
        if (($#last_hour = 0)); then
        echo No movement detected in the last hour
        exit 1
        fi






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Apr 18 at 12:37









        Stéphane Chazelas

        279k53514846




        279k53514846






















            up vote
            0
            down vote













            The reason is because "stat" doesn't see the full path "/volume1/surveillance/@Snapshot/". It just sees the filename. So you need to modify the
            script.



            #!/bin/sh
            DIR=/volume1/surveillance/@Snapshot
            LASTFILE=$(cd $DIR && ls *.jpg | tail -1)

            # Input file

            # How many seconds before file is deemed "older"
            OLDTIME=3600
            # Get current and file times
            CURTIME=$(date +%s)
            FILETIME=$(stat $DIR/$LASTFILE -c %Y)
            TIMEDIFF=$(expr $CURTIME - $FILETIME)

            # Check if file older
            if [ $TIMEDIFF -gt $OLDTIME ]; then

            echo "No Movement Dectected in Last Hour" ;
            exit 1
            fi





            share|improve this answer





















            • Why not replace the whole script with find dir -type f -mtime +1 or something similar?
              – Kusalananda
              Apr 18 at 12:37










            • Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
              – Buddika
              Apr 18 at 12:43














            up vote
            0
            down vote













            The reason is because "stat" doesn't see the full path "/volume1/surveillance/@Snapshot/". It just sees the filename. So you need to modify the
            script.



            #!/bin/sh
            DIR=/volume1/surveillance/@Snapshot
            LASTFILE=$(cd $DIR && ls *.jpg | tail -1)

            # Input file

            # How many seconds before file is deemed "older"
            OLDTIME=3600
            # Get current and file times
            CURTIME=$(date +%s)
            FILETIME=$(stat $DIR/$LASTFILE -c %Y)
            TIMEDIFF=$(expr $CURTIME - $FILETIME)

            # Check if file older
            if [ $TIMEDIFF -gt $OLDTIME ]; then

            echo "No Movement Dectected in Last Hour" ;
            exit 1
            fi





            share|improve this answer





















            • Why not replace the whole script with find dir -type f -mtime +1 or something similar?
              – Kusalananda
              Apr 18 at 12:37










            • Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
              – Buddika
              Apr 18 at 12:43












            up vote
            0
            down vote










            up vote
            0
            down vote









            The reason is because "stat" doesn't see the full path "/volume1/surveillance/@Snapshot/". It just sees the filename. So you need to modify the
            script.



            #!/bin/sh
            DIR=/volume1/surveillance/@Snapshot
            LASTFILE=$(cd $DIR && ls *.jpg | tail -1)

            # Input file

            # How many seconds before file is deemed "older"
            OLDTIME=3600
            # Get current and file times
            CURTIME=$(date +%s)
            FILETIME=$(stat $DIR/$LASTFILE -c %Y)
            TIMEDIFF=$(expr $CURTIME - $FILETIME)

            # Check if file older
            if [ $TIMEDIFF -gt $OLDTIME ]; then

            echo "No Movement Dectected in Last Hour" ;
            exit 1
            fi





            share|improve this answer













            The reason is because "stat" doesn't see the full path "/volume1/surveillance/@Snapshot/". It just sees the filename. So you need to modify the
            script.



            #!/bin/sh
            DIR=/volume1/surveillance/@Snapshot
            LASTFILE=$(cd $DIR && ls *.jpg | tail -1)

            # Input file

            # How many seconds before file is deemed "older"
            OLDTIME=3600
            # Get current and file times
            CURTIME=$(date +%s)
            FILETIME=$(stat $DIR/$LASTFILE -c %Y)
            TIMEDIFF=$(expr $CURTIME - $FILETIME)

            # Check if file older
            if [ $TIMEDIFF -gt $OLDTIME ]; then

            echo "No Movement Dectected in Last Hour" ;
            exit 1
            fi






            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Apr 18 at 12:34









            Buddika

            1364




            1364











            • Why not replace the whole script with find dir -type f -mtime +1 or something similar?
              – Kusalananda
              Apr 18 at 12:37










            • Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
              – Buddika
              Apr 18 at 12:43
















            • Why not replace the whole script with find dir -type f -mtime +1 or something similar?
              – Kusalananda
              Apr 18 at 12:37










            • Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
              – Buddika
              Apr 18 at 12:43















            Why not replace the whole script with find dir -type f -mtime +1 or something similar?
            – Kusalananda
            Apr 18 at 12:37




            Why not replace the whole script with find dir -type f -mtime +1 or something similar?
            – Kusalananda
            Apr 18 at 12:37












            Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
            – Buddika
            Apr 18 at 12:43




            Yes. your method is the perfect one but the OP wants to get the last file. "LASTFILE=$(cd $DIR && ls *.jpg | tail -1)"
            – Buddika
            Apr 18 at 12:43


            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Christian Cage

            How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?