Main Content

ls

List folder contents

Description

ls lists the contents of the current folder.

ls name lists the files and folders in the current folder that match the specified name.

example

list = ls(___) returns the names of all the files and folders in the current folder that match the specified name. You can specify list with any of the arguments in the previous syntaxes.

example

Examples

collapse all

List all the files and folders with names that contain my.

ls *my*
my_class.m     my_function.m  my_script.m    my_text.txt

List all the files and folders with a .m extension.

ls *.m
my_class.m       my_function.m    my_script.m      test_function.m

Save a list of the names of all the files and folders in the current folder to the variable MyList.

MyList = ls;

Input Arguments

collapse all

Name of file or folder, specified as a character vector or string scalar. Use the '*' wildcard to match patterns. For example, ls *.m lists all files and folders with a .m extension, and ls m* lists all files and folders with names that begin with the letter m.

Data Types: char | string

Output Arguments

collapse all

List of files and folders, specified as a character array in one of these formats.

  • UNIX® platformslist is a character vector of names separated by tab and space characters.

  • Microsoft® Windows® platformslist is an m-by-n character array of names. m is the number of names and n is the number of characters in the longest name. MATLAB® pads names shorter than n characters with space characters.

Tips

  • To further modify the results of the ls command on UNIX platforms, you can add any flags that the operating system supports. For example, ls -c displays files by timestamp and ls -d displays only directories. For more information, see the UNIX ls documentation.

Alternatives

Use the dir command to return file attributes for each file and folder in the output argument.

You can also view files and folders in the Current Folder browser by issuing the filebrowser command.

Version History

Introduced before R2006a

See Also

|