Vamos começar com o nome do seu arquivo:
$ f=base/app/main/sub/first/tib1.ear
Para extrair o nome base:
$ echo "${f##*/}"
tib1.ear
Para extrair a parte desejada do nome do diretório:
$ g=${f%/*}; echo "${g#base/app/}"
main/sub/first
${g#base/app/}
e ${f##*/}
são exemplos de remoção de prefixo . ${f%/*}
é um exemplo de remoção do sufixo .
Documentação
De man bash
:
${parameter#word} ${parameter##word} Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ''#'' case) or the longest matching pattern (the ''##'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. ${parameter%word} ${parameter%%word} Remove matching suffix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ''%'' case) or the longest matching pattern (the ''%%'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable sub‐ scripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Alternativas
Você também pode considerar os utilitários basename
e dirname
:
$ basename "$f"
tib1.ear
$ dirname "$f"
base/app/main/sub/first