doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

ARC20190311150800.mp4

Sat Mar 16, 2019 12:43 pm

I have set up a NAS to which I send files (both .mp4 and .jpg) from my security cameras.
Currently I move the ;.jpg' files from the main directory to another directory called 'jpgFiles', if the files are older than 7 days, but the search for these files is taking longer and longer each day it runs as more files of both type keep loading into the main directory (called 'CCTV') from the security cameras.
the code I currently use to move older files via crontab is

Code: Select all

5 8 * * * find /CCTV/RearOfHouse/ -type f -mtime +2 -name '*.jpg' -execdir mv '{}' /CCTV/RearOfHouse/jpgFiles/ \;
Each file is named in the format as the subject of this thread (starting with ARC, then 4 digits for year, 2 digits for month, 2 digits for day of month, and the rest is the time) with the exception of ending in either .mp4 or .jpg,

I want to archive the lot but to achieve this my problem is :-

1. How do I read out the files individualy first to last and enable me to check the filenames as below.?
2. Once I do this I want to check (split?) the filenames to see if the month and year part of the filename is earlier than this month/year.
3. I will then want to check if I have a subdirectory named monthyear ready to recieve it, If the subdirectory does not exist I want to create it.
4. If the file is earlier I then want to move it to the subdirectory that I have created within this directory.

I will repeat the process for both .mp4 and .jpg files

The reason I want the files moved into their respective months is for archiving and making security checks easier/quicker.

Any pointers would be great
Thanks
WD

Andyroo
Posts: 1954
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: ARC20190311150800.mp4

Sat Mar 16, 2019 5:31 pm

Something like this will create the directory in the format YYYYMM (not tested on a Pi - only Pi Desktop emulated under Fusion):

Code: Select all

year=${file:3:4}
month=${file:7:2}
filedate=${year}${month}
mkdir -p ${filedate}
This will return the current date in YYYYMM (I have assumed you are using the UK locale for this - check first):

Code: Select all

todayyr=$( date +"%Y")
todaymth=$( date+"%m")
today=${today}${todaymn}
Then a check to see if the file date is less than today:

Code: Select all

if [ $[filedate -lt today ]
    mv $file.mp4 $filedate
    mv $file.jpg $filedate
fi
Notes:
I assume the file name is in the variable ${file}
The file(s) is in the current directory
The archive directory are directly below the current directory
I create the directory in YYYYMM format as this sorts and compares better - old old trick from an old pgmr :lol:
I cheat and use the '-p' option in mkdir as it does not return an error if it already exists :lol:
Its waffle code and can be shortened but this way you can try each line rather than one big long command
Nothing like being standard - Uppercase 'Y' for year in numeric and lowercase 'm' for month :shock: :o :shock:
Need Pi spray - these things are breeding in my house...

doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: ARC20190311150800.mp4

Sat Mar 16, 2019 5:38 pm

Andyroo wrote:
Sat Mar 16, 2019 5:31 pm
Something like this will create the directory in the format YYYYMM (not tested on a Pi - only Pi Desktop emulated under Fusion):

Code: Select all

year=${file:3:4}
month=${file:7:2}
filedate=${year}${month}
mkdir -p ${filedate}
This will return the current date in YYYYMM (I have assumed you are using the UK locale for this - check first):

Code: Select all

todayyr=$( date +"%Y")
todaymth=$( date+"%m")
today=${today}${todaymn}
Then a check to see if the file date is less than today:

Code: Select all

if [ $[filedate -lt today ]
    mv $file.mp4 $filedate
    mv $file.jpg $filedate
fi
Notes:
I assume the file name is in the variable ${file}
The file(s) is in the current directory
The archive directory are directly below the current directory
I create the directory in YYYYMM format as this sorts and compares better - old old trick from an old pgmr :lol:
I cheat and use the '-p' option in mkdir as it does not return an error if it already exists :lol:
Its waffle code and can be shortened but this way you can try each line rather than one big long command
Nothing like being standard - Uppercase 'Y' for year in numeric and lowercase 'm' for month :shock: :o :shock:
Thanks, just what I wanted to be pointed towards

WD

doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: ARC20190311150800.mp4

Sun Mar 17, 2019 9:45 am

Andyroo wrote:
Sat Mar 16, 2019 5:31 pm
Something like this will create the directory in the format YYYYMM (not tested on a Pi - only Pi Desktop emulated under Fusion):

Code: Select all

year=${file:3:4}
month=${file:7:2}
filedate=${year}${month}
mkdir -p ${filedate}
This will return the current date in YYYYMM (I have assumed you are using the UK locale for this - check first):

Code: Select all

todayyr=$( date +"%Y")
todaymth=$( date+"%m")
today=${today}${todaymn}
Then a check to see if the file date is less than today:

Code: Select all

if [ $[filedate -lt today ]
    mv $file.mp4 $filedate
    mv $file.jpg $filedate
fi
Notes:
I assume the file name is in the variable ${file}
The file(s) is in the current directory
The archive directory are directly below the current directory
I create the directory in YYYYMM format as this sorts and compares better - old old trick from an old pgmr :lol:
I cheat and use the '-p' option in mkdir as it does not return an error if it already exists :lol:
Its waffle code and can be shortened but this way you can try each line rather than one big long command
Nothing like being standard - Uppercase 'Y' for year in numeric and lowercase 'm' for month :shock: :o :shock:
This is what I have come up with from your code (about to try it today) I have put it in a crontab file.

########################################
10 10 * * * find /CCTV/RearOfHouse/ -type file year=$(file:3:4) month=$(file:7::2) filedate=$(year)&(month) mkdir -p $(filedate)

todayyr=$( date +"%Y")
todaymth=$( date+"%m")
today=${today}${todaymn}

if [ $[filedate -lt today ]
mv $file.mp4 $filedate
mv $file.jpg $filedate
fi
########################################

Cheers
WD

doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: ARC20190311150800.mp4

Sun Mar 17, 2019 11:23 am

doubleudee1 wrote:
Sun Mar 17, 2019 9:45 am
Andyroo wrote:
Sat Mar 16, 2019 5:31 pm
Something like this will create the directory in the format YYYYMM (not tested on a Pi - only Pi Desktop emulated under Fusion):

Code: Select all

year=${file:3:4}
month=${file:7:2}
filedate=${year}${month}
mkdir -p ${filedate}
This will return the current date in YYYYMM (I have assumed you are using the UK locale for this - check first):

Code: Select all

todayyr=$( date +"%Y")
todaymth=$( date+"%m")
today=${today}${todaymn}
Then a check to see if the file date is less than today:

Code: Select all

if [ $[filedate -lt today ]
    mv $file.mp4 $filedate
    mv $file.jpg $filedate
fi
Notes:
I assume the file name is in the variable ${file}
The file(s) is in the current directory
The archive directory are directly below the current directory
I create the directory in YYYYMM format as this sorts and compares better - old old trick from an old pgmr :lol:
I cheat and use the '-p' option in mkdir as it does not return an error if it already exists :lol:
Its waffle code and can be shortened but this way you can try each line rather than one big long command
Nothing like being standard - Uppercase 'Y' for year in numeric and lowercase 'm' for month :shock: :o :shock:
This is what I have come up with from your code (about to try it today) I have put it in a crontab file.

########################################
10 10 * * * find /CCTV/RearOfHouse/ -type file year=$(file:3:4) month=$(file:7::2) filedate=$(year)&(month) mkdir -p $(filedate)

todayyr=$( date +"%Y")
todaymth=$( date+"%m")
today=${today}${todaymn}

if [ $[filedate -lt today ]
mv $file.mp4 $filedate
mv $file.jpg $filedate
fi
########################################

Cheers
WD

Utter Disaster :oops:

Soooooo many errors, trying to run it in shell window:-

[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type file year=$(file:3:4) month=$(file:7:2) filedate=$(year)&(month) mkdir -p $(filedate)
-bash: syntax error near unexpected token `mkdir'
[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type file year=$(file:3:4) month=$(file:7:2) filedate=$(year)&(month)
[1] 28625
-bash: file:3:4: command not found
-bash: month: command not found
-bash: file:7:2: command not found
[email protected]:/var/spool/cron/crontabs $ (mkdir -p $(filedate))-bash: year: command not found
find: Must separate multiple arguments to -type using: ','
House/ -type file year=$(file:3:4) month=$(file:7:2) filedate=$(year)&(month)
-bash: syntax error near unexpected token `find'
[1]+ Exit 1 find /CCTV/RearOfHouse/ -type file year=$(file:3:4) month=$(file:7:2) filedate=$(year)
[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type, file year=$(file:3:4) month=$(file:7:2) filedate=$(year)&(month) mkdir -p $(filedate)
-bash: syntax error near unexpected token `mkdir'
[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type, file, year=$(file:3:4), month=$(file:7:2), filedate=$(year)&(month), mkdir -p $(filedate)
-bash: syntax error near unexpected token `,'
[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type, file, year=$(file:3:4), month=$(file:7:2), filedate=$(year)&(month) mkdir -p $(filedate) -bash: syntax error near unexpected token `mkdir'
[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type, file, year=$(file:3:4), month=$(file:7:2), filedate=$(year)&(month) echo filedate
-bash: syntax error near unexpected token `echo'
[email protected]:/var/spool/cron/crontabs $ find /CCTV/RearOfHouse/ -type, file, year=$(file:3:4), month=$(file:7:2), filedate=$(year)&(month)
[1] 30273
-bash: month: command not found
-bash: file:3:4: command not found
-bash: file:7:2: command not found
[email protected]:/var/spool/cron/crontabs $ -bash: year: command not found


WD

User avatar
rpdom
Posts: 13712
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: ARC20190311150800.mp4

Sun Mar 17, 2019 11:27 am

Firstly, you are supposed to put all of those commands into a file and make it executable, then run it from the command prompt.

Secondly, there is a big difference between

Code: Select all

$(file:3:4)
and

Code: Select all

${file:3:4}
Signature is on holiday.

doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: ARC20190311150800.mp4

Sun Mar 17, 2019 11:50 am

rpdom wrote:
Sun Mar 17, 2019 11:27 am
Firstly, you are supposed to put all of those commands into a file and make it executable, then run it from the command prompt.

Secondly, there is a big difference between

Code: Select all

$(file:3:4)
and

Code: Select all

${file:3:4}
Sorry, my eyesight is going so I mistook curly brackets for straight ones.
I will look up how to put the commands into a file and make it executable from the command prompt. Would the file still be able to run daily automatically via crontab?

Thanks

User avatar
rpdom
Posts: 13712
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: ARC20190311150800.mp4

Sun Mar 17, 2019 12:34 pm

doubleudee1 wrote:
Sun Mar 17, 2019 11:50 am
I will look up how to put the commands into a file and make it executable from the command prompt.
You can use any Linux text editor to create the file and save it where you like. I usually create a folder called "cron" or "crons" and store mine in there.

Making it executable is as simple as typing

Code: Select all

chmod +x your_file_name_here
and for bash shell scripts (as that is what your file would be), it is preferable to have

Code: Select all

#!/bin/bash
as the very first line of the script. That tells the system what is needed to run the file.
Would the file still be able to run daily automatically via crontab?
Yes, just put the full path to the file in the crontab entry.
Signature is on holiday.

doubleudee1
Posts: 61
Joined: Fri Nov 30, 2018 2:39 pm
Location: Wales

Re: ARC20190311150800.mp4

Sun Mar 17, 2019 3:21 pm

Thanks for your help and guidance.
WD

Return to “Beginners”