1

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 my_user_name@192.168.150.4 'bash -s tree /f'
ssh my_user_name@192.168.150.4 '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 my_user_name@192.168.150.4 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.

Community
  • 1
  • 1
SaiKiran
  • 6,244
  • 11
  • 43
  • 76

2 Answers2

1

All you need to do is specify the command, without using "bash". Your default shell will be used on the remote device to execute the command.

ssh remote-host command

To save the output of your ls command to a file, you can simply use the usual shell redirection:

ssh remote-host command > output.txt

Just in case you end up with multiple file names on a single line, you may need to use -1 on the ls command line. Also, remember that if a filename includes a space, you need quotes in a shell script to support those...

To run multiple commands in a row, although the output won't be as easy to manage, you use quotes and separate commands with semicolons (for example) as in:

ssh remote-host "command1; command2; command3" > output.txt

In regard to md5sum, you can run that against all the files in a directory using the find command along with md5sum:

ssh remote-host "find . -type f -exec md5sum {} \;" > output.txt

Change the path (. in the example) to whatever works for you.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • Can i make it multiple commands like `cd something/sub ls `.Which moves to another directory and then list files? – SaiKiran Jan 19 '17 at 06:23
  • 1
    @SaiKiran: Just pass the path as part of the command, i.e. `ls -LR /something/sub > ~/fileList` as in my answer – Inian Jan 19 '17 at 06:27
  • @Inian here i have several files inside the folder so can i pass something which returns md5 sum of this files `md5sum ls /somethin/sub` – SaiKiran Jan 19 '17 at 06:34
  • @SaiKiran: This is totally a new requirement now. Can you update the question with all your requirements? and let people answer them in one-shot? – Inian Jan 19 '17 at 06:37
1

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:~/
Inian
  • 80,270
  • 14
  • 142
  • 161
  • If you put the `> md5sum_fileList` inside the quotes, then the file is created on the remote host. From what I understand the OP wanted the result in the local host. – Alexis Wilke Jan 19 '17 at 21:55
  • @AlexisWilke: Totally agree with you, OP wanted to create the file in the remote machine and copy it back to local using `scp` – Inian Jan 20 '17 at 05:24