Get last dirname/filename in a file path argument in Bash Get last dirname/filename in a file path argument in Bash bash bash

Get last dirname/filename in a file path argument in Bash


basename does remove the directory prefix of a path:

$ basename /usr/local/svn/repos/exampleexample$ echo "/server/root/$(basename /usr/local/svn/repos/example)"/server/root/example


The following approach can be used to get any path of a pathname:

some_path=a/b/cecho $(basename $some_path)echo $(basename $(dirname $some_path))echo $(basename $(dirname $(dirname $some_path)))

Output:

cba


Bash can get the last part of a path without having to call the external basename:

subdir="/path/to/whatever/${1##*/}"