All about rosbags you ever wanted to know!

Robot Operating System (ROS) is cool. Rosbags are cooler. So here’s everything about a rosbag that you ever wanted to know!


First some customary basic info. A bag is a file format in ROS for storing ROS message data. So all messages being published on topics are stored in a .bag file with timestamps that can be played back according to will of the user. So if you’re running your robot upon ROS, then practically you can simulate every physical condition encountered by the bot by storing the sensor values, messages, commands and literally every message type in a ROS compatible format.

Recording a rosbag

To record all topics, enter in a new terminal:

rosbag record -a

and press Ctrl+C when done. Your bag would be stored as a <timestamp>.bag in the current folder, e.g. 2015-12-03-31-15.bag

Recording specific topics

Enter in a terminal window

rosbag record rosout cmd_vel vn_ins

Replace rosout, cmd_vel, vn_ins etc. with topic names

Playing back rosbags

Enter in a window

rosbag play <Enter Name>.bag

The rosbag will play for the time for which you’ve recorded it for.

To infinitely loop it, add -l at the end.

Changing the name of bag

Enter in a new window

rosbag record -O <Enter Name>.bag /chatter /vn_ins

Displaying Information about a bag file

Enter in a new terminal

rosbag info <Enter Name>.bag

which will summarize the length, topics and timestamp of the bag

Playing bag for a specific time


rosbag play -u 240 recorded1.bag

where 240 is the value in seconds upto which the bag is to be played

Checking bag for consistency and playability

rosbag check old.bag

Saving bag data in a text file

This is a particularly useful  command to save the data of a topic in a file called data.txt

rostopic echo -b <filename>.bag -p /topic > data.txt

Visualizing every topic in a file

rqt_bag is a great tool to make visualizing the data in a bag file fairly easy. It is by no means similar to the familiar rqt_plot which has a fairly different GUI. It supports quite a lot of message types as well.

Accessing Bag files with Python

ROSbags can be accessed easily just like normal files with their python API. I’m attaching the sample code as on There is a similar C++ API as well.

import rosbag
from std_msgs.msg import Int32, String

bag = rosbag.Bag('test.bag', 'w')

    str = String() = 'foo'

    i = Int32() = 42

    bag.write('chatter', str)
    bag.write('numbers', i)

Hope this mini-guide helps you out in your next ROS project.

Leave a Reply

Your email address will not be published. Required fields are marked *