Checking whether an item on the remote exists

Hello!

Am I right in thinking that the best way to check for the existence of a file or directory is to use rclone size? Admittedly, I am unsure what my criteria are for 'best' but 'quick' is one of them.

I see that already there is an answer by ncw to this question, in this thread. However, the link in that answer seems partially broken - it leads to a page (this one) but not to a specific part of the page - and I could do with some further help. It seem one can run a rclone rc command and then grep its output for the name one wants - but I do not know the syntax of the rclone rc command.

Rclone 1.52.2
Linux.
B2.

PS: I wish all fora used software as good as the software used on this forum!

rclone rc is the most efficient way of doing this if you have lots to do.

Firstly run an rc server rclone rcd (or add the --rc to an existing rclone mount for example) - you'll need to set some auth up or use the --rc-no-auth flag.

You can then do

rclone rc operations/list fs=b2:bucket remote=directory

That is great for listing directories, but note that if you list a non-existent directory you'll just get an empty answer not an error (this is true of all the bucket based backends)

For a file you could list the parent to see if it exists. As it says in that thread I should probably implement operations/newobject which would be more efficient.

The other thing you could do which will work quite well is use rclone mount and check in the mount with stat. This will do caching and will be quite efficient.

Do you know in advance whether you are looking for a file or a directory?

Thank you, Nick.

I will be seeking a file with these features: it is a .tar file; I know its name; it will be the only occupant of a specified directory.

Here is the reason that I said speed was desirable. The check/seek in question will be one of multiple steps in an interactive script; the user will be waiting for the output. But speed is not really crucial because: the script runs only on-demand; and only one check is needed per run.

Does running a 'rc server' entail keeping it running all the time? If so, then it's not practical for me.

All in all, perhaps the best approach for me is to use - yes? - rclone ls.

It sounds like rclone lsf is probably what you want. Just grep the output to check the file is there.

Maybe I should do an rclone stat command? If you could give it an optional hint as to whether you were expecting a file or a directory that could be used to speed things up.

So

rclone stat remote: path
rclone stat --file remote:path
rclone stat --dir remote:path

The output would be what you'd get from lsjson just on that object with an error code if it wasn't found.

Thank you.

So, rlcone lsf - like this? rclone lsf --http-url https://example.com :http:path/to/dir. (Source.) I see that one does not specify the remote in the normal way but instead via an URL . .

An rclone stat command does sound better, partly because it uses the more normal format to specify its object - and having a non-zero return code for 'not found' would he helpful too!

That looks OK

Err, that is only if you are using a config-less HTTP remote.

More normally you'd just do

rclone lsf remote:path/todir

I'll have a think about that