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

Another exe - mkdir question?

Wed Apr 03, 2019 1:35 pm

I have a directory where I am keeping all my executable files it is is /home/pi/ExecFiles, I am the only user of my pi so I thought it would be OK to create and run any exec files I want from there.
I have created one as shown below:-

Code: Select all

###/home/pi/CCTV/RearOfHouse/ARC2019042134139.jpg   = full path inc filename 
##create an array with all the .jpg files found inside ~/CCTV/RearOfHouse/
#!/bin/bash
arr=(~/CCTV/RearOfHouse/*.jpg)

# iterate through array using a counter
for ((i=0; i<${#arr[@]}; i++)); do
    #do something to each element of array
    j=${arr[$i]:9:26}  ###  this sets "j" to equal "CCTV/RearOfHouse/ARC201904"
     echo  "${j}"  ##just to check I am right
###  mkdir -p "${j}"
     echo "${arr[$i]:29:6}"     #gives the year and month
#     echo "${arr[$i]}"  #gives the full path including filename
#    echo "${arr[$i]:0:5}" #gives the output '/home'
done

mkdir -p "${j}"  ###  but this creates Dirs in location of this exec file (/home/pi/ExecFiles)?

How do I alter the code to make a directory where the bit " j=${arr[$i]:9:26} " is going to point to? I would like it to create a directory in "/CCTV/RearOfHouse/" where the name would be that of what is put into the variable "j"?
e.g. so the result would be creating different directories such as "/CCTV/RearOfHouse/201904", or "/CCTV/RearOfHouse/201905"

I can then use further code to move files into their relevant directories, perhaps within the same 'for' loops.

Thanks

WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
scruss
Posts: 2630
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Another exe - mkdir question?

Wed Apr 03, 2019 3:01 pm

mkdir -p is your friend:

Code: Select all

       -p, --parents
              no error if existing, make parent directories as needed
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

Re: Another exe - mkdir question?

Wed Apr 03, 2019 3:47 pm

scruss wrote:
Wed Apr 03, 2019 3:01 pm
mkdir -p is your friend:

Code: Select all

       -p, --parents
              no error if existing, make parent directories as needed
Thanks, I have '-p' in my code, so does this mean I have to move my executable file into the 'CCTV/RearOfHouse/' directory to run it? as it creates the directory (and sub dirs) inside my 'ExecFiles' dir but they all exist in my 'home/pi/CCTV' directory already?
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
Paeryn
Posts: 2749
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Another exe - mkdir question?

Wed Apr 03, 2019 4:56 pm

doubleudee1 wrote:
Wed Apr 03, 2019 3:47 pm
scruss wrote:
Wed Apr 03, 2019 3:01 pm
mkdir -p is your friend:

Code: Select all

       -p, --parents
              no error if existing, make parent directories as needed
Thanks, I have '-p' in my code, so does this mean I have to move my executable file into the 'CCTV/RearOfHouse/' directory to run it? as it creates the directory (and sub dirs) inside my 'ExecFiles' dir but they all exist in my 'home/pi/CCTV' directory already?

Code: Select all

    j=${arr[$i]:9:26}  ###  this sets "j" to equal "CCTV/RearOfHouse/ARC201904"
     echo  "${j}"  ##just to check I am right
###  mkdir -p "${j}"
You have stripped the initial parts of the absolute path from the value held in j making it a relative one so when mkdir makes a directory using that value it will make it starting in the current working directory (wherever that is, not necessarily the directory where the script is). Keep the whole of the path and it will make them in the same directory as the files (which is what I assume you want).

Just noticed, in your original post you wrote e.g. so the result would be creating different directories such as "/CCTV/RearOfHouse/201904", did you really want them in the directory starting /CCTV (that is CCTV in the root directory, not the CCTV in your home directory)? If so then just prepend the / to the start of j

Code: Select all

j=/${arr[$i]:9:26}
or (came to me whilst I was out) just strip one character earlier

Code: Select all

j=${arr[$i]:8:27}
She who travels light — forgot something.

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

Re: Another exe - mkdir question?

Thu Apr 04, 2019 10:55 am

Paeryn wrote:
Wed Apr 03, 2019 4:56 pm
doubleudee1 wrote:
Wed Apr 03, 2019 3:47 pm
scruss wrote:
Wed Apr 03, 2019 3:01 pm
mkdir -p is your friend:

Code: Select all

       -p, --parents
              no error if existing, make parent directories as needed
Thanks, I have '-p' in my code, so does this mean I have to move my executable file into the 'CCTV/RearOfHouse/' directory to run it? as it creates the directory (and sub dirs) inside my 'ExecFiles' dir but they all exist in my 'home/pi/CCTV' directory already?

Code: Select all

    j=${arr[$i]:9:26}  ###  this sets "j" to equal "CCTV/RearOfHouse/ARC201904"
     echo  "${j}"  ##just to check I am right
###  mkdir -p "${j}"
You have stripped the initial parts of the absolute path from the value held in j making it a relative one so when mkdir makes a directory using that value it will make it starting in the current working directory (wherever that is, not necessarily the directory where the script is). Keep the whole of the path and it will make them in the same directory as the files (which is what I assume you want).

Just noticed, in your original post you wrote e.g. so the result would be creating different directories such as "/CCTV/RearOfHouse/201904", did you really want them in the directory starting /CCTV (that is CCTV in the root directory, not the CCTV in your home directory)? If so then just prepend the / to the start of j

Code: Select all

j=/${arr[$i]:9:26}
or (came to me whilst I was out) just strip one character earlier

Code: Select all

j=${arr[$i]:8:27}

Thanks, but what I am looking to achieve is to create new direcrories inside the existing "/CCTV/RearOfHouse/" which would be named "201902", "201903", "201904" so I would have a whole lot of ne directories to move the relevant files into.

FYI ("CCTV" is a HDD attached to the pi and all files are redirected into the relative directories on it).

WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

User avatar
scruss
Posts: 2630
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Another exe - mkdir question?

Thu Apr 04, 2019 12:37 pm

doubleudee1 wrote:
Thu Apr 04, 2019 10:55 am
Thanks, but what I am looking to achieve is to create new directories inside the existing "/CCTV/RearOfHouse/" which would be named "201902", …
When you say "/CCTV/RearOfHouse/", that means "CCTV off the root directory", not "CCTV relative to /home/pi". Start by creating a variable that is the absolute path of your CCTV folder, then append the programmatic bits, then do 'mkdir -p'
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

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

Re: Another exe - mkdir question?

Thu Apr 04, 2019 1:44 pm

scruss wrote:
Thu Apr 04, 2019 12:37 pm
doubleudee1 wrote:
Thu Apr 04, 2019 10:55 am
Thanks, but what I am looking to achieve is to create new directories inside the existing "/CCTV/RearOfHouse/" which would be named "201902", …
When you say "/CCTV/RearOfHouse/", that means "CCTV off the root directory", not "CCTV relative to /home/pi". Start by creating a variable that is the absolute path of your CCTV folder, then append the programmatic bits, then do 'mkdir -p'
OK, thanks

WD
Make the most of your family and friends , your children grow up too quickly and you don't notice yourself ageing, friends dissapearing, moving on. You make plans for your future, but they can all be smashed in an instant, live life and enjoy. :)

Return to “Other programming languages”