linuxbashfindpathname

Linux, rename files with bash 'no such file or directory' error


I'm trying to add a prefix to all files that are not .sh files in the current directory, and all subdirectories, with bash using find.

The problem is that I get an error:

mv: cannot move './test.txt' to 'PRE_./test.txt': No such file or directory

My one-liner is:

find -type f ! -name "*.sh" -exec sh -c 'mv "{}" PRE_"{}"' _ {} \;

I tried xargs with rename, too:

find -type f ! -name "*.sh" -print0 | xargs -0 rename 's/(.*)$/PRE_$1/'

But I got the same error. What's wrong? Thanks in advance!


Solution

  • find -type f ! -name "*.sh" -exec rename -n 's|.*/\K|PRE_|' {} +
    

    Remove -n option from rename once it shows correct renaming