Using basename dirname to split a file path and to run the run

58 Views Asked by At

I have a generic filepath- /home/aa/bb/cc/d.txt and want to script something like this-

so in the script, one thing would be for it to take that /path/to/testcase/d.txt

And split it into

working_dir = /path/to/testcase

netlist = d.txt

then the script would do:

cd $working_dir

<run $netlist>

I want to use basename and dirname to split the filepath here. Can anyone help me with this?

Expecting to run a testcase from a particular file path but making the command line generic for everyone to use.

1

There are 1 best solutions below

1
nightWolf On

We have 2 native linux commands basename & dirname which will be perfect for your requirement. These commands comes with package coreutils.

Here is an example:

$ dirname /usr/local/xyz/home/test/foo.bar
/usr/local/xyz/home/test

$ basename /usr/local/xyz/home/test/foo.bar
foo.bar

Hope this helps you.