UNIX guru's, I need some help with the basename utility

Discussion in 'OT Technology' started by SnYpR808, Feb 21, 2004.

  1. SnYpR808

    SnYpR808 Guest

    i'm writing a small program and I need to use basename so that it strips the excess path info off the name of the program, so that I can call it using jrm instead of username.pj.jrm. the program works fine (it takes whatever i tell it to and sends it to the junk directory, if the junk directory doesn't exist it creates it and sends the file there.) I just can't get this stupid basename problem solved. I checked the man pages and everything and it still doesn't help with this problem. thanks in advance

    cat > username.pj.jrm
    if test ! -d .junk
    mkdir .junk
    mv [email protected] .junk
  2. SLED

    SLED custom title

    Sep 20, 2001
    Likes Received:
    how are you calling it? it works fine for me :dunno:

    [email protected]_home:~> basename /home/geoff/httpd.conf
    [email protected]_home:~> basename /home/geoff/httpd.conf .conf
  3. col_panic

    col_panic calm like a bomb Moderator

    Sep 19, 2003
    Likes Received:
    winter haven, fl
    first: badass avatar

    second: i don't understand - you want to cat to a filename by using basename instead of the full filename?
  4. SnYpR808

    SnYpR808 Guest

    SLED - I'm not trying to use basename to strip off the rest of the path. I'm trying to use it so that i can call a program using only the last part of the filename i guess. refer to my response to col_panic. Thanks

    col_panic -thanks, for the compliment. What I want basename to do is strip off the "username.pj" from "username.pj.jrm" so that i can call the program using only "jrm." I was told it was possible using something like prog `basename $0` but i have no idea what that's supposed to do. I tried it and it didn't work.
  5. GmH

    GmH Guest

    Why don't you just rename it to "jrm" (or make a symbolic link to it called "jrm")?

    If you can't do that for some reason, then (depending on which shell you use) you can probably make an alias.

    For Bourne shells (sh, bash, ksh, etc), stick this in your .profile:
    alias jrm=username.pj.jrm

    For C shells (csh, tcsh, etc) stick this in your .cshrc:
    alias jrm username.pj.jrm

    FYI, the basename command will only strip off any leading path components, and optionally a specified extension:

    basename '/path/to/somefile.ext' => 'somefile.ext'
    basename '/path/to/somefile.ext' .ext => 'somefile'

    It does not strip off the leading components of the filename, since obtaining that part (rather than removing it) is the whole point of the command.
    Last edited by a moderator: Feb 21, 2004

Share This Page