3

I have files which are named as "images123.jpg", "images456.jpg" etc

I would want to mv these files into testfolder folder and rename them accordingly to "123.jpg", "456.jpg" etc.

This is what i tried

for file in *.jpg; d
  mv $file testfolder/($file | cut -c7-)
done
Ninja Dude
  • 1,332
  • 4
  • 27
  • 54

1 Answers1

0

The below script should do what you need.

#!/bin/sh
for file in images*.jpg; do
    mv ${file} testfolder/${file#images}
done

The key part is ${file#images}. That is a bash shell parameter expansion:

${parameter#word}

${parameter##word}

The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches the beginning 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.

In this particular case it matches and strips images from the start of each file name.

kaylum
  • 13,833
  • 2
  • 22
  • 31