Search code examples
bashsshscplscp

List md5sum of files on a remote server using ssh and return the output in text file?


I have Machine-A and Machine-B and both are Ubuntu servers. Now I want to list all the files on Machine-B using ssh. I want to return the result in a text file so I can analyse the result and the use scp to copy the required files.

ssh [email protected] 'bash -s tree /f'
ssh [email protected] 'bash -s ls -LR'

Now this command is not giving the result I wanted. Can anyone help with this so I can list all files on the remote computer using ssh and return the output in the form of a text file.

I am using ls -LR to list files and SSH to remote script execution. From the Answer i worked on my problem and iam updating the question to match one little requirement.

I got the list of files throught this command ssh [email protected] ls /something/sub > output.txt

But i want the md5sum of all files instead of names because 2 file names might get match.So is there any way to list all files and return all md5sum of all files and return to output.txt file.


Solution

  • Copy the file list to a valid path in Machine B and copy it back to Machine A using scp

    ssh username@machineB 'ls -LR /path/to/dir > ~/fileList'
    

    To return the md5sum of all the files in the directory, use find as

    ssh username@machineB 'find /path/to/dir -type f -exec md5sum {} \; > ~/md5sum_fileList'
    

    Now copy the file back to machine A, using a glob pattern to copy the files having the pattern fileList

    scp username@machineB:~/*fileList* username@machineA:~/