开发者

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

开发者 https://www.devze.com 2022-12-09 21:35 出处:网络
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 re

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/ba开发者_开发问答sh

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


Try this:

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

Or simply:

ssh myname@example.com '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.
0

精彩评论

暂无评论...
验证码 换一张
取 消