How to copy a hierarchy of pages to another space and replace text using Confluence CLI

Description

This article explains how to copy a hierarchy of pages to another space while being able to replace specific text in the pages.


Instructions

  1. To copy a page with the child pages to another space, use the CLI copyPageHierarchy action command. Using this command, you can also modify the titles and other page elements as required.

    In the below example, SRC is the source space key and TRG is the target space key. The page, Chapter1 with its hierarchy is copied from source space to target space and pasted under Soft key info.

    --action copyPageHierarchy --space SRC --title "Chapter1" --newSpace TRG --parent "Soft key info" --copyAttachments --copyLabels
  2. Once confirmed that the page hierarchy is copied to target space, modify the pages with the required content by using the CLI findReplace parameter.

    The actions below work together in finding specific text and replacing them as per the text mentioned in the relevant commands:

    --action runFromContentList --cql "space=TRG and type=page" --common "--action modifyPage --id @pageId@ --findReplace \"important#valid,page#doc,need#have\" --special \" #\""

    For example, the word page is replaced by the word doc etc.

    In the above action command, --special is used to ordered list of alternate characters for comma ( , ), colon ( : ), at ( @ ), quote ( ' ), and double quote ( " ) characters used for specialized processing of some specific parameters.

    Note: In Confluence CLI, actions that do allow both --cql and --space. The --space parameter is ignored when --cql is provided in the action command.

    The result: