-6

I'm trying to list files that start with "s" and with "l".

The command I'm trying is ls -l *sfa*

  • 1
    @muru, after the edit the question is very confusing. The suggested command doesn't relate to the question. Dragonslayer, please clarify. – laugh Mar 21 '16 at 6:34
  • @laugh what's so particularly confusing about adding the command-line and bash tags? OP is using the command line, and the default shell is bash. – muru Mar 21 '16 at 6:37
  • 2
    From your comment to cmks this is a homework question. We are not here to do your homework. – andy256 Mar 21 '16 at 8:03
  • Homework typically comes with a textbook and a teacher. Have you tried looking into those ? – Sergiy Kolodyazhnyy Mar 21 '16 at 8:08
  • @muru - sorry, I didn't realize there was another edit before yours that changed the question text significantly. However "bash" is irrelevant here, wildcards are pretty much the same in all shells (and the OP may be using another shell - he didn't indicate bash). – laugh Mar 21 '16 at 10:11
4

The question is not very clear... based on your comment to @cmks, here are two options:

  1. If you are looking for files where the first character of the filename is "s" and the last is "l" - then use ls -l s*l

  2. If you are looking for files where the first character of the filename is (either "a" or "s") and the last is (either "a" or "l") - then use ls [as]*[al]

If it's something else, please edit your question to clarify...

Your version ls -l sfa will only list files with the exact name "sfa" (or, if there's a directory with that name, any files in that directory). It doesn't do what you seem to be looking for.


The rules for matching file names ("globbing") are quite simple:

  1. "?" matches any single character
  2. "*" matches any number (zero or more) of characters
  3. Use square brackets ([]) to match a single character from a collection: [fqsb] matches either f, q, s, or b; [b-d] matches b, c, or d
  4. Use curly braces ({}} to match one of several specific strings: {foo,bar} matches either "foo" or "bar".

The directory separator "/" is excluded from matching - matching is only done on filenames.

See also ShellGlobbing in Ubuntu help.

1
ls -l s*l
ls -l s?l
  • A "*" means 0 or more characters
  • If you want an exact amount of letters between the "s" and the "l" a "?" means 1 char. 2 "??'s means 2 characters.
  • You can also put collections in between []. So ls -l s[a-z]l would limit it to sal through szl

From comments:

If you want files beginning with "s" and "a" you can use a collection:

ls -l [sa]*

will list all files beginning with those 2 letters. You can use that to create all kinds of groups. Exampe [sa][df][1ojwfihwef]* would make it search for file beginning with 3 letters that each match that specific group

0

You may give more than one parameter to the ls command at a time:

ls -l H* *l

What happens is, the shell does a Pathname Expansion. You may read more about it in the manual pages of bash:

man bash

Because the shell does it, the shell calls the command ls as follows:

ls -l Hejo Hesja Hejan Lesl Lesil Lesul Lestl

It is imported to understand, it is not the ls command but the shell who looks for matching pathnames.

  • Didn't work. I can do it this way tho, ls -l s* ; ls -l a* but i dont think my teacher would enjoy that. – Dragonslayer Mar 20 '16 at 21:59
  • That comment makes the question different than it originally was. – Rinzwind Mar 21 '16 at 7:58
  • 1
    @Dragonslayer: Please give us a more precise hint what files you are looking for. What is the first char, what is the last, and what else should be repected. Three Supporter edited your post to clarify that, maybe they are wrong. – cmks Mar 21 '16 at 8:00
  • I agree with @cmks :-) and 1 addition: edit your question ;-) – Rinzwind Mar 21 '16 at 8:00
  • Okay sorry, but i will try to clarify now. I'm in a folder right now. Which i have the files Hejo Hesja Hejan och Lesl Lesil Lesul Lestl. I want to list every file that start with a H and ends with a l and i can accomplish it while doing ls -l H* ; ls -l *l but i dont know if i can combine them. – Dragonslayer Mar 21 '16 at 13:12
0

Aside from using ls command and shell's wildcards, one could use find command as well.

$ find -maxdepth 1 -iname "s*l"                                                                       
./script.pl
./storm carl
./sal
./simple_curl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.