Posted: . At: 12:22 PM. This was 6 years ago. Post ID: 12141
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to use rsync to list filenames in a directory on a remote Linux server.


The rsync command with the –list-only parameter allows the user to list filenames on a remote machine.

But by default, it looks like this.

jason@Yog-Sothoth » ~ » $ rsync --list-only jason@192.168.1.6:/home/jason/Documents/
jason@192.168.1.6's password: 
drwxr-xr-x          4,096 2018/05/09 09:49:49 .
-rw-r--r--        867,863 2018/04/19 12:44:56 altis_insurgency_altis.pbo
-rw-rw-r--        681,638 2018/05/04 09:03:16 pg768.txt
-rwxrwxr-x            166 2018/01/17 09:48:52 rhsafrf.0.4.5.bikey
-rwxrwxr-x            166 2018/01/17 09:40:34 rhsgref.0.4.5.bikey
-rwxrwxr-x            165 2018/01/17 09:40:32 rhssaf.0.4.5.bikey
-rwxrwxr-x            166 2018/01/17 09:45:58 rhsusaf.0.4.5.bikey

But by adding an awk one-liner that accepts a pipe, this can be filtered out. Now it can look like the example below.

jason@Yog-Sothoth » ~ » $ rsync --list-only jason@192.168.1.6:/home/jason/Documents/ | awk '{ $1=$2=$3=$4=""; print substr($0,5); }'
jason@192.168.1.6's password: 
.
altis_insurgency_altis.pbo
pg768.txt
rhsafrf.0.4.5.bikey
rhsgref.0.4.5.bikey
rhssaf.0.4.5.bikey
rhsusaf.0.4.5.bikey

This gives our network user the ability to filter out unwanted detail and avoid information overload. This is a very, very useful networking trick, this is good for keeping track of what files are on a remote server.

This example below, shows how to run a command on the remote machine after the previous command completes. In this example, I am running the ps command on the remote machine to get information about the running processes.

jason@Yog-Sothoth » ~ » $ rsync --list-only jason@192.168.1.6:/home/jason/Documents/ | awk '{ $1=$2=$3=$4=""; print substr($0,5); }' ; ssh 192.168.1.6 'ps'
jason@192.168.1.6's password: 
.
altis_insurgency_altis.pbo
pg768.txt
rhsafrf.0.4.5.bikey
rhsgref.0.4.5.bikey
rhssaf.0.4.5.bikey
rhsusaf.0.4.5.bikey
[email protected]'s password: 
  PID TTY          TIME CMD
 1045 ?        00:00:00 systemd
 1046 ?        00:00:00 (sd-pam)
 1050 ?        00:00:00 fluxbox
 1107 ?        00:00:00 ssh-agent
 1110 ?        00:00:00 dbus-launch
 1111 ?        00:00:00 dbus-daemon
 2200 ?        00:00:00 sshd
 2201 ?        00:00:00 ps

I hope you find these examples very useful, I have had fun getting these together and they are useful on a Linux network.


1 thought on “How to use rsync to list filenames in a directory on a remote Linux server.”

  1. On my computer – the text in your boxes is completely invisible unless I highlight it all.

    Thank you for the great tip – this is what I was looking for – that is why I had to highlight it to finally find it.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.