ls with regexp


Updated 10 Aug 2017

View License

wrapper for ls, which lists the files in one or more folders
patterns can use combinatorial logic (and, or, not, etc)
patterns can use regexp or ls syntax
output can be a structure (ala ls), or a cell array of filenames.
list all files with extensions txt and m
[textfiles,mfiles] = lsr('*.txt','*.m');
m-files starting with A or B, names only (two examples)
foo = lsr('A* or B* and *.m','-name')
foo = lsr('^[A|B].*\.m$','-name')
txt files larger than 1MB and all mat files
foo = lsr('size>1mb and txt or mat')

Cite As

Christopher Pedersen (2023). lsr (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on File Operations in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

changed recursion of subfolders to use fileattrib instead of genpath
added -nomacfile to supress automatic use of -macfile option on mac OS systems

New warnings when receiving patterns that might be ambiguous.
New output option to return patterns
Fixed recursion of subfolders under windows
Added a warning when trying to use a range of matches (e.g. \d{1,3} to catch 1-3 digits), as this does not parse properly in lsr.

fixed bug where using -root and -path option together caused filenames to start with / or \

changed -root to be an optional output
help updated for clarification.