Any command to display a filename without extension?

Any command to display a filename without extension?

Post by Lee Hon Chor (cf » Wed, 13 Jun 2001 18:51:29



Hi,

Could somebody tell me how to, with a command, display a filename
without its extension? For example, for 'abc.txt', the command
displays 'abc'. Thanx.

David

 
 
 

Any command to display a filename without extension?

Post by Jens.Toerr.. » Wed, 13 Jun 2001 19:20:35



Quote:> Could somebody tell me how to, with a command, display a filename
> without its extension? For example, for 'abc.txt', the command
> displays 'abc'. Thanx.

There is no such command because UNIX doesn't has extensions. The '.'
is just another character you can have in your filename and it isn't
special in any respect. This said, of course you can get rid of every-
thing including and following a point. Since you don't specify in
what context you want to do this I can just guess and give you an
example in bash: If your filename, say 'abc.def', is stored in the
variable a you get the filename without the 'extension' by using

        ${a%.*}

This also works for files like 'abc.def.ghi' where this cuts of just
the 'last extension', giving 'abc.def'.
                                          Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

Any command to display a filename without extension?

Post by Scott Hawle » Wed, 13 Jun 2001 21:04:44


You could try cut as well,

echo abc.c | cut -d. -f1 will print 'abc'


Quote:> Hi,

> Could somebody tell me how to, with a command, display a filename
> without its extension? For example, for 'abc.txt', the command
> displays 'abc'. Thanx.

> David

--
Scott Hawley

 
 
 

Any command to display a filename without extension?

Post by F. Michael Or » Wed, 13 Jun 2001 21:58:01




> > Could somebody tell me how to, with a command, display a filename
> > without its extension? For example, for 'abc.txt', the command
> > displays 'abc'. Thanx.

> There is no such command because UNIX doesn't has extensions. The '.'
> is just another character you can have in your filename and it isn't
> special in any respect. This said, of course you can get rid of every-
> thing including and following a point. Since you don't specify in
> what context you want to do this I can just guess and give you an
> example in bash: If your filename, say 'abc.def', is stored in the
> variable a you get the filename without the 'extension' by using

UNIX doesn't use extensions to determine executables, but it most certainly
does use extensions.  The command to see a filename without the extension is
'dirname'.  To see _just_ the filename, without extension or path, is
'basename'.
 
 
 

Any command to display a filename without extension?

Post by Arto Viitane » Wed, 13 Jun 2001 22:30:26


 Lee> Hi, Could somebody tell me how to, with a command, display a filename
 Lee> without its extension? For example, for 'abc.txt', the command displays
 Lee> 'abc'. Thanx.

Command basename(1) is closest. Actually, basename is used in removing
directory part of the filename, like

$ basename /d1/d2.ext2/d3/d4.ext4/file.ext5

 file.ext5

If you give extension, you get

$ basename /d1/d2.ext2/d3/d4.ext4/file.ext5 .ext5

 file

But, you have to know the extension already.

--

University of Tampere, Department of Computer and Information Sciences
Tampere, Finland                                      http://www.cs.uta.fi/~av/

 
 
 

Any command to display a filename without extension?

Post by Steve Alla » Thu, 14 Jun 2001 01:06:36






>> > Could somebody tell me how to, with a command, display a filename
>> > without its extension? For example, for 'abc.txt', the command
>> > displays 'abc'. Thanx.

>> There is no such command because UNIX doesn't has extensions. The '.'
>> is just another character you can have in your filename and it isn't
>> special in any respect. This said, of course you can get rid of every-
>> thing including and following a point. Since you don't specify in
>> what context you want to do this I can just guess and give you an
>> example in bash: If your filename, say 'abc.def', is stored in the
>> variable a you get the filename without the 'extension' by using

>UNIX doesn't use extensions to determine executables, but it most certainly
>does use extensions.  The command to see a filename without the extension is
>'dirname'.  To see _just_ the filename, without extension or path, is
>'basename'.

Are you sure?  On my solaris box:

% dirname /tmp/foo.txt
/tmp
% basename /tmp/foo.txt
foo.txt

--
-- Steve __

 
 
 

Any command to display a filename without extension?

Post by Steve Alla » Thu, 14 Jun 2001 01:10:59


D'oh!  I should have read on and I would have learned about the
'suffix' arg to basename before posting.

% basename /tmp/foo.txt .txt
foo

That's a new one for me.

--
-- Steve __

 
 
 

Any command to display a filename without extension?

Post by Theo Lutgeri » Thu, 14 Jun 2001 06:13:04



Use 'basename'.

It has a lot of options, or write some command yourself. That must not
be too difficult, I suppose.

Quote:>Hi,

>Could somebody tell me how to, with a command, display a filename
>without its extension? For example, for 'abc.txt', the command
>displays 'abc'. Thanx.

>David

 
 
 

Any command to display a filename without extension?

Post by Dowe Kell » Thu, 14 Jun 2001 13:54:49



Quote:>Hi,

>Could somebody tell me how to, with a command, display a filename
>without its extension? For example, for 'abc.txt', the command
>displays 'abc'. Thanx.

>David

If you know what the extension is, you can simpy type:

basename foo.bar .bar

--

                                Project : http://freshmeat.net/projects/vsh

 
 
 

Any command to display a filename without extension?

Post by Juergen Pfan » Thu, 14 Jun 2001 15:00:45



> You could try cut as well,

> echo abc.c | cut -d. -f1 will print 'abc'

ACK in most cases - but gives "wrong" result
for filenames with more than one dot, as one
might expect the filename minus the last dot and
"extension", not the part of the name up
to the _first_ dot.
With a known "extension", I'd say, as some other
posters already, the 'basename' tool is the easiest
way - and "multiple-dot-safe" :
basename abc.c .c
in the case of your example.

Juergen

 
 
 

1. URLs Without Filename Extensions?

I've noticed that on a few sites (e.g., W3C and ApacheWeek) that many of
the URLs don't have the expected (at least by me ;-) .html, .htm, .gif, ...
filename extensions.

I assume, given the frequency of occurence on these sites, that this is by
design?  If so, what is the rationale, and is this a "good thing" (i.e., is
it primarily to save the effort to type ".html" for every URL, and/or is it
so the same URL can refer to a document or subdir, etc.)?

Just wondering, er, wandering in my endless quest for knowledge ....

----------------------------------------------

2. Protocol analyzer for internal modem?

3. command to display filenames for a particular month

4. Convert vc file format to Lotus 1-2-3 file format?

5. command > filename when command needs keyboard input - HOW?

6. Splitting an image in half (into two images) from a script

7. change from DISPLAY 0 to DISPLAY 1 without CTRL+ALT+F?

8. Another simple newbie question...

9. sh vs. filename extensions

10. apache + frontpage extensions: problems with filenames

11. Extracting the filename extension

12. long filename extensions? HELP!!

13. Bourne shell: splitting filename into base and extension