Score:2

How to delete a tracker tag containing special characters

kn flag

'tracker' is a command-line file indexing and searching tool. Among other things, it allows files to be annotated with tags. I'm trying to delete a tag from a PDF file. Other tags on other files have deleted successfully. This tag name is long and has some special characters in it:

$ tracker tag -ts
Tags (shown by name):
  setup# connections# features# upgrading# troubleshooting# recovery# specifications
#esuprt_desktop#esuprt_alienware_dsk#Alienware Aurora#alienware-aurora#Owner's Manual 
    file://FILEPATH.pdf

Note the newline in the tag name after "specifications".

(I've substituted FILEPATH in here instead of the actual file name. It doesn't seem to be relevant. I've successfully deleted other tags in similar paths before, and have moved this file just in case, but am still unable to delete its tag)

I'm failing to delete this tag, using tracker tag -d TAGNAME, presumably due to my failure to represent or escape the special characters in the tag name. I first tried things like:

$ tracker tag -d "setup# connections# features# upgrading# troubleshooting# recovery# specifications
#esuprt_desktop#esuprt_alienware_dsk#Alienware Aurora#alienware-aurora#Owner's Manual"

This reports "Tag was removed successfully", but that's a lie - the tag is still present. This is the message that is displayed when deleting a tag that doesn't exist:

$ tracker tag -d NONEXISTANT
Tag was removed successfully

To make the special characters in the tag name visible, I used:

$ tracker info 'file://FILEPATH.pdf' | grep hasTag
  'nao:hasTag' = 'urn:tag:setup%23%20connections%23%20features%23%20upgrading%23%20troubleshooting%23%20recovery%23%20specifications%0D%0A%23esuprt_desktop%23esuprt_alienware_dsk%23Alienware%20Aurora%23alienware-aurora%23Owner's%20Manual'

Here we can see the urlencoded special characters. What I described as a "newline" earlier is actually a %0D%0A, which I think I can pass on the command-line using "\r\n" (Update: this is my mistake, see @steeldriver's answer), hence, maybe my delete should read:

$ tracker tag -d "setup# connections# features# upgrading# troubleshooting# recovery# specifications\r\n#esuprt_desktop#esuprt_alienware_dsk#Alienware Aurora#alienware-aurora#Owner's Manual"
Tag was removed successfully

This message, again, is a lie. The tag is still present.

I'm in Bash, on Ubuntu 20.10 Groovy. I do have ownership and write permissions to the tagged file, although don't think that is required.

Score:1
hr flag

Backslash escapes are supported in the context of the shell's printf and echo (with the -e option) builtins, but to force them to be expanded in a plain string argument you'd need to use ANSI quoting. From man bash:

   Words of the form $'string' are treated specially.  The word expands to
   string,  with backslash-escaped characters replaced as specified by the
   ANSI C standard.

It's a little complicated in your case because there's a single quote (apostrophe) in the tag string, but the following should work:

tracker tag -d $'setup# connections# features# upgrading# troubleshooting# recovery# specifications\r\n#esuprt_desktop#esuprt_alienware_dsk#Alienware Aurora#alienware-aurora#Owner'\''s Manual'

Alternatively, you can compose the CR character using keyboard sequence Ctrl+V Enter (it will display as ^M but actually be only a single character). Don't forget to follow it with another Enter for the LF.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.