Unix Shell - ls

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2007 > ls





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author ls
Grant

2007-11-24, 1:38 am

How do I list only directories and no files ?

If I am in / using bash shell
ls -d produces a '.'
and
ls -dl produces
drwxr-xr-x 18 root root 4096 Oct 4 02:55 .
only

????


thanx
wyhang@gmail.com

2007-11-24, 1:38 am

On Nov 24, 8:28 pm, Grant <vi...@clear.net.nz> wrote:
> How do I list only directories and no files ?
>
> If I am in / using bash shell
> ls -d produces a '.'
> and
> ls -dl produces
> drwxr-xr-x 18 root root 4096 Oct 4 02:55 .
> only
>
> ????
>
> thanx


ls -l | grep -e "^d" | awk '{print $9}'
Janis Papanagnou

2007-11-24, 1:38 am

Grant wrote:
> How do I list only directories and no files ?


ls -d */

>
> If I am in / using bash shell
> ls -d produces a '.'
> and
> ls -dl produces
> drwxr-xr-x 18 root root 4096 Oct 4 02:55 .
> only
>
> ????
>
>
> thanx

franzi

2007-11-24, 1:38 am

On 24 Nov, 04:31, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:[vbcol=seagreen]
> Grant wrote:
>
> ls -d */
>
>
>
>
>

franzi

2007-11-24, 1:38 am

On 24 Nov, 04:31, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:[vbcol=seagreen]
> Grant wrote:
>
> ls -d */
>
>
>
>
>

sorry if i'm getting in to the question,but i tried that script
ls -l | grep -e "^d" | awk '{print $9}'
and i tried to chenge grep -e "d"|awk '{print $123456789}'
so what's the meaning of ^ and the meaning of 123456789,i mean how can
awk manage the print $1 or print $2
does it interact to the c libraries or whatelse?
Grant

2007-11-24, 1:38 am

Janis Papanagnou wrote:[vbcol=seagreen]
> Grant wrote:
>
> ls -d */
>


Thanks


ls -d */

works but I don't understand why it does and the expected `ls -d` doesn't

????

`man ls` doesn't suggest this
Janis Papanagnou

2007-11-24, 7:31 am

Grant wrote:
> Janis Papanagnou wrote:
>
>
>
>
> Thanks
>
>
> ls -d */
>
> works but I don't understand why it does and the expected `ls -d` doesn't
>
> ????
>
> `man ls` doesn't suggest this


My man page does...

For specifying ls or ls -d without files it says...

"the current directory by default"

So ls or ls -d is equivalent to ls . or ls -d . respectively.
And for specifying option -d it says...

"list directory entries instead of contents"

The contents of directory . is the files you see and the entry itself
is the plain . (dot).

Janis
Janis Papanagnou

2007-11-24, 7:31 am

franzi wrote:
> On 24 Nov, 04:31, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> sorry if i'm getting in to the question,but i tried that script
> ls -l | grep -e "^d" | awk '{print $9}'
> and i tried to chenge grep -e "d"|awk '{print $123456789}'


What did you try to achieve by that expression?

> so what's the meaning of ^ and the meaning of 123456789,i mean how can


If you look into the docs you see that grep is expecting a
regular expression, and in regular expressions a ^ at the
front means to match if the subsequent string starts the
line.

A simple d will match any d on the line while ^d will match
a d at the front of the line.

> awk manage the print $1 or print $2


If you read the basics about awk you'll learn that $1 and
$2 are the first and the second field of the records that
are given to awk.

If you feed the output of ls -l it $9 will take the nineth
field from that output.

> does it interact to the c libraries or whatelse?


No, it has nothing at all to do with that.

Janis
franzi

2007-11-24, 7:31 am

On 24 Nov, 10:09, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> franzi wrote:
>
>
>
>
>
>
>
>
> What did you try to achieve by that expression?
>
>
> If you look into the docs you see that grep is expecting a
> regular expression, and in regular expressions a ^ at the
> front means to match if the subsequent string starts the
> line.
>
> A simple d will match any d on the line while ^d will match
> a d at the front of the line.
>
>
> If you read the basics about awk you'll learn that $1 and
> $2 are the first and the second field of the records that
> are given to awk.
>
> If you feed the output of ls -l it $9 will take the nineth
> field from that output.
>
>
> No, it has nothing at all to do with that.
>
> Janis- Nascondi testo tra virgolette -
>
> - Mostra testo tra virgolette -


thanks very much for the replay
Wayne

2007-11-24, 7:31 am

Janis Papanagnou wrote:
> Grant wrote:
>
> My man page does...
>
> For specifying ls or ls -d without files it says...
>
> "the current directory by default"
>
> So ls or ls -d is equivalent to ls . or ls -d . respectively.
> And for specifying option -d it says...
>
> "list directory entries instead of contents"
>
> The contents of directory . is the files you see and the entry itself
> is the plain . (dot).
>
> Janis


You're not alone in having difficulty with this. I
tracked it down on Linux and the SUS/POSIX documentation.
It turns out not to have anything to do with the
utility you use, it has to do with the OS's method
of resolving pathnames, in this case pathnames that
end in a symlink that refers to a directory.

Anyway, on Linux see man page for path_resolution(2),
the section on trailing slashes. Basically it says
if the last component of a pathname is a slash, the
pathname must match a directory.

In the "Pathname Resolution" section of the POSIX standard
it states in section 4.11 (in part):
....
If a symbolic link is encountered during pathname resolution,
the behavior shall depend on whether the pathname component
is at the end of the pathname and on the function being
performed. If all of the following are true, then pathname
resolution is complete:

1. This is the last pathname component of the pathname.
2. The pathname has no trailing slash.
3. The function is required to act on the symbolic link
itself, or certain arguments direct that the function
act on the symbolic link itself.

In all other cases, the system shall prefix the remaining
pathname, if any, with the contents of the symbolic link.
....

Or in simpler terms, a trailing slash forces symlinks to
be expanded. With no trailing slash, it depends on the
system call in question (e.g., lstat(2) operates on the
symlink but stat(2) operates on the file/directory the
symlink points to), and whether or not the symlink refers
to a directory.

-Wayne
Maxwell Lol

2007-11-24, 7:31 am

Grant <virus@clear.net.nz> writes:

> ls -d */
>
> works but I don't understand why it does and the expected `ls -d` doesn't



* - matches all files
*/ - matches all directories because only
directories have an optional "/" as part of the name.


Both these commands do the same

cd /usr/
cd /usr

Glenn Jackman

2007-11-24, 1:21 pm

At 2007-11-24 07:28AM, "Grant" wrote:
> How do I list only directories and no files ?
>
> If I am in / using bash shell
> ls -d produces a '.'


ls -d *

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com