I tried accessing files from remote server "10.101.28.83"
and manipulating files to create folders on host server where script has been run. But *
is the output of echo "$(basename "$file")" command which implies that files are not read from remote server.
#!/bin/bash
#for file in /root/final_migrated_data/*; do
for file in root@10.101.28.83:/root/final_migrated_data/* ; do
echo "$(basename "$file")"
IN="$(basename "$file")"
IFS='_'
read -a addr <<< "$(basename "$file")"
# addr[0] is case_type, addr[1] is case_no, addr[2] is case_year
dir_path="/newdir1";
backup_date="${addr[0]}_${addr[1]}_${addr[2]}";
backup_dir="${dir_path}/${backup_date}";
mkdir -p "${backup_dir}";
cp /root/final_migrated_data/"${backup_date}"_* "${backup_dir}"
done
I expect the output of echo "$(basename "$file")" to be the list of files present at the location /root/final_migrated_data/
of remote server but the actual output is *
.
You can use sshfs. As the name suggests, sshfs allows to mount locally (both for reading and writing) a distant filesystem to which you have SSH access. As long as you already know SSH, its usage is very straightforward:
# mount the distant directory on your local machine:
sshfs user@server.com:/my/directory /local/mountpoint
# manipulate the filesystem just like any other (cd, ls, mv, rm…)
# unmount:
umount /local/mountpoint
If you need to mount the same distant filesystem often, you can even add it to your /etc/fstab
, refer to the documentation for how to do it.
Note however that using an SSH filesystem is slow, because each operation on the filesystem implies fetching or sending data through the network, and sshfs is not particularly optimized against that (it does not cache file contents, for instance). Other solutions exist, which may be more complex to set up but offer better performance.
See by yourself whether speed is a problem. In your case, if you are simply copying files from one place in your server to another place in your server, it seems rather absurd to make it transit through your home computer and back again. It may be more advantageous to simply run your script on your server directly.