JmJ
JmJ

Reputation: 2098

How to get name of last folder found using `find`

I have a folder structure containing multiple components:

src-/
    |-components-/
                 |-componentA
                 |-componentB
                 |-componentC

I want to find each component folder, execute a command on it, and use the folder name as the name of the each output folder inside my dist/ directory. So it should look like this:

dist-/
     |-componentA
     |-componentB
     |-componentC

Currently I'm running the following command:

BABEL_ENV=cjs find src/components/** -type d -exec babel {} --out-dir dist/{} \;

However this gives me the full file path inside {}, so the result is as follows:

src/components/componentA/index.js -> dist/src/components/componentA/index.js

I would like the result to be:

src/components/componentA/index.js -> dist/componentA/index.js

How can I achieve this?

Thanks in advance

Upvotes: 0

Views: 41

Answers (1)

tshiono
tshiono

Reputation: 22022

Please try the following:

while read -r -d "" path; do
    basename=${path##*/}    # will remove "src/components/" portion
    BABEL_ENV=cjs babel "$path" --out-dir "dist/$basename"
done < <(find src/components/* -type d -print0)

It transfers the output of find to the while loop in which the path name is modified to meet your requirement.

Upvotes: 1

Related Questions