How to filter `ls` output?

QuestionsQuestions8 SkillsProBasic Files OperationsOct, 14 2025
0338

You can filter the output of the ls command using various options and tools. Here are some common methods:

1. Using Options:

  • By File Type: Use -d to list only directories.

    ls -d */
    
  • By File Extension: Use a wildcard to filter by extension.

    ls *.txt
    

2. Using grep:

You can pipe the output of ls to grep to filter results based on patterns.

ls | grep "pattern"

For example, to find files containing "report":

ls | grep "report"

3. Using find:

For more complex filtering, use the find command.

find . -name "*.txt"

This command finds all .txt files in the current directory and its subdirectories.

If you have more questions or need further assistance, feel free to ask!

0 Comments

no data
Be the first to share your comment!