Bosscheesemo
Posts: 5
Joined: Tue Jul 10, 2018 10:06 pm

Yes/No file contents query

Wed Jul 11, 2018 2:44 am

Let's say I've got a particular directory on a remote (same LAN) device. I don't know what's in the directory at any given time maybe there's something there, maybe not. I want my Pi to regularly query that directory and make a choice based on whether or not it finds something in the directory. How do I do that?

Thanks everyone!

bertlea
Posts: 178
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Yes/No file contents query

Wed Jul 11, 2018 3:25 am

using bash?
https://superuser.com/questions/352289/ ... -directory

using Python?
https://thispointer.com/python-how-to-c ... -is-empty/

using other programming language? Can try google your programming language name and keywords: 'check if folder is empty'.

tpyo kingg
Posts: 314
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Yes/No file contents query

Wed Jul 11, 2018 4:17 am

If it's not a high-volume activity, then incron could be used to monitor the directory and trigger a script when a filename appears or is deleted.

That script could be a shell script (avoid bashisms when possible to promote portability) or a python or perl script.

pfletch101
Posts: 209
Joined: Sat Feb 24, 2018 4:09 am

Re: Yes/No file contents query

Wed Jul 11, 2018 2:08 pm

bertlea wrote:
Wed Jul 11, 2018 3:25 am
using bash?
https://superuser.com/questions/352289/ ... -directory

using Python?
https://thispointer.com/python-how-to-c ... -is-empty/

using other programming language? Can try google your programming language name and keywords: 'check if folder is empty'.
tpyo kingg wrote:
Wed Jul 11, 2018 4:17 am
If it's not a high-volume activity, then incron could be used to monitor the directory and trigger a script when a filename appears or is deleted.

That script could be a shell script (avoid bashisms when possible to promote portability) or a python or perl script.

All these answers seem to assume that the directory to be monitored is local. The OP specified that the directory (s)he wanted to monitor was on another system (on the same LAN). At the very least, this would require that the remote directory be shared and (presumably) mounted on the local system, and I am not sure whether (particularly) the incron approach would work under those circumstances.

tpyo kingg
Posts: 314
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Yes/No file contents query

Wed Jul 11, 2018 2:29 pm

As long as the remote system runs GNU/Linux it can run scripts launched by incron. Those scripts could then use single-purpose SSH keys to launch a second script on the 'local' system. It's not hard to set up that way, but depending on how frequently the 'local' scripts must be called it might be important to set up the 'remote' script to avoid firing up two or more SSH sessions at once. flock might do it from a shell script.

Bosscheesemo
Posts: 5
Joined: Tue Jul 10, 2018 10:06 pm

Re: Yes/No file contents query

Thu Jul 12, 2018 2:14 am

Ok so a little clarification.

The specific idea I have in mind is to have my Raspberry Pi monitor my network for a successful ping to my phone's IP address. If it reads that address on the network, I need it to empty a particular folder on my phone that I use to store pics. If the pi sees anything at all, it's to assume that it needs to relocate everything in that folder to my NAS. Maybe I took pics a given day, maybe I didn't. I've got the network monitor code built and tested, I've got the NAS transfer figured, just need to know how to get the pi to query my phone.

More generally, I have a lot of other ideas for the script once I've got a lot more experience. One idea I think would be really cool is having one Pi able to query another Pi's network interfaces file after a fresh OS install and automatically write custom IP settings to the file.

Kinda the general idea: one machine on the LAN queries a directory on another device, then does something based on the true/false return.

ghans
Posts: 7801
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Yes/No file contents query

Thu Jul 12, 2018 4:27 am

Which protocol does the file server on your phone support ? Without that i can only give very general hints which are obvious: Linux can mount remote directories, provided the protocol is supported. Remote directories are indistinguishable from local ones to 90 % of Linux apps, especially when using manual mounting.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Return to “Beginners”