[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: pathname expansion part two
From: |
Bob Proulx |
Subject: |
Re: pathname expansion part two |
Date: |
Fri, 15 Oct 2010 13:13:33 -0600 |
User-agent: |
Mutt/1.5.20 (2009-06-14) |
javajo91 wrote:
> "For example, if you wanted to list all of the files in the directories /usr
> and usr2, you could type ls /usr*.
Because the '*' is a file glob. It is called a glob because it
matches a glob of characters. The process of the expansion is called
globbing. "/usr*" matches "/usr" and "/usr2" both. That is expanded
on the command line.
$ ls /usr*
is the same as
$ ls /usr /usr2
The ls command never sees a '*' because the shell expands it first.
You can use echo to see what the shell has expanded.
$ echo foo /usr*
foo /usr /usr2
> If you were only interested in the files
> beginning with the letters b and e in these directories, you could type ls
> /usr*/[be]* to list them."
The /usr* matches and expands to /usr /usr2 and then the [be]* matches
"bin" and "etc". The result is the same as
$ ls /usr/bin /usr/etc
And so the contents of those directories are listed. Again you can
see this using echo. [Most people won't have a /usr/etc on their
system though and so that would not expand for them.]
$ echo foo /usr*/[be]*
/usr/bin /usr/etc
> When i type /usr*/[be]* i do not get all the files within /usr that begin
> with a b or an e but instead get ALL the files within /usr/bin and /usr/etc.
Because you are asking ls to list those directories they are being
listed for you. If you want to ask ls to list the directory arguments
as a file instead of as a directory then you can add the -d option.
(But it is almost the same as echo in that case.)
$ ls /usr*/[be]*
...contents of /usr/bin and /usr/etc...
$ ls -d /usr*/[be]*
/usr/bin /usr/etc
Also remember that directories are simply files in a Unix filesystem.
They are special files and have special properties but files just the
same.
Bob