1

I need to use rsync or scp for copy file or file folder from remote server to computer.

To copy (synchronize) a file folder I use the following function:

foldertocomp() {
   rsync -avz pi@192.168.1.5:"'/home/pi/Torrents/$1'"/* "/home/comp/Torrent/$1"
}

I can use the following functions to copy a file:

filetocomp() {
   rsync -avz pi@192.168.1.5:"'/home/pi/Torrents/$1'" "/home/comp/Torrent/"
}

OR

filetocomp() {
   scp pi@192.168.1.5:"'/home/pi/Torrents/$1'" /home/comp/Torrent
}

All these functions work well. But I need to combine these functions into one single function. Like this:

tocomp() {
   Some code...
}

Use function:

tocomp "HERE name of folder OR name of file"

I think I need to build the function like this:

tocomp() {
   if "$1 is a FILE"
      rsync -avz pi@192.168.1.5:"'/home/pi/Torrents/$1'" "/home/comp/Torrent/"
   elif "$1 is a FOLDER"
      rsync -avz pi@192.168.1.5:"'/home/pi/Torrents/$1'"/* "/home/comp/Torrent/$1"
   fi
}

But how to check $1 is a folder or file?

I found the following method: stackoverflow.com/a/4665080/10728472. But I think this method not for remote server.

How to check $1 is a folder or file on a remote server?

Denis
  • 113
  • 2
  • I think if you just add `-r`, you'll be covered for both cases: `scp -r pi@192.168.1.5:"'/home/pi/Torrents/$1'" /home/comp/Torrent` – glenn jackman Apr 28 '19 at 15:16
  • Im with glenn, but in any case if you sync local with remote why you need check if is dir or not in remote I sume is the same content and if is dir in local is also dir in remote, in any case if you want test if is dir in remote you need a new script, connect to remote server over ssh and test if is dir or not. – Skamasle Apr 28 '19 at 15:22
  • To Glenn Jackman: Yes, I previously used this method. But scp does not know how to synchronize (using scp you can not interrupt copying files in a folder and then continue) files in the folder, so I abandoned this method. I apologize for not writing about it right away. – Denis Apr 28 '19 at 16:01

2 Answers2

1

You can use stat command on the remote host as shown below to check for the type. You may need to adjust stat arguments depending on your remote OS. This should work fine on any Linux flavor.

arg="/home/pi/Torrents/$1"

file_or_directory=`ssh pi@192.168.1.5 "stat --format=%F $arg" 2>&1`

if [ "$file_or_directory" = "directory" ] ; then
  echo "$arg: is a directory"
elif [ "$file_or_directory" = "regular file" ] ; then
  echo "$arg: is a file"
elif [ "$file_or_directory" = "symbolic link" ] ; then
  echo "$arg: is a link"
else
  echo "$arg: unknown"
fi
Arul Selvan
  • 1,428
  • 13
  • 11
1

Your rsync command to sync the directory

rsync -avz pi@192.168.1.5:"'/home/pi/Torrents/$1'"/* "/home/comp/Torrent/$1"

can be written as

rsync -avz pi@192.168.1.5:"'/home/pi/Torrents/$1'" "/home/comp/Torrent/"

Both sync directory $1 to /home/comp/Torrent/$1. So you'll only need one command, it's the same as your rsync command for the file.

Freddy
  • 2,039
  • 7
  • 13