PodcastsNyhederHacker Public Radio

Hacker Public Radio

Hacker Public Radio
Hacker Public Radio
Seneste episode

227 episoder

  • Hacker Public Radio

    HPR4607: UNIX Curio #3 - basename and dirname

    31.03.2026
    This show has been flagged as Clean by the host.

    This series is dedicated to exploring little-known—and occasionally useful—trinkets lurking in the dusty corners of UNIX-like operating systems.


    Hopefully it doesn't seem like I'm picking on
    Linux Journal
    , but like UNIX Curio #1 (HPR4587), this column has been inspired by
    an article of theirs


    1
    . The author was demonstrating a clever
    bash
    script that would take a filename and send the file to standard output or, if the filename ended in .gz, decompress it and send the result to standard output. Slightly rearranged, he had:



    F=`echo $1 | perl -pe 's/\.gz$//'`
    if [[ -f $F ]] ; then
    cat $F
    elif [[ -f $F.gz ]] ; then
    gunzip -c $F
    fi



    He took some heat on the web site and in letters to the magazine for cranking up a whole Perl interpreter just to chop the .gz off the end of a filename. Our curio for today is a standard UNIX utility made for just this purpose called


    basename




    2
    . Along with its brother


    dirname




    3
    , it is used to pull apart pathnames to get the part you want. What
    basename
    does is remove any leading path on the name given to it, and if a suffix is specified as well, removes that also. If a directory path with a trailing slash is given, it returns the last part with no slashes. Here are some examples:



    $ basename /bin/gzip
    gzip
    $ basename /bin/gzip .so
    gzip
    $ basename /usr/lib/libz.so .so
    libz
    $ basename /usr/lib/
    lib



    The counterpart,
    dirname
    , does essentially the opposite. It removes the last part of the pathname and returns a directory name (with no trailing slash):



    $ dirname /usr/lib/libz.so
    /usr/lib
    $ dirname /usr/lib/
    /usr
    $ dirname file_in_this_dir
    .



    So we can replace the first line of the script up top with
    F=`dirname $1`/`basename $1 .gz`
    , get the same result, and be sure it will work on any UNIX-like system, no Perl necessary. The more observant among you may be thinking "
    sed
    could do that, too!" and you're right;
    F=`echo $1 | sed 's/\.gz$//'`
    also would work anywhere.



    One might suspect that as a general-purpose text processor,
    sed
    would be slower than
    basename
    and
    dirname
    . To see how they compared, we ran each method against a randomly-generated list of 5,000 filenames. Turns out the critics were right, as Perl ran the longest at 59 seconds. Using
    basename
    /
    dirname
    took 44 seconds—a nice improvement, but
    sed
    blew past it at 34 seconds. Probably the fact that only one call to
    sed
    was needed versus two for
    basename
    and
    dirname
    made the difference.



    Helpful suggestions in response to the article revealed a shell curio. You may have seen the brace syntax for parameters. For example, to show a filename $F with an "X" appended, you can't use
    echo $FX
    because that means a parameter named
    FX
    . Instead, you'd use
    echo ${F}X
    and the shell only interprets what's inside the braces as the parameter name.





    Modifiers can also go inside the braces


    4
    and one of these, %, is just what we need to chop off that extension. This works in
    bash
    ,
    zsh
    , and any shell conforming to the current POSIX standard, but not
    csh
    and friends or older implementations of the Bourne shell. We can rewrite the first line of the original script as simply
    F=${1%.gz}
    and forgo any outside utilities. Performance? Under half a second to process those 5,000 filenames. Not bad at all.



    References:







    Treating Compressed and Uncompressed Data Sources the Same
    https://www.linuxjournal.com/content/treating-compressed-and-uncompressed-data-sources-same





    Basename specification
    https://pubs.opengroup.org/onlinepubs/009695399/utilities/basename.html





    Dirname specification
    https://pubs.opengroup.org/onlinepubs/009695399/utilities/dirname.html





    Shell Command Language: Parameter Expansion
    https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02







    This article was originally written in July 2010. The podcast episode was recorded in March 2026.


    Provide feedback on this episode.
  • Hacker Public Radio

    HPR4606: My Nerdy Childhood: From Floppy Disks to Dial-Up Dreams

    30.03.2026
    This show has been flagged as Explicit by the host.







    It all started at
    Flanders Technology International in 1987... a tech
    expo where an eleven-year-old watched a wooden block move across a
    desk and an arrow follow it on screen. That was it. That was the
    moment. He
    had to have a computer with a mouse.


    What followed was a story of after-school showroom squatting,
    summer jobs, game piracy, a modem bill that nearly gave his
    parents a heart attack, and an education in computing that no
    school could have provided.


    From the
    Amstrad PC1512 and the GEM windowing system, to the
    Schneider Euro PC with its infamous
    Turbo button that turned Ms. Pac-Man into a half-second
    blur — this episode is a love letter to the glorious chaos of home
    computing in the late 1980s.


    Along the way: the satisfying clatter of a
    matrix printer
    , the dark arts of
    config.sys and
    autoexec.bat
    ,
    Digger
    , the allure of the
    Commodore 64
    , forbidden floppy disks at computer club, a 2400-baud modem, and
    the very first taste of online community — long before anyone
    called it the internet.


    The computers




    Amstrad PC1512 — the showroom machine that started it all



    Schneider Euro PC — the computer-in-a-keyboard with the
    infamous Turbo button



    Commodore 64 — legendary sounds, legendary forbidden
    floppy disks



    Play the games


    Digger — play in your browser

    Ms. Pac-Man — play in your
    browser

    Samantha Fox Strip
    Poker (C64)


    Leisure Suit Larry —
    Looking for Love in All the Wrong Places — play in your
    browser






    Provide feedback on this episode.
  • Hacker Public Radio

    HPR4605: Lee locks down his wifey poo

    27.03.2026
    This show has been flagged as Clean by the host.

    Two geeky people, both HPR hosts, decide to sign on the dotted line and do the darn thing and get married.

    Provide feedback on this episode.
  • Hacker Public Radio

    HPR4604: Quick Tips for January 20 26

    26.03.2026
    This show has been flagged as Clean by the host.

    It's going to be pretty short, I'm going to go through my quick
    tips. Looks like I've got enough to kind of do a little short
    episode for you guys. Quick tips are basically just, you know,
    kind of things that I think about, that add value, kind of like
    those stupid viral videos on social media where they show, like
    how to make a pancake with a square, like it, you know.

    Prevent messiness when making Matcha

    Baking soda and super glue to get an epoxy type of seal.

    "Tile batter pad on top of speaker in middle" - even operat0r has no clue !

    How to blowing out a candle

    Request for more shows on rsync inspired by hpr4341 :: Transferring Large Data Sets sent in by hairylarry

    How to make a foam machine

    Fixing garden chairs by replacing the Vinyl straps

    How to use a Fabric belt

    YTDLnis Full Featured Downloader using yt-dlp, available on F-Droid

    Use a wet paper towel over Microwave food.

    Use binder clips instead of chip clips

    Things To Get Me since Amazon got rid of their add arbitrary item to wish list feature.

    Provide feedback on this episode.
  • Hacker Public Radio

    HPR4603: On the Erosion of Freedom in Open Source Software

    25.03.2026
    This show has been flagged as Explicit by the host.

    DISCLAIMER: "Hill willing to kill on" is an idiom. I do not condone or tolerate open calls for violence and want to make this very clear here. Due to the incredibly unfortunate nature of the topic at hand, I have also taken the liberty to mark this episode as explicit.







    This episode was recorded while I had some short downtime. There are filler phrases and pauses throughout. I apologize in advance.

    Provide feedback on this episode.

Flere Nyheder podcasts

Om Hacker Public Radio

Hacker Public Radio is an podcast that releases shows every weekday Monday through Friday. Our shows are produced by the community (you) and can be on any topic that are of interest to hackers and hobbyists.
Podcast-websted

Lyt til Hacker Public Radio, Genstart og mange andre podcasts fra hele verden med radio.dk-appen

Hent den gratis radio.dk-app

  • Bogmærke stationer og podcasts
  • Stream via Wi-Fi eller Bluetooth
  • Understøtter Carplay & Android Auto
  • Mange andre app-funktioner