File Exchange

image thumbnail

Recursive directory listing - Enhanced RDIR

version (9 KB) by Thomas Vanaret
Files and directories listing, including recursive and other special features


Updated 27 Jan 2014

View License

Enhanced version of RDIR function, fixing some bugs and adding some features.

- Basic use is similar to Matlab "dir" function, which allow you to list files and directories in a given path :

rdir([matlabroot, '\*.txt'])

When asked, rdir output structure is the same as dir output structure :

d = rdir([matlabroot, '\*.txt'])

- As initial RDIR function, you can use a double wildcard (**) to list files in all subdirectories from a given path :

rdir([matlabroot, '\**\*tmpl*.m'])

- And also use a filter entered as 2nd argument to refine your search :

rdir([matlabroot, '\**\*tmpl*.m'], 'bytes>0')

This enhanced version of RDIR allow you to define filter on all fields of returned structure and also to use functions like "regexp" or "strfind" on "name" field. You can also use a function handle working on dir-like structure to refine the search.

- You can then add as 3rd argument a path to remove from beginning of "name" field of each listed item :

rdir([matlabroot, '\*.txt'], '', 'C:\Program Files\')
All in : C:\Program Files\
76 kb 02-Aug-2007 16:03:52 MATLAB\R2007b\license.txt
631 b 02-Aug-2007 15:18:42 MATLAB\R2007b\patents.txt
413 b 04-Aug-2007 12:16:36 MATLAB\R2007b\trademarks.txt

- ".svn" directories created by SubVersion (SVN) are excluded from the recursive listing.

See published examples for more details on how to use all RDIR capabilities.

Cite As

Thomas Vanaret (2020). Recursive directory listing - Enhanced RDIR (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (33)

Christian Bothe

Tiago Fujii


1) does not handle wrong slashes, eg rdir('A\B/C.txt') returns "A\C.txt".
2) cannot list folders.
3) Matlab's built-in also supports '**', since 2016b, and is 2x faster.

Useful features:
1) omits "." ".." from output.
2) returns full path in the name property (often used)
3) allows filtering on date and file size.

So how about this instead, best of both worlds, for 2016b onward:

function D = dirp(name,filter)
%Same as dir, plus omit '.' '..', allows filtering, returns file path.
% D = rdir(name,filter)
%DANGER: Uses eval, can execute any system command!
% D=dirp('**'); %all (files and folders)
% D=dirp('**\str*\**\*.str') %name filter (files and folders)
% D=dirp('**','~isdir & datenum>now-2') %files modified in last 2 days
D = dir(name); %list
D(ismember({},{'.' '..'}))=[]; %remove '.' and '..'
if nargin>=2 && ~isempty(filter) %filter
bytes = [D.bytes];
isdir = [D.isdir];
datenum = [D.datenum];
try eval(sprintf('D((%s)==0)=[];',filter));
catch, error('Invalid filter "%s"',filter);
t = strcat({D.folder},filesep,{}); [D.file]=t{:}; %file path

Jeffrey Rumschlag

Very useful! I had to change the name of the function because MATLAB was recognizing rdir as something else, but once I did that, it worked well. Thanks!

Marcin Konowalczyk

Nur Fajar Trihantoro


For a basic recursive search this version is almost 2x slower (for me) then the original version by Gus Brown. Perhaps it is worth mentioning this in the description.


Serge Kogan

Adi Navve

Nice and simple. Well done.

Cristina Domnisoru

Thank you :) you've saved me from using the file explorer to delete files

Noam Greenboim

Very useful, I don't understand why this file wasn't adopted by Mathworks in the original version of Matlab.

Ben Kang

Very good and easy-to-use function!

Tom Clark

Excellent, does exactly what it says on the tin and saves a bunch of time writing recursive functions.

A little slow, but it might just be my OS which is kind of cantankerous at the moment, so I'll go ahead with the full 5*. Thanks for your contribution!

Noam Greenboim

Very useful, works great

Muhammad Naveed

not able to use it for mp3 files listing from D:\
kindly help me


Thomas Vanaret

@KRUNAL : use "*" to match any folder from the same path.
Use "**" to match any folder and subfolders from the same path.
See examples for more help




Hey andrew, how did you manage to make it find multiple files with same name which are in different folders?

Alexandre De Barros

Joao Henriques

Steve Goley

Nice implementation and it's nice that it has the same output as "dir". My only grep is that it can be somewhat slow when you're recursing through really dense file structures. This could be my file system or the fact the implementation is pure MATLAB, which is nicely portable, but it would be nice to parse sections out to an option mex file for extra speed.

Ron Lu

Thanks, very useful with regexp support.


Jaroslaw Tuszynski

Very good - I use it all the time.


Thanks, that was really useful in a selective by-size backup

Thomas Vanaret

Thank you Alan for your suggestion.
Problem has been fixed in new version available (2.2).


Sorry... manage to have it working!


Very useful indeed!
I am not sure if I am missing something, but unfortunately I am not able to make it finding multiple files with same name (even if in different folders). This would be a great improvement!

Alan Jennings

Works great and saved me a lot of time.
A suggested improvement, instead of looking at the file size to identify directories, use the isdir flag.
Line 286: if D(ii).isdir %sz<0,
Change display to allow for 0b files
Line 291-293:
ss = max(0,min(4,floor(log2(sz)/10)));
disp(sprintf('%4.0f %1sb %20s %-64s ',...
sz/1024^(ss+eps),pp{ss+1}, datestr(D(ii).datenum, 0), D(ii).name));

Alan Jennings


extremely useful - thanks!


Examples script update only

Version 2.3 :
Adding improvements suggested by X. Mo :
- function handle as TEST input
- code optimisation (avoiding loop)
Fixing possible bug when using a wildcard at the beginning;
Common path as 2nd optionnal output;

Version 2.2
Fixing bug on display with 0b files;
Specific display when no file match filter;

Licence update

MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux