ls: Do not show directories that match same pattern in wildcard searches, only files

13,978

Solution 1

Looks like your question is "How to list files by pattern excluding directories with ls only".

There is no way to do it with pure ls. You can combine ls + grep like:

ls -ld *2010* | grep -v '^d'

However it's much better to use find for that:

find . -maxdepth 1 -type f -name "*2010*"

Solution 2

It's not possible to selectively filter out directories using only ls. You need either find or ls | grep, as described in rush's answer . But for your specific example, to answer the question you asked in your comment:

So again: I want the tax* files in the current directory to be listed, but the directory "invoices2010" which resides in the same working directory and which also matches the given 2010 wildcard pattern should be skipped in the listing.

you can do

ls -l --ignore='invoices*' *tax2010*

which filters out anything matching the ignore shell pattern.

Solution 3

ls -dlp *2010* would be a good start to the solution, but it depends what you want the output to look like.

Solution 4

Use zsh…

ls -l *2012*(.)

The glob qualifier . means regular files only.

Share:
13,978

Related videos on Youtube

syntaxerror
Author by

syntaxerror

Shyy erny anzr - Naqernf Rvonpu

Updated on September 18, 2022

Comments

  • syntaxerror
    syntaxerror over 1 year

    Supposing I have something like the following, a typical business PC situation:

    drwxr-xr-x 1 whatever whoever       3 Oct  3 16:40 invoices2009
    drwxr-xr-x 1 whatever whoever       4 Oct  3 16:40 invoices2010
    drwxr-xr-x 1 whatever whoever       2 Oct  3 16:40 invoices2011
    -rwxr-xr-x 1 whatever whoever  440575 Oct  3 16:40 tax2010_1
    -rwxr-xr-x 1 whatever whoever  461762 Oct  3 16:40 tax2010_2
    -rwxr-xr-x 1 whatever whoever  609123 Oct  3 16:40 tax2010_3
    

    Now let's be lazy and just type:

    $ ls -l *2010*
    

    Supposing that there is something in the invoices2010 directory, it won't work as expected. Since the directory name contains the 2010 year as well, ls will also list the files in invoices2010, although I only want to list those in the current directory. Even funnier: imagine the tax2010* files weren't there at all and there were not those three directories as in the example, but 50 of them. Yes I've tried it out: ls will not even indicate which files are in which directory, but simply list them top-down, just as if all files resided in the current directory (unless you explicitly specify the -R option, certainly I do know that)

    Plus, I know that I can do this with find, too, but is there also any way to accomplish this task with a plain ls one-liner (which, obviously, has a far less complicated syntax)?

  • syntaxerror
    syntaxerror over 11 years
    No, it would not be a good start at all. In my example, I wish to have the tax* files listed, whilst your solution would simply list all directories that match the pattern without showing their file content. That's something completely different.
  • Yoshidk
    Yoshidk over 11 years
    I'm not really sure what you want the output to look like. You said you only want files in the current directory to be listed. Try also ls -ldp *2010
  • syntaxerror
    syntaxerror over 11 years
    There is actually nothing much to "edit". So again: I want the tax* files in the current directory to be listed, but the directory "invoices2010" which resides in the same working directory and which also matches the given *2010* wildcard pattern should be skipped in the listing.
  • syntaxerror
    syntaxerror over 11 years
    Ah! I should have thought about greping for ^d. I didn't think of that, obviously. Thank you.
  • manatwork
    manatwork over 10 years
    \dev\null?!?!
  • Quido
    Quido about 3 years
    Not what the author asked, but it is what I needed when Google found this answer for me. +1.