Rosanswers logo

Is there a simple way to select multiple topics (e.g. all topics from a node or (sub)namespace) with some sort of search pattern in rosbag2? The previous ROS(1) rosbag implementation did allow to use regular expressions of the form

$ rosbag record -e "/(.*)_foo/bar"

I can not find anything like that in rosbag2 CLI or documentation. However, this looks like a pretty common use case. Am I missing something or is there a more generic way of doing just this in ROS2.

Originally posted by Phgo on ROS Answers with karma: 218 on 2020-10-20

Post score: 0

Original comments

Comment by timok on 2020-11-11:
Wondering the same thing. There is no issue in rosbag2 repo for regex. I thought that ros2 targets to have feature parity with ros(1). Is it not so?

Comment by timok on 2020-11-11:
I added issues for -e and -x options for rosbag2


2 Answers 2


Rosanswers logo

To sum this up, it looks like this feature is simply not yet implemented and there is at least no obvious generic way in ROS2 of achieving this.

You can follow up the corresponding issue on the rosbag2 repo.

If you find a suitable workaround feel free to add another answer.

In the meantime the feature was added and is available for galactic with rosbag -e and -x option. For older distributions you might want to consider the workaround provided by @gilaadb

-e REGEX, --regex REGEX  
                  recording only topics matching provided regularexpression  
-x EXCLUDE, --exclude EXCLUDE  
                      exclude topics matching provided regular expression.  
                      Works with -a and -e, subtracting excluded topics

Originally posted by Phgo with karma: 218 on 2021-01-08

This answer was ACCEPTED on the original site

Post score: 1


Rosanswers logo

This is how I would go about doing it in ROS2:

ros2 bag record `ros2 topic list | grep --regexp="/.*_foo/bar"`

Of course you can add more topics manually if you want:

ros2 bag record `ros2 topic list | grep --regexp="/.*_foo/bar"` /kuku /muku

Originally posted by gilaadb with karma: 21 on 2021-11-30

This answer was NOT ACCEPTED on the original site

Post score: 2

Original comments

Comment by timok on 2021-12-01:
The workaround works only if the topics exist at the start of recording.

Comment by bvaningen on 2022-02-16:
To get this to work in a python launch file for ROS2 Foxy use the following:

        cmd=['sleep 5; ros2 bag record `ros2 topic list'
             ' | grep --regexp="/depth_camera/*" -v`'],

the sleep 5 is to ensure that the system has time to startup all nodes, since the command for recording will not find any new topics that do not show up at startup


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.