Query on Code

Hi All,

Can some one please explain the below code. Especially the %,* How it renaming the files and removing last field

for file in *.gz.*
do
 echo mv $file ${file%\.*}
done

i/p

abcd.gz.20151011.1

o/p

abcd.gz.20151011

Yes, that's a "Parameter Expansion / Remove matching suffix pattern".

man bash :

2 Likes

Hello nag_sathi,

This is called "parameter expansion" see following from documentation.

Here is the link for understanding parameter expansion too.
Shell Parameter Expansion (Bash Reference Manual)

I hope this helps.

Thanks,
R. Singh

2 Likes

Further notes:

  • The backslash serves no purpose here, you can leave it out.
  • There should be double quotes around the variable expansions "$file" "${file%.*}"
  • It is best to put -- (end of options) after mv
2 Likes