Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!mordor!joyce!zodiac!ames!killer!dcs!wnp From: wnp@dcs.UUCP (Wolf N. Paul) Newsgroups: comp.unix.questions Subject: Re: Wanted: How to strip off pathnames to obtain filenames. Summary: Bourne/Korn shells: use "basename"; C-Shell: use :t Keywords: Any efficient way to generate "pr filenames" from "usend pathnames"? Message-ID: <194@dcs.UUCP> Date: 2 Sep 88 12:12:02 GMT References: <5968@ihlpf.ATT.COM> Reply-To: wnp@dcs.UUCP (Wolf N. Paul) Organization: DCS, Dallas, Texas Lines: 49 In article <5968@ihlpf.ATT.COM> pcl@ihlpf.ATT.COM (pcl) writes: >My question is, given a full or partial pathname, how to obtain the >last field (which is the filename) in a straight foward manner. > >Is there such command that does > filename=`echo $pathname | cut -fLAST -d/" # no such thing -fLAST YES! basename(1) does exactly what you want! filename=`basename $pathname` will yield the part of $pathname to the right of the last slash; filename=`basename $pathname .ext` will yield the part of the pathname to the right of the last slash, minus the extension ".ext". Under System V Release 2, basename(1) is a shell script, so even if it does not exist on your system (why would that be?) I'm sure you can find it somewhere within your organization (your corporate security wouldn't like it if I posted it :-)). However, this is also very straightforward to do in C: #include #include main(argc, argv) int argc; char **argv; { char *ptr; ptr=strrchr(argv[1], '/'); *ptr++; puts(ptr); } Of course, if you are using C-Shell, things are even simpler: set filename=$pathname:t # = basename $pathname set filename=$pathname:t:r # = basename $pathname .* etc. -- Wolf N. Paul * 3387 Sam Rayburn Run * Carrollton TX 75007 * (214) 306-9101 UUCP: killer!dcs!wnp ESL: 62832882 DOMAIN: dcs!wnp@killer.dallas.tx.us TLX: 910-380-0585 EES PLANO UD