Get folder/filename from a path

Get folder name, filename, file extension from a path:


# File name: Strip from start longest match of [*/]
FILENAME="${BASEFILE##*/}"
echo "FILENAME $FILENAME"

# Folder: Substring from 0 to start of filename
FOLDER="${BASEFILE:0:${#BASEFILE} - ${#FILENAME} - 1}"
echo "FOLDER $FOLDER"

# File prefix: Strip from end longest match of [dot plus at least one non-dot char]
FILEPREFIX="${FILENAME%.[^.]*}"
echo "FILEPREFIX $FILEPREFIX"

# File extension: Strip from start shortest match of [at least one non-dot char plus dot]
EXTENSION="${FILENAME##[^.]*.}"
echo "EXTENSION $EXTENSION"

This entry was posted in Linux and tagged , , by swk. Bookmark the permalink.

About swk

I am a computational linguist, teacher of computer science and above all a huge fan of LaTeX. I use LaTeX for everything, including things you never wanted to do with LaTeX. My latest love is lilypond, aka LaTeX for music. I'll post at irregular intervals about cool stuff, stupid hacks and annoying settings I want to remember for the future.