Output ls contents of a server directory using bash

I'm getting really excited spending more and more time using the command line.

Today I had an issue, I wanted to write a script that would ssh into a server and save the ls from a certain path.

If you're not familiar with bash, ls -al will list everything in a vertical list like this:

1
2
3
4
5
6
7
8
9
$ ls -al

total 109
drwxr-xr-x 18 root root 4096 Jun 9 21:12 ./
drwxr-xr-x 18 root root 4096 Jun 9 21:12 ../
drwxr-xr-x 2 root root 4096 Jun 9 21:14 bin/
drwxr-xr-x 3 root root 1024 Jun 9 20:32 boot/
drwxr-xr-x 6 root root 36864 Jul 12 10:26 dev/
drwxr-xr-x 34 root root 4096 Jul 12 10:25 etc/

I thought about how I'd do this with my limited knowledge of bash and did some googling but didn't get anywhere.

I sent out a tweet and copied in a few people.

Within 10 minutes, a few awesome people had replied all with solutions.

This is a shout out to everyone that is so freaking awesome (sorry Harry, I know I'm not American, but how I love the word :wink:) and willing to take time out of their day to help.

With the information above I was able to put a little script together:

1
2
3
4
5
#!/bin/bash

ssh [email protected] 'ls -al /where/you/want/' > server1.txt
ssh [email protected] 'ls -al /where/you/want/' > server2.txt
ssh [email protected] 'ls -al /where/you/want/' > server3.txt

Now I have a directory locally called server that has a file in called server.sh, I can cd to it and run bash server.sh and I end up with the ls -al contents of the servers.

https://gist.github.com/s10wen/6cf00ab86b96e5d1fa9c

Niiiiiiiiiiiiiiiiiiice.


Like this post? Hire Me!