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
  • 28,728
  • 32
  • 139
  • 229
  • 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
  • 411,918
  • 54
  • 1,065
  • 1,164
  • This should work perfectly. Given [`umount -l` is dangerous](https://unix.stackexchange.com/q/390056/143394), I'm surprised to see that [`systemd` lazy unmounts the mountpoint](https://github.com/systemd/systemd/issues/6754) with this command. – Tom Hale Sep 06 '17 at 03:34