How does `*/` work with `ls`?

QuestionsQuestions8 SkillsProDec, 16 2025
0115

You're curious about how */ works specifically with ls! That's a great demonstration of how powerful shell wildcards can be.

When you use ls with */, like in ls */, here's what happens step-by-step:

  1. Shell Expansion (before ls even runs):

    • The * is a wildcard character that matches any sequence of zero or more characters.
    • The / specifically restricts the match to directories.
    • So, */ instructs your shell (like Bash or Zsh) to find names in the current directory that end with a / (meaning they are directories).
    • The shell then replaces */ with the actual list of all immediate subdirectories in the current path.
  2. ls Execution:

    • Once the shell has expanded */ into a list of directory names, the ls command is executed with those directory names as arguments.
    • For example, if your current directory has subdirectories named test and my_docs, the shell will transform ls */ into ls test/ my_docs/.
    • ls then proceeds to list the contents of each of those specified directories.

In simpler terms:

*/ acts as a shortcut to say "look inside all of the folders directly within this current location."

Why is this useful?

  • Quick overview: It allows you to see the contents of multiple subdirectories at once without changing your current directory or running ls command for each subdirectory separately.
  • Efficiency: It saves typing when you want to inspect several directories.

Let's assume your current directory /home/labex/project contains:

  • data_file.txt (a file)
  • test/ (a directory)
  • test_file.txt (a file)
  • another_dir/ (a directory)

If you run ls */:

  1. The shell sees */.
  2. It finds test/ and another_dir/ in the current directory.
  3. It transforms ls */ into ls test/ another_dir/.
  4. ls then lists the contents of test/ and another_dir/ separately.

You can see this in your own command history! When you ran ls -l */, you got output like:

total 0

This indicated that even though the */ correctly identified your subdirectories (like test/, which you created), those subdirectories themselves were currently empty.

It's a fantastic way to quickly get insight into the structure of your file system!

0 Comments

no data
Be the first to share your comment!