function::reverse_path_walk - Linux


function::reverse_path_walk is a C++ function that performs a reverse path walk on a local file system. It recursively traverses the directory tree starting from the specified path, visiting each directory and file in reverse order.


int reverse_path_walk(const fs::path& p, std::function<int(const fs::path&)> callback);




// Visit all files and directories in reverse order
int main() {
  fs::path path("/tmp");
  auto status = reverse_path_walk(path, [](const fs::path& p) {
    if (fs::is_directory(p)) {
      std::cout << "Directory: " << p << std::endl;
    } else {
      std::cout << "File: " << p << std::endl;
    return 0;
  if (status != 0) {
    std::cerr << "Error walking path: " << status << std::endl;
  return 0;

Common Issues

  • Error code -1: The path provided is not a valid path.
  • Error code -2: An I/O error occurred while traversing the path.


function::reverse_path_walk can be used in conjunction with other Boost.Filesystem functions for advanced file and directory manipulation tasks. For example, it can be used with directory_iterator to iterate over the files and directories in a path in reverse order.

Related Commands

  • fs::recursive_directory_iterator: Performs a forward path walk on a directory tree, visiting all files and directories in depth-first order.
  • fs::directory_iterator: Iterates over the files and directories in a directory.