Is there a way to do a remote "ls" much like "scp" does a remote copy?
Solution 1:
You could always do this:
ssh user@host ls -l /some/directory
That will SSH to the host, run ls, dump the output back to you and immediately disconnect.
Solution 2:
To list all files in a directory:
rsync host.name.com:directory/path/'*'
For something like find directory/path -ls
rsync -r host.name.com:directory/path
Solution 3:
For all coming via google to this question because they are looking for a way to list remote files but can not access the remote server via ssh (common case for backup servers) you could use 'sftp'.
Example:
sftp [email protected]
ls
cd somedir
exit
Start an interactive session in a specific remote directory:
sftp [user@]host[:dir]
Solution 4:
Yes. SSH and do an ls
:
ssh host ls /path
You could easily script this to be more flexible, or use the host:path syntax scp
uses.
Solution 5:
The above answers do not contemplate when you need to add a password. To include password and username in a single command, install sshpass
.
For mac: $ brew install hudochenkov/sshpass/sshpass
For linux: sudo apt-get install sshpass -y
Then:
$ sshpass -p your_password ssh user@hostname ls /path/to/dir/
You can also save output:
$ sshpass -p your_password ssh user@hostname ls /path/to/dir/ > log.txt
In python3:
import subprocess
cluster_login_email = 'user@hostname'
cluster_login_password = 'your_password'
path_to_files = '/path/to/dir/'
response = subprocess.run([
'sshpass', '-p', cluster_login_password, 'ssh', cluster_login_email, 'ls',
path_to_files], capture_output=True)
response = response.stdout.decode("utf-8").split('\n')