Next: , Up: file names   [Contents][Index]


6.1.1 Splitting a file name into its components

Function: mbfl_file_extension pathname
Function: mbfl_file_extension_var _RV pathname

Extract the extension from a file name. Starting from the end of the string pathname: search the last dot character in the argument string and echo to stdout the range of characters from the dot to the end, not including the dot. If a slash character or the beginning of the string is found first: echoes to stdout the empty string.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_file_extension /path/to/file.ext   -| ext
mbfl_file_extension /path/to/file.      -| <no output>
mbfl_file_extension /path/to/file       -| <no output>
mbfl_file_extension /path/to/.file.ext  -| ext
mbfl_file_extension /path/to/.dotfile   -| <no output>
mbfl_file_extension .dotfile            -| <no output>

local _RV
mbfl_file_extension_var _RV /path/to/file.ext
"$_RV"  ⇒ ext
Function: mbfl_file_dirname pathname
Function: mbfl_file_dirname_var _RV pathname

Extract the directory part from a fully qualified file name. Search the last slash character in the input string and echo to stdout the range of characters from the first to the slash, not including the slash.

If no slash is found: echo a single dot (the current directory).

If the input string begins with / or // with no slash characters after the first ones: echo a single slash.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_file_dirname /path/to/file.ext     -| /path/to
mbfl_file_dirname file.ext              -| .
mbfl_file_dirname /file.ext             -| /
mbfl_file_dirname //file.ext            -| /
mbfl_file_dirname /path/to///file.ext   -| /path/to
mbfl_file_dirname //////file.ext        -| /
mbfl_file_dirname a/b                   -| a
mbfl_file_dirname a                     -| .
mbfl_file_dirname ../a                  -| ..
mbfl_file_dirname ./a                   -| .
mbfl_file_dirname ../abcd               -| ..
mbfl_file_dirname ./abcd                -| .
mbfl_file_dirname ../abcd/efgh          -| ../abcd
mbfl_file_dirname ./abcd/efgh           -| ./abcd

local _RV
mbfl_file_dirname_var _RV /path/to/file.ext
"$_RV" ⇒ /path/to
Function: mbfl_file_rootname pathname
Function: mbfl_file_rootname_var _RV pathname

Extract the root portion of a file pathname: everything excluding the last component’s extension and the extension’s dot separator. Search the last dot character in the argument string and echo to stdout the range of characters from the beginning to the dot, not including the dot. If a slash is the last character: skip it; then if a slash character is found first, or no dot is found, or the dot is the first character: echo the full pathname.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_file_rootname file.ext              -| file
mbfl_file_rootname /path/to/file.ext     -| /path/to/file
mbfl_file_rootname /path/to/file..ext    -| /path/to/file.
mbfl_file_rootname /path/to/file.ext/    -| /path/to/file
mbfl_file_rootname /path/to/file.ext///  -| /path/to/file
mbfl_file_rootname /path/to/file         -| /path/to/file
mbfl_file_rootname /path/to.to/file      -| /path/to.to/file
mbfl_file_rootname .dotfile              -| .dotfile
mbfl_file_rootname /path/to/.dotfile     -| /path/to/.dotfile
mbfl_file_rootname a                     -| a
mbfl_file_rootname /                     -| /
mbfl_file_rootname .                     -| .
mbfl_file_rootname ..                    -| ..

local _RV
mbfl_file_rootname_var /path/to/file.ext
"$_RV" ⇒ /path/to/file
Function: mbfl_file_tail pathname
Function: mbfl_file_tail_var pathname

Extract the file portion from a fully qualified pathname. Search the last slash character in the input string and echo to stdout the range of characters from the slash to the end, not including the slash. If no slash is found: echo the whole string.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_file_tail /path/to/file.ext  -| file.ext
mbfl_file_tail /path/to/          -| <no output>
mbfl_file_tail file.ext           -| file.ext

local _RV
mbfl_file_tail_var _RV /path/to/file.ext
"$_RV" ⇒ file.ext
Function: mbfl_file_split pathname

Separate a file name into its components. One or more contiguous occurrences of the slash character is used as separator. The components are stored in an array named SPLITPATH, that may be declared local in the scope of the caller; the base index is zero. The number of elements in the array is stored in a variable named SPLITCOUNT. Return true.

local -a SPLITPATH
local -i SPLITCOUNT

mbfl_file_split /path/to/file.ext
"${SPLITPATH[0]}"       ⇒ path
"${SPLITPATH[1]}"       ⇒ to
"${SPLITPATH[2]}"       ⇒ file.ext
$SPLITCOUNT             ⇒ 3
Function: mbfl_file_strip_trailing_slash pathname
Function: mbfl_file_strip_trailing_slash_var _RV pathname

Remove all the trailing slashes from pathname and print the result on stdout. If pathname consists only of slashes: print a single dot.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_file_strip_trailing_slash '/path/to/file.ext'
-| /path/to/file.ext

mbfl_file_strip_trailing_slash '/path/to/dir.ext/'
-| /path/to/dir.ext

mbfl_file_strip_trailing_slash '/path/to/dir.ext///'
-| /path/to/dir.ext

mbfl_file_strip_trailing_slash '/'      -| .
mbfl_file_strip_trailing_slash '///'    -| .
mbfl_file_strip_trailing_slash 'file'   -| file

local _RV
mbfl_file_strip_trailing_slash_var _RV '/path/to/dir.ext/'
"$_RV" ⇒ /path/to/dir.ext
Function: mbfl_file_strip_leading_slash pathname
Function: mbfl_file_strip_leading_slash_var _RV pathname

Remove all the leading slashes from pathname and print the result on stdout. If pathname consists only of slashes: print a single dot.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.

mbfl_file_strip_leading_slash '/path/to/file.ext'
-| path/to/file.ext

mbfl_file_strip_leading_slash '/path/to/dir.ext/'
-| path/to/dir.ext/

mbfl_file_strip_leading_slash '///path/to/dir.ext'
-| path/to/dir.ext

mbfl_file_strip_leading_slash '/'       -| .
mbfl_file_strip_leading_slash '///'     -| .
mbfl_file_strip_leading_slash 'file'    -| file
Function: mbfl_file_normalise pathname
Function: mbfl_file_normalise pathname prefix
Function: mbfl_file_normalise_var _RV pathname
Function: mbfl_file_normalise_var _RV pathname prefix

Normalise a file name: remove all the occurrences of . and ...

If pathname is relative (according to mbfl_file_is_absolute()) and prefix is not present or it is the empty string: the current process working directory is prepended to pathname.

If prefix is present and non empty, and pathname is relative (according to mbfl_file_is_absolute()): prefix is prepended to pathname and normalised, too.

Echo to stdout the normalised file name. Return true.

The function variant _var stores the result in the variable _RV, rather than print it; Result variables.


Next: , Up: file names   [Contents][Index]

This document describes version 3.0.0-devel.0 of Marcos Bash Functions Library.