1Password CLI: Adding tags?

BrBill
BrBill
Community Member
edited September 2020 in CLI

I would like to bulk-add a tag to about 200+ of my 700+ items. Can this be done with the CLI? I can't figure out how, if so. There's no CLI documentation on adding or removing tags, that I can find.

Using 1Password 7 native mac client, I can bulk-delete tags, but not bulk-add them.

Thanks


1Password Version: 7.6
Extension Version: Not Provided
OS Version: macOS 10.15.6
Sync Type: 1Password account

Comments

  • Hey @BrBill

    Using 1Password 7 native mac client, I can bulk-delete tags, but not bulk-add them.

    You can select multiple items in the item list and then drag them to a tag in the sidebar to add that tag to all of the selected items. :)

    Can this be done with the CLI?

    In case the above doesn't help, or you'd like an answer to this anyway, I've moved this question over to our CLI category so our experts there can better assist. :+1:

    Thanks!

    Ben

  • BrBill
    BrBill
    Community Member

    Thanks, Ben. If there's no way with the CLI, at least there is something that I can do to cut down the work.

  • SvenS1P
    edited September 2020

    Hi @BrBill, once you've created an item, you can change the tags on it by doing op edit item <item> tags=<tags>. You'll need to specify all the tags you want that item to have, including any existing tags.

    As an example of bulk tagging in bash (I've tested this in Fedora Remix), if you wanted to add the tag "finance" to all Login items with a title containing the word "bank", you could do something like this with jq:

    $ op list items --categories Login | jq -c '.[] | select(.overview.title | ascii_downcase | contains("bank"))' | while read -r line ; do op edit item "$(jq -r '.uuid' <<< $line)" tags="$(jq -r '.overview.tags | join(",") + "," + "finance"' <<< $line)" ; done
    

    To break that down a bit:

    • op list items --categories Login: gets a JSON array of all Login items in an account
    • jq -c '.[] | select(.overview.title | ascii_downcase | contains("bank"))': selects any items where the lowercase title contains "bank", and then prints each matching item on a separate line
    • while read -r line ; do ... ; done: loops over each line (which corresponds to a matched item) provided by the last command
    • jq -r '.uuid' <<< $line: gets the UUID for each matched item
    • jq -r '.overview.tags | join(",") + "," + "finance"' <<< $line: get the existing tags for the item, and append the new tag "finance" to them
    • op edit item $(jq -r '.uuid' <<< $line) tags=$(jq -r '.overview.tags | join(",") + "," + "finance"' <<< $line): edit the item using the UUID and new tags obtained from the previous two commands

    You can also find the documentation for jq over here: https://stedolan.github.io/jq/manual/.

    I hope that helps, but let us know if there's anything we can do! :smile:

  • BrBill
    BrBill
    Community Member
    edited September 2020

    This is a great compressed tutorial and I will customize it to fit my needs. Thanks, Ben!

    Is deleting tags also possible?

  • SvenS1P
    edited September 2020

    Just to confirm, are you still looking for this in relation to the CLI tool @BrBill?

    If you just need to remove the tag from one item on the CLI, edit them item and omit tag you want to delete. For example, to remove the tag C from an item with tags A,B,C, you'd run op edit item <item> tags=A,B.

    If you're looking to bulk remove tags, then you'll need to use a tool like jq again. For example, you could make just a small change to the command I gave before and delete the finance tag from any items with bank in the title:

    $ op list items --categories Login | jq -c '.[] | select(.overview.title | ascii_downcase | contains("bank"))' | while read -r line ; do op edit item "$(jq -r '.uuid' <<< $line)" tags="$(jq -r '.overview.tags | map(select(. != "finance")) | join(",")' <<< $line)" ; done
    

    The only thing that needed to be changed here was the third jq command: jq -r '.overview.tags | map(select(. != "finance")) | join(","). Instead of adding the tag on to the end, it now excludes the finance tag if it finds it. You could also combine the two to replace/rename a tag.

This discussion has been closed.