4

Lets say I've got the following directory structure:

base/
|
+-- app
|   |
|   +-- main
|       |
|       +-- sub
|           |
|           +-- first
|           |   |
|           |   +-- tib1.ear
|           |   \-- tib1.xml
|           |
|           \-- second
|               |
|               +-- tib2.ear
|               \-- tib2.xml

One of the relative paths to an ear file would be base/app/main/sub/first/tib1.ear, how could I extract the substrings for:

  • The file, tib1.ear or tib2.ear
  • The sub-directory after base/app/ but not including the file, that being main/sub/first or main/sub/second

All of the directory names are dynamically generated, so I don't know them beyond base/app/, and therefore cannot simply use the lengths of the known sub-strings and use cut to truncate them; but I see how it could be possible once the filenames are known. I just feel like there's an easier way than cutting and joining a bunch of strings based on the length of other results.

I remember seeing some regular expression magic for something similar to this. It dealt with splitting and joining the substrings with backslashes, but sadly, I don't remember how they did it or where I saw it on here to look it up again.

6

Let's start with your filename:

$ f=base/app/main/sub/first/tib1.ear

To extract the base name:

$ echo "${f##*/}"
tib1.ear

To extract the desired part of the directory name:

$ g=${f%/*}; echo "${g#base/app/}"
main/sub/first

${g#base/app/} and ${f##*/} are examples of prefix removal. ${f%/*} is an example of suffix removal.

Documentation

From 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.

Alternatives

You may also want to consider the utilities basename and dirname:

$ basename "$f"
tib1.ear
$ dirname "$f"
base/app/main/sub/first
  • 1
    Thanks John, this was perfect. For those that don't like to read the whole man-page for functions: ${string##substring} Deletes longest match of $substring from front of $string. ${string%substring} Deletes shortest match of $substring from back of $string. From here – Darrel Holt Sep 15 '17 at 21:56
2

creating the test files

mkdir -p base/app/main/sub/{first,second}
touch base/app/main/sub/first/tib1.{ear,xml}
touch base/app/main/sub/second/tib2.{ear,xml}

finding the ear files with bash

shopt -s globstar nullglob
ear_files=( base/**/*.ear )
printf "%s\n" "${ear_files[@]}"
base/app/main/sub/first/tib1.ear
base/app/main/sub/second/tib2.ear

Iterate over the array and use John1024's answer to extract the necessary info from each path.

for f in "${ear_files[@]}"; do ...; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.