Listing files on a remote machine is very easy. Rsync is the best way to do this. It can be used to transfer a bunch of files from one machine to another, but it also can be used to list a remote directory.
Here is a very useful example.
┌──[jason@192.168.1.2]─[~] └──╼ ╼ $ rsync --list-only jason@192.168.1.3:/home/jason/Documents/ jason@192.168.1.3's password: drwxr-xr-x 4,096 2021/01/03 18:16:37 . -rw-rw-r-- 2,477,140 2020/09/07 09:12:13 ballgirl.webm -rw-rw-r-- 1,552,097 2020/09/07 12:52:51 ballreaction.webm -rw-rw-r-- 276 2020/07/27 08:55:48 testing.c drwxrwxr-x 4,096 2021/01/03 18:17:24 systemd |
This shows how easy it is to list the contents of a remote directory. This is indeed very useful for us.
Below is another example, I am using awk to only show the fifth column of text.
──[jason@192.168.1.2]─[~] └──╼ ╼ $ rsync --list-only jason@192.168.1.3:/home/jason/Documents/ | awk '{print $5}' jason@192.168.1.3's password: . ballgirl.webm ballreaction.webm testing.c systemd |
To remove the first line of text in the output, use this example.
┌──[jason@192.168.1.2]─[~] └──╼ ╼ $ rsync --list-only jason@192.168.1.3:/home/jason/Documents/ | awk 'NR>1 {print $5}' jason@192.168.1.3's password: ballgirl.webm ballreaction.webm testing.c systemd |
Using NR>1 in awk will remove the first line. This is a very useful shell trick, this is a very useful part of shell scripting. The ability to filter text easily is what makes the Linux bash shell so useful.