How to list only files of a certain type, such as PDF or MP3, using the ls command in Linux?

Published on Aug. 22, 2023, 12:17 p.m.

To list only files of a certain type (e.g. PDF or MP3) in a directory using the ls command in Linux, you can use the wildcard * character combined with the file extension. Here are some examples:

To list all PDF files in the current directory:

ls *.pdf

To list all MP3 files in a subdirectory called “Music”:

ls Music/*.mp3

To list all files of a certain type in all subdirectories of the current directory, you can use the recursive -R option with the wildcard character. For example, to list all PDF files in all subdirectories:

ls -R *.pdf

Note that these commands will only list files with the specified extension, and not directories or other types of files. If you want to include other types of files in your listing, you can combine the above commands with other options such as -l and -a.

Alternatively, you can use the find command to search for files with a specific extension in a directory and its subdirectories. Here is an example command to find all PDF files in the current directory and its subdirectories:

find . -name '*.pdf' -type f

This will display a list of all PDF files in the current directory and its subdirectories, including the full path to each file.

Tags:

ls