How to (quickly) know if a remote path is a file or directory?

I’m writing some bash functions to wrapper rclone.
My goal is to have three basic commands “rclone-status”, “rclone-pull” and “rclone-push” that assumes some local directory structure and remote names to make my life a little bit easier… yes, I’m too lazy to keep writing source and target continuously! :wink:

In them I need to distinguish if the path entered by the user is a file or a directory, because rclone fails in some operations if target is a file.
When doing the “push-like” sync I just can check with normal bash method if the path is a file or directory.
But when doing “pull-like” sync, I should check the remote with rclone.
AFAIK the only way to know if a remote path is a file or a directory is to use “rclone size” and check the “Total objects”…?
But this approach has two (very) big problems:

  • If it is a directory, and a big one, it will take a lot of time.
  • If it is an empty directory it wouldn’t work.

Is there any quick and robust way to query if a remote path is a file or a directory?
If not, can we convert this question into a feature request? :wink:

Something like:
$ rclone isfile remote:/path/to/file.ext
yes
$ rclone isfile remote:/path/to/dir
no

Thanks a lot for such a nice tool!

Albert

This was the best I could come up with

$ rclone rmdir -vv --dry-run test/hello.txt ; echo $?
2018/02/25 09:49:05 DEBUG : Using config file from "/home/ncw/.rclone.conf"
2018/02/25 09:49:05 DEBUG : rclone: Version "v1.39-175-g749fb01c-vfs-update" starting with parameters ["rclone" "rmdir" "-vv" "--dry-run" "test/hello.txt"]
2018/02/25 09:49:05 Failed to create file system for "test/hello.txt": is a file not a directory
1
$ rclone rmdir -vv --dry-run test ; echo $?
2018/02/25 09:49:12 DEBUG : Using config file from "/home/ncw/.rclone.conf"
2018/02/25 09:49:12 DEBUG : rclone: Version "v1.39-175-g749fb01c-vfs-update" starting with parameters ["rclone" "rmdir" "-vv" "--dry-run" "test"]
2018/02/25 09:49:12 INFO  : Local file system at /tmp/test: Modify window is 1ns
2018/02/25 09:49:12 NOTICE: Local file system at /tmp/test: Not deleting as dry run is set
2018/02/25 09:49:12 DEBUG : Go routines at exit 2
2018/02/25 09:49:12 DEBUG : rclone: Version "v1.39-175-g749fb01c-vfs-update" finishing with parameters ["rclone" "rmdir" "-vv" "--dry-run" "test"]
0

Hope that helps!

It works great!
Thanks a lot!

1 Like