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)?
Best Answer
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 combinels
+grep
like:However it's much better to use
find
for that: