[Novalug] question about ls

Nino R. Pereira ninorpereira at gmail.com
Mon Jan 16 08:55:45 EST 2012


Thanks to all who replied.

I'm sorry to say that I mislead you. My command was not simply

ls -l

but it was

ls -l cryst*

Then, when I have a file called 'cryst-1' and a directory called 'crystals',
I get the files and sub-directories in 'crystals'. But, when I don't 
have a file
called 'cryst-anything' I only get the files and sub-directories in 
'crystals',
without any indication that those are part of a specific directory.

I didn't know about the command 'tree' that Peter mentioned. This is indeed
a nice one. I didn't have it, so I downloaded it. It almost does what 
I'd like to see:

pereira at steve:~$ tree -L 1 cryst*
crystals
├── accounting
├── orders
├── quartz
└── technical

It adds what is missing from ls -l, namely the name of the directory:

pereira at steve:~$ ls -l cryst*
total 16
drwxr-xr-x 2 pereira pereira 4096 2012-01-15 16:31 accounting
drwxr-xr-x 11 pereira pereira 4096 2011-10-01 11:00 orders
drwxr-xr-x 2 pereira pereira 4096 2012-01-13 17:59 quartz
drwxr-xr-x 3 pereira pereira 4096 2011-11-20 18:24 technical

When I add a file, ls gives me exactly what I want, like so:

pereira at steve: touch crysta-
pereira at steve:~$ ls cryst*
crysta-

crystals:
accounting orders quartz technical

except that it has that one pesky file that may or may not be there in 
real life.
Without this file I get:

pereira at steve:~$ rm crysta-
pereira at steve:~$ ls cryst*
accounting orders quartz technical
pereira at steve:~$

where there's no indication of the directory's name that contains the 
sub-directories
(these are blue on my screen: "alias ls='ls --color=auto' ").

What option do I have to add to 'ls' to make the directory name appear?
It's entirely possible that I overlooked the option in the man pages: 
there are
so many options....

Thanks again. Tree is wonderful.

Nino



'm wasn't you On 01/15/2012 10:46 PM, Jon LaBadie wrote:
> On Sun, Jan 15, 2012 at 05:27:16PM -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
>>
>> 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.
> I was going to say much the same as Peter.  The behavior you
> describe can not be coming from a simple "ls -l" with no file
> name arguments.  (note, in this context file name is generic,
> i.e. file, directory, socket, device, FIFO, etc names)
>
> If given no file names ls uses its default file name, ".".
> In that case it would only list the contents of the current
> directory, not any subdirectory contents.
>
> If you gave it a single argument, eg. "crystals", it will list
> the contents of "crystals" without telling you the name.  You
> must know the directory name because you gave it to ls :)
>
> But if you give ls multiple names, and any of them are directories
> then ls tells you the directory name.  Doesn't matter that they
> are "subdirs" of the current directory, just that they are dirs.
> Ex. "ls /tmp /var" would show you the names and contents.
>
> So your simple "ls -l" must be expanding to something in both
> cases you describe.  In the first case to two names, in the
> second to just crystals.  Thus Peter asked to check if your
> environment might have "-l" aliased to something.  I'd also
> check if you have "ls" aliased to something.  So try "type ls"
> and/or "alias ls" to check it.  Sounds like your ls might be
> aliased to "ls *".  If this is the case you might get a surprise
> in an empty directory, eg. "*: no such file or directory".
>
> In answer to your specific question, how to get it to list
> the directory name in your second example you would need to
> make sure it always had multiple arguments.  For example,
> if you aliased ls to "ls * ''" so it got a second, empty
> argument ('').  But then you would always get an error
> message "'': no such file or directory".  This could be
> thrown away by adding "2>  /dev/null" to the alias, but
> that would also throw away legitimate error messages.
>
> jl



More information about the Novalug mailing list