Omar Montemayor
Omar Montemayor

Reputation: 5

bash script to access a file in a remote host three layers deep

So in the terminal I access the remote host through ssh -p then once I'm in i have to cd /directory1/directory2/. Then I want to find the latest directory which I do using ls -td -- */ | head -n 1 then using this I want to cd into that and tail -n 1 file1

All these commands work in the terminal but I want to automate it to where I can just type ./tailer.sh and have that be output.

Any ideas would be appreciated.

Upvotes: 0

Views: 51

Answers (1)

sai
sai

Reputation: 349

The shell script tailer.sh can look something like this

#!/bin/bash
ssh -p <PORT> <HOST_NAME> '( cd /directory1/directory2/ && LATEST_DIR=$(ls -td -- */ | head -n 1) && cd ${LATEST_DIR} &&  tail -n 1 file1 )'

Then give execute permissions to tailer.sh using chmod u+x tailer.sh

Run the script using ./tailer.sh

Upvotes: 1

Related Questions