export a recursive directory & file listing to a text file in Linux Bash shell with an SSH command

June 23, 2017 - Reading time: 3 minutes
find is a good utility to obtain (recursively) all the content of a directory. If you want the file (and directory) names with their permissions:
find /home/kparisi -printf "%M %p\n"

You can then use ssh to run this command on the remote server:

ssh kparisi@remote.com 'find /home/kparisi -printf "%M %p\n"'

And finally, if you want to store this in a file on your local server:

ssh kparisi@remote.com 'find /home/kparisi -printf "%M %p\n"' > file 

Alternatively:

tree > fsstruct.txt

for me this format is simpler to read.

It is easy also to use other features of tree:

tree -p > fsstruct.txt

prints file type and permissions before file and it is more accessible when you are reading plain text, which is a file and which is a directory.

tree -h > fsstruct.txt

this prints sizes of files in a human readable format:

tree -ph > fsstruct.txt

Also, it is possible to send output to the file:

tree -ph . -o fsstruct.txt

or create HTML:

tree -Hph . -o tree.htm