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 | $ ls -al |
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.
Please help bash friends, copy `ls -al` from a server to local txt file https://t.co/R1w0Pe1m8P +@sindresorhus @mathias @csswizardry
— Simon Owen (@s10wen) July 22, 2014
Within 10 minutes, a few awesome people had replied all with solutions.
@s10wen @sindresorhus @mathias @csswizardry output to file with >> then scp the file from the server?
— Ash Davies (@erraticwelshie) July 22, 2014
@s10wen @sindresorhus @mathias https://t.co/mxwDGZPDXL ?
— Harry Roberts (@csswizardry) July 22, 2014
@s10wen If all you need is `ls -al` output, run `ssh host 'ls -al /some/path' > foo.txt` on your local machine. +@sindresorhus @csswizardry
— Mathias Bynens (@mathias) July 22, 2014
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 |
|
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.
Browse by category:
- apple1
- bash1
- browser1
- case study4
- chrome2
- chrome dev tools4
- clojure overtone2
- conference2
- css2
- dev tips1
- digihike1
- dotfiles2
- event4
- ffconf1
- freelance6
- gaming2
- health3
- hexo1
- https1
- jank1
- javascript1
- jobs1
- lego1
- mcrfred107
- nux1
- octopress4
- photography2
- raspberry pi1
- responsive web design1
- sass1
- screencast1
- speaking2
- svg4
- unplugged6
- upfrontconf1
- web development2
- webgl1
- wordpress17
- work51
- workflow16