12

How do I convert arbitrary paths to systemd unit file name format?

For example, how would I convert a path like /media/backup to media-backup so I could stop the automounter via:

systemctl stop media-backup.automount

The solution should escape special characters.

Tom Hale
  • 30,455
  • Is there any list of those arbitrary paths or do you want to convert every path in the system? What have you tried to do ? – mrc02_kr Sep 05 '17 at 16:28

1 Answers1

26

You should use systemd-escape which is designed explicitly for this:

systemd-escape -p --suffix=automount /media/backup

-p specifies that you’re escaping a path, and handles /, . and .. appropriately. Special characters are handled correctly too. Thus your command would become

systemctl stop "$(systemd-escape -p --suffix=automount /media/backup)"

The escaping rules are described in the systemd.unit manpage.

Stephen Kitt
  • 434,908