{{announcement.body}}
{{announcement.title}}

Extracting the Base Name of a File in Bash

DZone 's Guide to

Extracting the Base Name of a File in Bash

Here's a simple addition you can make to any bash script to get to the base name of files you're working with.

· Open Source Zone ·
Free Resource

I have a handy bash script that transcodes videos using Don Meton’s video_transcoding tools. This script was written in a hurry, and one limitation it had was that it re-transcoded any source file even if the output file already existed.

The script looked like this:

#!/usr/bin/env bash

readonly source_dir="${1:-MKV}"
readonly output_dir="${2:-MP4}"

for file in "$source_dir"/*.mkv; do
    ./transcode.sh "$file" "$output_dir"
done


What it should do is only run transcode.sh if the output file doesn’t exist, so I updated it.

Parameter Expansion to the Rescue!

I needed to extract the base name and then construct the target filename in order to test for its existence. To extract the base name of the file, I used parameter expansion:

filename=${file##*/}
basename=${filename%.*}


The first line removes the directory portion of file, This works by using the ##[word] expansion, which removes the largest prefix pattern. That means that for */ it removes everything up to the last / in the string — i.e. any directory paths.

The second line removes the extension from the filename by using the %[word] expansion. This removes the smallest suffix pattern on the string: for .*, it removes the extension.

I can then create the target by concatenating the output_dir, basename and the .mp4 extension and then test for the target file’s existence.

The final script is now:

#!/usr/bin/env bash

readonly source_dir="${1:-MKV}"
readonly output_dir="${2:-MP4}"

for file in "$source_dir"/*.jpg; do
    filename=${file##*/}
    basename=${filename%.*}
    target="${output_dir}/${basename}.mp4"
    if [ ! -f "$target" ]; then
        ./transcode.sh "$file" "$output_dir"
    fi
done


It’s a small change, but makes my life easier as I can run my script multiple times without needing to ensure that I’ve deleted any of the source files for files that I’ve already transcoded.

Topics:
open source ,file name ,bash script ,parameter expansion ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}