Re: question about ls

2018-10-03 Thread Greg Wooledge
On Wed, Oct 03, 2018 at 12:01:02PM +, Virgo Pärna wrote:
> On Thu, 27 Sep 2018 09:14:00 -0400, Greg Wooledge  wrote:
> > But also note that using ls -l gives a completely wrong answer.
> >
> > wooledg:~$ mkdir /tmp/x && cd "$_"
> > wooledg:/tmp/x$ touch $'this\nis\none\nfile'
> > wooledg:/tmp/x$ ls -l | wc -l
> > 5
> >
> 
>   What about 'ls -1b | wc -l'?

Not portable, but if you're willing to write scripts that only work with
GNU coreutils installed, maybe an option.  You'd probably want to add -A
as well.

How about just using one of the bash builtin methods that are presented
on ?  Not only are they portable
(so long as bash is installed), but they don't require forking external
commands like ls(1) and wc(1) in the first place.



Re: question about ls

2018-10-03 Thread Virgo Pärna
On Thu, 27 Sep 2018 09:14:00 -0400, Greg Wooledge  wrote:
> But also note that using ls -l gives a completely wrong answer.
>
> wooledg:~$ mkdir /tmp/x && cd "$_"
> wooledg:/tmp/x$ touch $'this\nis\none\nfile'
> wooledg:/tmp/x$ ls -l | wc -l
> 5
>

What about 'ls -1b | wc -l'?

-- 
Virgo Pärna 
virgo.pa...@mail.ee



Re: question about ls

2018-09-28 Thread Richard Hector
On 29/09/18 12:21 AM, Greg Wooledge wrote:
> On Fri, Sep 28, 2018 at 09:06:59AM +0200, Ionel Mugurel Ciobîcă wrote:
>> On 28-09-2018, at 17h 52'07", Richard Hector wrote about "Re: question about 
>> ls"
>>> Eww. Tab completion also gets screwed up by this:
>>>
>>> richard@zircon:~/test$ rm 
>>> file  isone   this
> You're probably using "bash-completion", which is a separate project
> from bash, and is known to have many flaws.
> 
>> I am not a bash user, but if I try what you suggest, Richard, I see
>> the file as "this^Jis^Jone^Jfile" with tab completion (on one line).
> And in regular bash (without bash-completion), I see this:
> 
> wooledg:/tmp/x$ rm 'this
> is
> one
> file' _
> 
> where _ is the cursor.
> 

Seems likely. Maybe I should file a bug ... but the list is already a
mile long, and it's a (hopefully) unusual case that I've never run into
in ordinary usage.

Richard



signature.asc
Description: OpenPGP digital signature


Re: question about ls

2018-09-28 Thread Greg Wooledge
On Fri, Sep 28, 2018 at 09:06:59AM +0200, Ionel Mugurel Ciobîcă wrote:
> On 28-09-2018, at 17h 52'07", Richard Hector wrote about "Re: question about 
> ls"
> > Eww. Tab completion also gets screwed up by this:
> > 
> > richard@zircon:~/test$ rm 
> > file  isone   this

You're probably using "bash-completion", which is a separate project
from bash, and is known to have many flaws.

> I am not a bash user, but if I try what you suggest, Richard, I see
> the file as "this^Jis^Jone^Jfile" with tab completion (on one line).

And in regular bash (without bash-completion), I see this:

wooledg:/tmp/x$ rm 'this
is
one
file' _

where _ is the cursor.

> P.S. What doesn't mean the $'foo\nbar' construction? In tcsh it says
> "Illegal variable name." and in history it is recalled as $\'foa\nbar'
> instead. I see that ksh also behave as bash with this $'foo\nbar'
> entity.

In bash, $'...' is a special form of quoting which acts like
single-quoting except that certain backslash+letter sequences are expanded
in a way that's similar to C's strings.  For example, \n expands to a
newline character, and \t to a tab.  See the man page for full details.

wooledg:~$ printf %s $' \t\n' | od -tx1 -An
 20 09 0a

It's the cleanest way to specify special characters in an argument string
or a variable assignment.



Re: question about ls

2018-09-28 Thread Michael Stone

On Fri, Sep 28, 2018 at 08:58:05AM +0200, to...@tuxteam.de wrote:

ls should be considered a user interface, not an API.


One should strive to minimize these differences, though. This is, of
course, just my opinion, but you can pry... (you know the rest ;-)


Sure you can--you use find. There are just too many different ways 
things can fail if you use ls, and too many ways that even things people 
think will help will fail because of differences between platforms.  
Making it "kinda better" will just create more traps like those unless 
you add a lot of explicit options to request specific output--which, 
again, ends up looking like find.


Mike Stone



Re: question about ls

2018-09-28 Thread Ionel Mugurel Ciobîcă
On 28-09-2018, at 17h 52'07", Richard Hector wrote about "Re: question about ls"
> 
> Eww. Tab completion also gets screwed up by this:
> 
> richard@zircon:~/test$ rm 
> file  isone   this
> 
> .. but you can't actually complete from any of those.
> 

Hi,

I am not a bash user, but if I try what you suggest, Richard, I see
the file as "this^Jis^Jone^Jfile" with tab completion (on one line).


Ionel

P.S. What doesn't mean the $'foo\nbar' construction? In tcsh it says
"Illegal variable name." and in history it is recalled as $\'foa\nbar'
instead. I see that ksh also behave as bash with this $'foo\nbar'
entity.



Re: question about ls

2018-09-28 Thread tomas
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Thu, Sep 27, 2018 at 11:03:29AM -0400, Michael Stone wrote:

[...]

> ls should be considered a user interface, not an API.

One should strive to minimize these differences, though. This is, of
course, just my opinion, but you can pry... (you know the rest ;-)

Cheers
- -- t
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlut0P0ACgkQBcgs9XrR2kaABACaAgIzkAOwWI31y1eJm04eNmBz
TYsAnj7L5beobdoit5WzMbNoqpDHoeD7
=IsdN
-END PGP SIGNATURE-



Re: question about ls

2018-09-28 Thread tomas
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Thu, Sep 27, 2018 at 09:59:58AM -0400, Greg Wooledge wrote:
> On Thu, Sep 27, 2018 at 08:53:28AM -0500, David Wright wrote:
> > On Thu 27 Sep 2018 at 07:55:56 (-0500), Kent West wrote:
> > > westk@westkbox:/opt$ ls -la | wc
> > >  7  56 321
> > > westk@westkbox:/opt$ ls -1a | wc
> > >  6   6  54
> > 
> > But do use 1A, not 1a, if you want to know how many items are
> > in a folder, otherwise . and .. will be included in the count.
> 
> All of the above give the wrong answers when filenames contain newlines.
> Any solution that involves printing the filenames to a stream and then
> trying to parse that stream to guess how many filenames are in the stream
> is a non-starter -- unless of course the stream uses NUL delimiters
> instead of newlines.
> 
> Sadly, the GNU coreutils maintainers have rejected every request, even
> requests with patches attached, to add a --null option to ls.  So,
> ls is not suited to this task.

To be fair, there's -b (escape nonprinting) and -q for that. Both defuse
the "newline-in-file-name" case.

Cheers
- -- tomás
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlut0GsACgkQBcgs9XrR2kbAGQCfasOAvCRWZEJS3+FBGyZi34o6
LbsAni35sZP/w1YSn/4ic0muQQNoz8gj
=Zrm4
-END PGP SIGNATURE-



Re: question about ls

2018-09-27 Thread Richard Hector
On 28/09/18 1:14 AM, Greg Wooledge wrote:
> On Thu, Sep 27, 2018 at 07:55:56AM -0500, Kent West wrote:
>> But also note the difference when using the letter "l" vs the numeral "1":
>>
>> westk@westkbox:/opt$ ls -la | wc
>>  7  56 321
>> westk@westkbox:/opt$ ls -1a | wc
>>  6   6  54
> 
> But also note that using ls -l gives a completely wrong answer.
> 
> wooledg:~$ mkdir /tmp/x && cd "$_"
> wooledg:/tmp/x$ touch $'this\nis\none\nfile'
> wooledg:/tmp/x$ ls -l | wc -l
> 5

Eww. Tab completion also gets screwed up by this:

richard@zircon:~/test$ rm 
file  isone   this

.. but you can't actually complete from any of those.

Richard



signature.asc
Description: OpenPGP digital signature


Re: question about ls

2018-09-27 Thread Greg Wooledge
On Thu, Sep 27, 2018 at 10:57:56AM -0400, Gene Heskett wrote:
> On Thursday 27 September 2018 08:31:27 Greg Wooledge wrote:
> 
> > On Tue, Sep 25, 2018 at 10:27:03PM +, Long Wind wrote:
> > > can ls show number of items in a folder?Thanks!
> >
> > https://mywiki.wooledge.org/BashFAQ/004
> 
> And finally, an "ls -l|wc -l" from the cli gives the answer to the 
> question asked.

The question that was asked was a yes-or-no question.  And the answer to
it is "no".  However, some people will not perceive "no" to be a USEFUL
answer, so I provided an answer with much more depth.

"ls -l|wc -l" gives you the number of newlines contained in a stream
generated by printing the names of SOME of the files in the current
working directory.

It omits all of the filenames that begin with a period.

It includes all of the newlines that are contained within filenames, and
thus gives an inaccurate count if any of the filenames contain newlines.

Since this is a Debian mailing list, I won't attempt to go into the
behaviors of ls on other Unix-based systems.  The two problems already
mentioned should suffice for this list.



Re: question about ls

2018-09-27 Thread Michael Stone

On Thu, Sep 27, 2018 at 09:59:58AM -0400, Greg Wooledge wrote:

All of the above give the wrong answers when filenames contain newlines.
Any solution that involves printing the filenames to a stream and then
trying to parse that stream to guess how many filenames are in the stream
is a non-starter -- unless of course the stream uses NUL delimiters
instead of newlines.

Sadly, the GNU coreutils maintainers have rejected every request, even
requests with patches attached, to add a --null option to ls.  So,
ls is not suited to this task.


Correct, find would be a better choice for programmatic use. E.g., 
something like 
find . -mindepth 1 -maxdepth 1 -printf '\n' | wc -l


ls should be considered a user interface, not an API. If ls were 
modified sufficiently to provide a rigorous programatic interface, it 
would basically become a very non-portable reimplementation of find.


Mike Stone



Re: question about ls

2018-09-27 Thread Gene Heskett
On Thursday 27 September 2018 08:31:27 Greg Wooledge wrote:

> On Tue, Sep 25, 2018 at 10:27:03PM +, Long Wind wrote:
> > can ls show number of items in a folder?Thanks!
>
> https://mywiki.wooledge.org/BashFAQ/004

And finally, an "ls -l|wc -l" from the cli gives the answer to the 
question asked. Change the -l to -lR if there are subdirs in that 
directory. Or if you want to know how many .jpg pix, use 
"ls -l *.jpg|wc -l".  The list goes on

The wiki is educational about how to make bash dance to your tune, but 
for this I'd druther use its default behavior.

bash can be and is a very powerfull scripting language, I've ported 
nearly everything I ever wrote in ARrexx, the universal do anything 
language for the Amiga from 25 years ago, to bash, and they've worked at 
least as good as on the Amiga.

-- 
Cheers, Gene Heskett
--
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page 



Re: question about ls

2018-09-27 Thread David Wright
On Thu 27 Sep 2018 at 09:59:58 (-0400), Greg Wooledge wrote:
> On Thu, Sep 27, 2018 at 08:53:28AM -0500, David Wright wrote:
> > On Thu 27 Sep 2018 at 07:55:56 (-0500), Kent West wrote:
> > > westk@westkbox:/opt$ ls -la | wc
> > >  7  56 321
> > > westk@westkbox:/opt$ ls -1a | wc
> > >  6   6  54
> > 
> > But do use 1A, not 1a, if you want to know how many items are
> > in a folder, otherwise . and .. will be included in the count.
> 
> All of the above give the wrong answers when filenames contain newlines.
> Any solution that involves printing the filenames to a stream and then
> trying to parse that stream to guess how many filenames are in the stream
> is a non-starter -- unless of course the stream uses NUL delimiters
> instead of newlines.
> 
> Sadly, the GNU coreutils maintainers have rejected every request, even
> requests with patches attached, to add a --null option to ls.  So,
> ls is not suited to this task.

Sure, your FAQ is an invaluable resource for scripting, especially in
a "hostile" environment. But I think one also needs to be aware of
practical commands for use at the commandline in a benign environment.
Knowing which technique is appropriate comes with experience.

One has to be aware of the limitations of all the tools one uses.
Just as ls has weaknesses, so does wc if misused. Most people assume
that -l counts the lines in a file, but actually it counts the
newlines, which matters for the last line.

But here's another plug, and there are lots more goodies there too:

https://mywiki.wooledge.org/BashFAQ/004

Cheers,
David.



Re: question about ls

2018-09-27 Thread Greg Wooledge
On Thu, Sep 27, 2018 at 08:53:28AM -0500, David Wright wrote:
> On Thu 27 Sep 2018 at 07:55:56 (-0500), Kent West wrote:
> > westk@westkbox:/opt$ ls -la | wc
> >  7  56 321
> > westk@westkbox:/opt$ ls -1a | wc
> >  6   6  54
> 
> But do use 1A, not 1a, if you want to know how many items are
> in a folder, otherwise . and .. will be included in the count.

All of the above give the wrong answers when filenames contain newlines.
Any solution that involves printing the filenames to a stream and then
trying to parse that stream to guess how many filenames are in the stream
is a non-starter -- unless of course the stream uses NUL delimiters
instead of newlines.

Sadly, the GNU coreutils maintainers have rejected every request, even
requests with patches attached, to add a --null option to ls.  So,
ls is not suited to this task.



Re: question about ls

2018-09-27 Thread David Wright
On Thu 27 Sep 2018 at 07:55:56 (-0500), Kent West wrote:
> On Tue, Sep 25, 2018 at 6:14 PM Ben Caradoc-Davies  wrote:
> 
> > On 26/09/2018 10:55, Long Wind wrote:
> > > sorry! you're right.
> > > after checking ls manual, i find ls has option -1
> >
> > Note also that ls will behave like it has the "-1" option if its output
> > is piped to another command, even without this option. I like to use the
> > "-1" option even in this case because it is what I mean (counting
> > entries one per line).
> >
> >
> But also note the difference when using the letter "l" vs the numeral "1":
> 
> westk@westkbox:/opt$ ls -la | wc
>  7  56 321
> westk@westkbox:/opt$ ls -1a | wc
>  6   6  54
> 
> The difference is that the letter includes the total count of blocks used
> at the top of the list (adding one to the count), whereas the numeral does
> not.

But do use 1A, not 1a, if you want to know how many items are
in a folder, otherwise . and .. will be included in the count.

Cheers,
David.



Re: question about ls

2018-09-27 Thread Greg Wooledge
On Thu, Sep 27, 2018 at 07:55:56AM -0500, Kent West wrote:
> But also note the difference when using the letter "l" vs the numeral "1":
> 
> westk@westkbox:/opt$ ls -la | wc
>  7  56 321
> westk@westkbox:/opt$ ls -1a | wc
>  6   6  54

But also note that using ls -l gives a completely wrong answer.

wooledg:~$ mkdir /tmp/x && cd "$_"
wooledg:/tmp/x$ touch $'this\nis\none\nfile'
wooledg:/tmp/x$ ls -l | wc -l
5

Thus, .



Re: question about ls

2018-09-27 Thread Kent West
On Tue, Sep 25, 2018 at 6:14 PM Ben Caradoc-Davies  wrote:

> On 26/09/2018 10:55, Long Wind wrote:
> > sorry! you're right.
> > after checking ls manual, i find ls has option -1
>
> Note also that ls will behave like it has the "-1" option if its output
> is piped to another command, even without this option. I like to use the
> "-1" option even in this case because it is what I mean (counting
> entries one per line).
>
>
But also note the difference when using the letter "l" vs the numeral "1":

westk@westkbox:/opt$ ls -la | wc
 7  56 321
westk@westkbox:/opt$ ls -1a | wc
 6   6  54

The difference is that the letter includes the total count of blocks used
at the top of the list (adding one to the count), whereas the numeral does
not.



-- 
Kent West<")))><
Westing Peacefully - http://kentwest.blogspot.com


Re: question about ls

2018-09-27 Thread Greg Wooledge
On Tue, Sep 25, 2018 at 10:27:03PM +, Long Wind wrote:
> can ls show number of items in a folder?Thanks! 

https://mywiki.wooledge.org/BashFAQ/004



Re: question about ls

2018-09-25 Thread Ben Caradoc-Davies

On 26/09/2018 10:55, Long Wind wrote:

sorry! you're right.
after checking ls manual, i find ls has option -1


Note also that ls will behave like it has the "-1" option if its output 
is piped to another command, even without this option. I like to use the 
"-1" option even in this case because it is what I mean (counting 
entries one per line).


Kind regards,

--
Ben Caradoc-Davies 
Director
Transient Software Limited 
New Zealand



Re: question about ls

2018-09-25 Thread Ben Caradoc-Davies

On 26/09/2018 10:27, Long Wind wrote:

can ls show number of items in a folder?Thanks!


I use:

ls -1 | wc -l

To include items whose names start with ".", add the "a" or "A" argument 
to ls ("man ls" for details).


For more advanced searches (recursive or filtering on file type), I use 
find.


Kind regards.

--
Ben Caradoc-Davies 
Director
Transient Software Limited 
New Zealand



question about ls

2018-09-25 Thread Long Wind
can ls show number of items in a folder?Thanks! 

On Wednesday, September 26, 2018 4:51 AM, Gary Dale  
wrote:
 

 For the last few days, some Scribus documents I work with have stopped 
accepting PDF files within image frames. Prior to this, they would 
display a preview. Now new image frames that I create show just the file 
name, but some older frames within the document still show the preview.

When I export the document as a PDF, the frames that just display the 
file name export as empty/blank. The older frame that shows a preview 
causes the export to stop with an error saying it was unable to load the 
image.

I'm not sure if this is related to something in the PDFs or not. However 
it is happening on two documents I am working on.