A script in ssh to a remote folder and check all files?

I have a pair of public / private keys, so I can ssh to a remote server without having to log in. I am trying to write a shell script that lists all the folders in a specific directory on a remote server. My question is: how to specify a remote location? Here is what I have:

 #!/bin/bash

for file in myname@example.com:dir/*
do
if [ -d "$file" ]
then
echo $file;
fi
done
+3
source share
1 answer

Try the following:

for file in `ssh myname@example.com 'ls -d dir/*/'`
do
    echo $file;
done

Or simply:

ssh myname@example.com 'ls -d dir/*/'

Explanation:

  • ssh , , ; ssh stdout stdout. ls.
  • ls -d dir/*/- , ls .
+14

Source: https://habr.com/ru/post/1721724/


All Articles