0
for file in *.pdf ; do
    newname=${file%\\.\*}
done

I don't understand how the %\.* is removing the file extension, can someone walk me through it.

Janis
  • 14,014
  • 3
  • 25
  • 42
Johnny
  • 11
  • 2

1 Answers1

1

I don't understand how the %\.* is removing the file extension

That's because it doesn't. :) The code as written is wrong, and it doesn't accomplish anything useful.

There is a standard shell expansion ${variable%pattern}, which takes $variable, and removes the shortest chunk at the end of it that matches pattern (there is a similar expansion ${variable%%pattern} that removes the longest match). However, pattern is supposed to be a shell glob, while the author of the piece of code above seems to have intended it as a regular expression.

The correct form is this: ${file%.*}. It works by removing everything to the right of the rightmost dot. In contrast, ${file%%.*} removes everything to the right of the leftmost dot. For example, if file=/some/very.long.directory.name/foo.pdf, then ${file%.*} expands to /some/very.long.directory.name/foo, and ${file%%.*} expands to /some/very.

lcd047
  • 7,160
  • 1
  • 22
  • 33