1

For example i have the following files:

./dirA/fileA.png
./dirA/fileB.png
./dirA/fileC.png
./dirB/fileD.png
./dirB/fileE.png
./dirB/dirC/fileF.png

Is there any one line command or script to rename the file name to be the following?

./dirA/fileA@2x.png
./dirA/fileB@2x.png
./dirA/fileC@2x.png
./dirB/fileD@2x.png
./dirB/fileE@2x.png
./dirB/dirC/fileF@2x.png
0

Try:

find . -type f -name '*.png' -execdir bash -c 'mv "$1" "${1%.png}@2x.png"' Move {} \;
  • find .

    This starts a file search with the current directory.

  • -type f

    This limits the search to regular files.

  • -name '*.png'

    This limits the search to files whose names end with .png.

  • -execdir bash -c '...' Move {} \;

    This runs the command in single quotes with $0 assigned to Move and $1 assigned to the name of the file that was found. In our case, the command in single quotes is:

  • mv "$1" "${1%.png}@2x.png"

    This renames the file to end with @2x.png. The construct ${1%.png} removes the .png from the end of the file name. Thus, ${1%.png}@2x.png replaces the .png at the end of the file name with @2x.png.

Example

Let's start with directories with these files:

$ find .
.
./dirA
./dirA/fileA.png
./dirA/fileC.png
./dirA/fileB.png
./dirB
./dirB/fileE.png
./dirB/dirC
./dirB/dirC/fileF.png
./dirB/fileD.png

Now, let's run our command:

$ find . -type f -name '*.png' -execdir bash -c 'mv "$1" "${1%.png}@2x.png"' Move {} \;

After the command has executed, we now have these files:

$ find .
.
./dirA
./dirA/fileC@2x.png
./dirA/fileA@2x.png
./dirA/fileB@2x.png
./dirB
./dirB/dirC
./dirB/dirC/fileF@2x.png
./dirB/fileD@2x.png
./dirB/fileE@2x.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.