coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [coreutils] basename/dirname can't handle stdin?


From: Bob Proulx
Subject: Re: [coreutils] basename/dirname can't handle stdin?
Date: Thu, 13 Jan 2011 22:58:52 -0700
User-agent: Mutt/1.5.20 (2009-06-14)

Jeff Blaine wrote:
> Yes, I've found there are a few ways around the lack of
> "filter mode" in GNU basename.  That there are other
> solutions doesn't negate the idea that it can/should be
> able to work in "filter mode" like so many other chainable
> applications, right?

After forty years it is hard to believe that basename really needs it.
Because basename really isn't a filter command.  Not like the classic
filter command sed for example.

  find . -print | sed 's/.*\/\([^/][^/]*\)/\1/;s/.txt$//'

That is a filter command.  Of course it can't handle arbitrary
filenames which is why I didn't suggest it as a solution.  And of
course bash and other shells can do the function using builtins too.

Certainly if you want the basename of files without removing suffixes
(which seemed to be the case of your original posting) and are already
using find (again as per your original posting) then you don't even
need basename.  That is more easily done with find.

basename-like:
  find . -printf '%f\n'

dirname-like:
  find . -printf '%h\n'

And zstring output too.

  find . -printf '%f\0' | xargs -0 -L1  # adding xargs -0 just to safe this

But I didn't say anything opposing making basename compatible with
BSD's basename.  Compatibility is good.  Even if it doesn't really
need the feature.  It certainly isn't difficult to do with the present
tools.  But it is a small thing and being compatible is useful.

To be clear I am not opposed to adding the *BSD compatibility options.

I am only opposed to thinking that it is needed.  :-)

> >BTW...  You shouldn't hard code paths such as /bin/basename.  On some
> >systems basename is in /bin and and on some in /usr/bin.  It is always
> >better to avoid hard coded paths, which create portability problems,
> >and instead locate programs from PATH.
> 
> I didn't hardcode anything :)  I specified the basename
> binary I wanted to use here because there is another one in
> my PATH before /bin (the basename code I am working on!  And
> I want it there at the front)

I am just working with the information provided.  :-) That is what you
wrote and so that is what I responded to.  And people have in the past
and do continue to hardcode paths such as /bin/basename.  Basename and
grep are two of the most problematic commands in that regard.  They
are all over the place on different systems.  I have hit that problem
many times.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]