Search code examples
bashshellfindrenamemv

I used the'-print0' option to handle filenames with spaces, but I get an error


#!/bin/bash
find /home/data -name '*QQ*' -print0 -exec bash -c ' mv $1 ${0/\-QQ/-TT}' {} \;

I used the'-print0' option to handle filenames with spaces, but I get an error

/home/data/gone to sea.1080p-QQ.mp4mv: target 'sea.1080p-TT.mp4' is not a directory

Which part is wrong?

Thanks


Solution

  • You don't need -print0, since you're not piping the output to another program.

    You just need to quote properly in the bash command.

    find /home/data -name '*-QQ*' -exec bash -c 'mv "$1" "${1/\-QQ/-TT}"' {} {} \;
    

    This should work as long as the filenames don't contain double quote or $ characters.

    You could also avoid bash -c by using the rename command:

    find /home/data -name '*-QQ*' -exec rename 's/-QQ/-TT/' {} +