Score:1

`ls` with wildcard with subdirectories

au flag
Tom

When I execute a command ls *, it will show

  1. files in the current directory
  2. directories in the current directories
  3. files and directories in the directories in the current directories.

When I do use ls I don't want to see inside the directories in the current directory. I'm bit confused because for more than 8 years I've been using ubuntu, I've never seen anything like this before.

Is this how ls should work? Is there a way to stop 3 from happening?

For example, if I have file1.file folder1 folder2 folder3 textfile1.txt

I want ls f* to show only

file1.file
folder1
folder2
folder3

Ubuntu 20.04 LTS

Doug Smythies avatar
gn flag
`ls -l -d *` should do what you want.
sudodus avatar
jp flag
+1 for Doug Smythies's tip, or even simpler (with only the names) `ls -d *` and `ls -d f*`
Score:0
id flag

One way is to use the find command instead of ls. You can use -maxdepth 1 so that you don't read into folders, and use only the -name option for wild cards.

Example:

find . -maxdepth 1 -name 'f*'

Will return everything in the current folder . starting with a lower case f even folder names, but not show you any contents within the folder(s).

Score:0
cn flag

You are expecting it to work like in MS-DOS, however, this is linux.

Wildcards in linux are expanded by your shell to match all items fitting the wildcards. ls does not see your wildcard. It only sees a list of file and directory names matching the wildcard. So it will show the contents of all items listed, i.e., the name of a file or the contents of a folder. In MS-DOS, wildcards would cause the dir command itself to filter the list to list only names fitting the wildcard.

To filter the output of ls, e.g., to only see file and folder names matching f*, use grep, i.e., pipe the output of ls into grep like:

ls | grep ^f.*

^ and .* are regular expressions. ^f means: f but only at the very start. .* means . any character * any number of times.

Tom avatar
au flag
Tom
Thank you for the answer. I don't know why this was never an issue for me until now.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.