Select Files Based On Filename

Often we want to select files based on their file name or file extension. In Linux, we can use wildcard characters to accomplish this.

Create Some Files

touch hello_world.txt
touch HELLO_world.txt
touch HELLO_world.md
touch hello_america.txt
touch goodbye_america.txt
touch goodbye_america_2.txt

View Directory Contents

ls
goodbye_america_2.txt  goodbye_america.txt  hello_america.txt  hello_world.txt  HELLO_world.txt

Select Files Containing “Hello”

ls hello*
hello_america.txt  hello_world.txt

Select Files Starting With “g” or “i”

ls [gi]*
goodbye_america_2.txt  goodbye_america.txt

Select All Markdown Files

ls *.md
HELLO_world.md

Select All Files That Are Markdown Or Text

ls *.md *.txt
goodbye_america_2.txt  goodbye_america.txt  hello_america.txt  HELLO_world.md  hello_world.txt  HELLO_world.txt

Select All Files With A Two Character File Extension

ls *.??
HELLO_world.md

Select All Files With A Three Character File Extension

ls *.???
goodbye_america_2.txt  goodbye_america.txt  hello_america.txt  hello_world.txt  HELLO_world.txt

Select All Files Containing A Digit

ls *[0-9]*
goodbye_america_2.txt