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/[email protected]
./dirA/[email protected]
./dirA/[email protected]
./dirB/[email protected]
./dirB/[email protected]
./dirB/dirC/[email protected]

1 Answer 1

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/[email protected]
./dirA/[email protected]
./dirA/[email protected]
./dirB
./dirB/dirC
./dirB/dirC/[email protected]
./dirB/[email protected]
./dirB/[email protected]
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .