ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Find the name of package containing a file, given the file's path

asked 2011-03-28 21:47:52 -0600

I would like to find the package a file is in with a shell command. Background is, I want to use "rospack cflags-only-I <package-name>" to get a list of directories to look for c++ headers to build ctags for omnicompletion. See this question. Currently I need to set the pwd of the editor to the package dir. Would be nice to get rid of that limitation.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2011-03-29 09:44:05 -0600

dornhege gravatar image

updated 2011-03-30 00:52:21 -0600

I don't know any ros command, but the following script works. It's quite slow though using rospack list and just checking prefixes (so half filenames will work too).


for i in $(rospack list | awk '{print $2}'); do
   pkg_name=$(basename $i)
   if [[ ${pkg_file} == ${i}* ]]; then
      echo ${pkg_name}
      exit 0

Searching for manifest.xml:

pkg_dir=$(dirname ${pkg_file})

pushd . > /dev/null

cd ${pkg_dir}

while [[ ! -e manifest.xml ]]; do
   cd ..
   if [[ $(pwd) == "/" ]]; then

if [[ -e manifest.xml ]]; then
   echo $(basename `pwd`)

popd > /dev/null
edit flag offensive delete link more


I'm not sure what makes a directory a package, but alternately you could just cd to the file's directory and then do 'cd .." until there is a manifest.xml.
dornhege gravatar image dornhege  ( 2011-03-29 09:48:36 -0600 )edit
A directory is a package if it contains a manifest.xml file.
tfoote gravatar image tfoote  ( 2011-03-29 11:08:00 -0600 )edit

answered 2011-03-29 11:07:46 -0600

tfoote gravatar image

The get_pkg_dir in roslib.packages is a way to do it in python

edit flag offensive delete link more

Question Tools


Asked: 2011-03-28 21:47:52 -0600

Seen: 2,395 times

Last updated: Mar 30 '11