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:
-
Shell Expansion (before
lseven 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.
- The
-
lsExecution:- Once the shell has expanded
*/into a list of directory names, thelscommand is executed with those directory names as arguments. - For example, if your current directory has subdirectories named
testandmy_docs, the shell will transformls */intols test/ my_docs/. lsthen proceeds to list the contents of each of those specified directories.
- Once the shell has expanded
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
lscommand 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 */:
- The shell sees
*/. - It finds
test/andanother_dir/in the current directory. - It transforms
ls */intols test/ another_dir/. lsthen lists the contents oftest/andanother_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!