[Novalug] question about ls

Peter Larsen plarsen at famlarsen.homelinux.com
Sun Jan 15 18:12:04 EST 2012


On Sun, 2012-01-15 at 17:27 -0500, Nino R. Pereira wrote: 
> Hi all,
> 
> in my home directory I have a sub-directory called crystals,
> and a file called cryst-1. When I do

Can you do a "tree" command and paste it here? From what you write, you
should NOT get the content of sub-directories by running ls -l. Could
you paste the output of alias -l here? I think you may have some hidden
options on your "ls" command.

Let me show you what I mean. I've created a subdirectory called
crystals, and 9 files there (crystal1-9). I created 3 subdirectories
(sub1,2,3) with 9 files in sub1. The output of "tree" with PWD=
$HOME/crystals I get:

[plarsen at boss crytals]$ tree
.
|-- crystal1
|-- crystal10
|-- crystal2
|-- crystal3
|-- crystal4
|-- crystal5
|-- crystal6
|-- crystal7
|-- crystal8
|-- crystal9
|-- sub1
|   |-- sub1
|   |-- sub10
|   |-- sub2
|   |-- sub3
|   |-- sub4
|   |-- sub5
|   |-- sub6
|   |-- sub7
|   |-- sub8
|   `-- sub9
|-- sub2
`-- sub3

Very standard setup.

I now run "ls -l", still in the crystals directory, I get:

[plarsen at boss crytals]$ ls -l
total 12
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal1
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal10
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal2
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal3
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal4
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal5
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal6
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal7
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal8
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal9
drwxrwxr-x. 2 plarsen plarsen 4096 Jan 15 18:01 sub1
drwxrwxr-x. 2 plarsen plarsen 4096 Jan 15 18:01 sub2
drwxrwxr-x. 2 plarsen plarsen 4096 Jan 15 18:01 sub3

I see all files, and the name of the directories. Not the content of
them.

On the other hand, if I do "ls -l *" the result is quite different - I
will get the sub-directories:

[plarsen at boss crytals]$ ls -l *
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal1
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal10
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal2
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal3
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal4
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal5
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal6
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal7
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal8
-rw-rw-r--. 1 plarsen plarsen    0 Jan 15 18:01 crystal9

sub1:
total 0
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub1
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub10
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub2
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub3
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub4
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub5
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub6
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub7
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub8
-rw-rw-r--. 1 plarsen plarsen 0 Jan 15 18:01 sub9

sub2:
total 0

sub3:
total 0

So why is that? With unix/linux the command line parameters are
interpreted (executed) by the shell BEFORE the command is run. This
means, that ls * is expanded and the complete command executed looks
like:

ls crystal1 crystal2 crystal3 crystal4 crystal5 crystal6 crystal7 \
  crystal8 crystal9 crystal10 sub1 sub2 sub2

Ls is then run for each parameter. The file names are looked up one by
one, and you get a single line. When the sub-directory is read by ls, it
means "content of" - ie. if you do "ls <name of subdirectory>" you get a
list of the files in that subdirectory: 

[plarsen at boss crytals]$ ls sub1
sub1  sub10  sub2  sub3  sub4  sub5  sub6  sub7  sub8  sub9

If there were subdirectories in sub1 I would just see the names of them.

So why is there a <name of subdirectory>: sometimes?? Well, if you pass
more than one directory to ls, in order for you to tell which directory
the files come from, ls tells you by adding a header. So if I do "ls
sub1 sub2 sub3" I get headers:

[plarsen at boss crytals]$ ls sub1 sub2 sub3
sub1:
sub1  sub10  sub2  sub3  sub4  sub5  sub6  sub7  sub8  sub9

sub2:

sub3:


That way I can tell sub2 and sub3 are empty directories and that sub1
contains files.  If sub2 and sub3 had contained files it would be clear
that they belonged to different directories. 

If this wasn't the case, you would have to look at inodes to tell the
files were different. But that wouldn't tell you what directory they
belonged to without the "header".

> 
> ls -l
> 
> I get the file cryst-1, and the files and sub-directories in the 
> directory crystals:
> 
> cryst-1
> 
> crystals
> sub1 sub2 file1 etc.
> 
> But, when I delete the file cryst-1 and do ls again, I get the
> sub-directories in 'crystals':
> 
> sub1 sub2 file1 etc.,
> 
> without an indication that these sub-directories or files are in
> a directory.
> 
> How can I make 'ls' give the sub-directory even when there's no file in
> the directory itself.

As you see on the above, that's default. Unless there's only one
subdirectory and NO other files on the command line, you do not get the
header. 

If you do not want ls to show you the content of a directory but just
the directory name, you need to pass the -d parameter.

-- 
Best Regards
  Peter Larsen

Wise words of the day:
On a normal ascii line, the only safe condition to detect is a 'BREAK'
- everything else having been assigned functions by Gnu EMACS.
	-- Tarl Neustaedter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://calypso.tux.org/pipermail/novalug/attachments/20120115/dd0016e5/attachment.bin 


More information about the Novalug mailing list