0

Inside of bash I am loading a file such as:

/path/to/dir/filename.ext

now the idea is to use that file name and "extend its' name" upon modification such as:

some operation /path/to/dir/filename.ext > path/to/dir/filename_extendingfilename.ext;

So far I have managed to call the filename using this from another question that has already been asked on here (for reference Extract filename and extension in Bash) :

basename filename .extension

basename /path/to/dir/filename.txt .txt

filename

The issue im having is the creation of a new file which has the filename of the source file and extents on it using "_ extension.ext"

DaRthR
  • 25
  • 6

1 Answers1

2

bash shell parameter expansion is an option here:

f=/path/to/dir/filename.ext
f2=${f%.ext*}"_extendingfilename.ext"

The shell parameter expansion cuts at the last occurrence of .ext (i.e. it will also work correctly for a file like /path/to/dir/file.ext.name.ext)

Here is an example for the redirection in the answer:

f=/path/to/dir/filename.ext
someoperation $f > ${f%.ext*}"_extendingfilename.ext"
Pete
  • 421
  • 3
  • 6
  • does that work when im creating a new file thou ? as in path/to/dir/filename_extendingfilename.ext such as `f=/path/to/dir/filename.ext f2=${f%.ext*}"_extendingfilename.ext" echo $f2 some operation /path/to/dir/filename.ext > path/to/dir/echo $f + $f2.ext;` – DaRthR Jul 04 '19 at 06:20
  • added a one-liner as an example for your redirection of *some operation*. – Pete Jul 04 '19 at 17:34