# Ros2cd - a complete roscd replacement in ros2

**URL:** <https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368>\
**Category:** ROS General\
**Tags:** ros2\
**Created:** [January 2, 2025, 1:35pm UTC](https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368 "2025-01-02T13:35:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin-Oehler](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Martin-Oehler](https://discourse.openrobotics.org/u/Martin-Oehler)\
**Post date:** [January 2, 2025, 1:35pm UTC](https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368/1 "2025-01-02T13:35:15Z")

</div>

Hello everyone,

we recently moved to ros2 but I quickly noticed the lack of an equivalent for the old `roscd`. For me, this was an essential functionality to navigate workspaces efficiently. I know there is [colcon\_cd](https://github.com/colcon/colcon-cd) but it is very limited in functionality and basically only works in the current workspace.

As I could not find any alternative, I just wrote my own:

[https://github.com/Martin-Oehler/ros2cd](https://github.com/Martin-Oehler/ros2cd)

Simply source the bash file and off you go. The provided script replicates the behavior of the old `roscd` command in ros2 (and shares the name because of muscle memory). The current working directory is changed to the same one where `ros2 launch` would execute a launch file from. If a sym-link install is found, it goes to the source directory instead.

Best regards,  
Martin

---

<div class="post-metadata">

**Author:** ![MoffKalast](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/moffkalast/32/6355_2.png) [@MoffKalast](https://discourse.openrobotics.org/u/MoffKalast)\
**Post date:** [January 4, 2025, 10:51am UTC](https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368/2 "2025-01-04T10:51:50Z")

</div>

Amazing, just tested it on Jazzy and Humble and it works great!

You would not believe the string of excuses saying why this is clearly impossible and a fool’s errand to get working I’ve heard every time I’ve asked anywhere why it’s missing in ROS 2. Glad we finally have it and hopefully it’s included as stock in the next release.

My smallest nitpick is that if you `roscd metapackage` it sends you to the `src/metapackage/metapackage` subpackage that defines it which is technically correct, but unlikely to be what one would actually want in that case. Maybe worth adding an automatic `cd ..` if the last two dirs are the same?

---

<div class="post-metadata">

**Author:** ![Martin-Oehler](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Martin-Oehler](https://discourse.openrobotics.org/u/Martin-Oehler)\
**Post date:** [January 6, 2025, 9:30am UTC](https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368/3 "2025-01-06T09:30:56Z")

</div>

I am glad that you like it. I was quite surprised, too, that nobody had written such a script yet.

> Glad we finally have it and hopefully it’s included as stock in the next release.

The default `ros2` command is implemented in Python and python scripts can not change the current working directory, so adding it as a verb there is unfortunately not possible. However, it should be possible to distribute the script as a separate package through the build farm. If there is enough demand, I can look into that.

> Maybe worth adding an automatic `cd ..` if the last two dirs are the same?

I am very hesitant to implement such kind of special behavior. It could lead to unexpected results, especially when package and folder have the same name but it is not a meta package.

---

<div class="post-metadata">

**Author:** ![DLu](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dlu/32/19014_2.png) [@DLu](https://discourse.openrobotics.org/u/DLu)\
**Post date:** [January 10, 2025, 6:27pm UTC](https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368/4 "2025-01-10T18:27:46Z")

</div>

Note: `ros_command` also has [an implementation](https://github.com/MetroRobots/ros_command/blob/743967f5208bd987970e624538fdafa9ce27222d/src/ros_command/ros_command_setup.bash#L8-L30) of `roscd` that also includes tab completions.

---

<div class="post-metadata">

**Author:** ![jlblancoc](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/jlblancoc/32/4731_2.png) [@jlblancoc](https://discourse.openrobotics.org/u/jlblancoc)\
**Post date:** [January 11, 2025, 10:56am UTC](https://discourse.openrobotics.org/t/ros2cd-a-complete-roscd-replacement-in-ros2/41368/5 "2025-01-11T10:56:28Z")

</div>

> [@Martin-Oehler](#):
>
> The default `ros2` command is implemented in Python and python scripts can not change the current working directory, so adding it as a verb there is unfortunately not possible. However, it should be possible to distribute the script as a separate package through the build farm. If there is enough demand, I can look into that.

Then a possibility is to create a new `ros2` sh script that dispatches the `cd` verb to this new script, and the rest to the old `ros2` python script… It’s a non trivial amount of work but if there is interest enough, it’s technically possible.
