Yola
Yola

Reputation: 19019

extract file name from full file name

#!/bin/bash
FILES=src/*.erl
shopt -s nullglob
for f in $FILES
do
  echo "Processing $f file..."
# ???
done

How i can extract file name from full path? And what does it mean - "pathfilename=${f%.*}"?

Upvotes: 2

Views: 2503

Answers (2)

Mark Longair
Mark Longair

Reputation: 467023

Update: I've removed my answer to the first part, since apparently I misunderstood what was being asked.

The syntax you mention, pathfilename=${f%.*}, means that pathfilename is set to the value of $f with the shortest possible match for .* removed from the end of the string. This will strip the extension from the filename. The bash manual describes this syntax as follows:

${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 subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

Upvotes: 2

plundra
plundra

Reputation: 19212

I'll just copy the help-output, since it has examples and everything.

~$ basename --help
Usage: basename NAME [SUFFIX]
  or:  basename OPTION
Print NAME with any leading directory components removed.
If specified, also remove a trailing SUFFIX.

      --help     display this help and exit
      --version  output version information and exit

Examples:
  basename /usr/bin/sort       Output "sort".
  basename include/stdio.h .h  Output "stdio".

Upvotes: 5

Related Questions