tags:

views:

202

answers:

1

I have a public/private key pair set up so I can ssh to a remote server without having to log in. I'm trying to write a shell script that will list all the folders in a particular directory on the remote server. My question is: how do I specify the remote location? Here's what I've got:

 #!/bin/bash

for file in [email protected]:dir/*
do
if [ -d "$file" ]
then
echo $file;
fi
done
+6  A: 

Try this:

for file in `ssh [email protected] 'ls -d dir/*/'`
do
    echo $file;
done

Or simply:

ssh [email protected] 'ls -d dir/*/'

Explanation:

  • The ssh command accepts an optional command after the hostname and, if a command is provided, it executes that command on login instead of the login shell; ssh then simply passes on the stdout from the command as its own stdout. Here we are simply passing the ls command.
  • ls -d dir/*/ is a trick to make ls skip regular files and list out only the directories.
Siddhartha Reddy
awesome, thank you.
Goose Bumper