Question: **Premise**

I couldn't find a tool or script that would rename multiple files (100+) in the manner I needed it to. So I tried to write a Bash Script utilizing the 'mv' command.

**Problem**

The script does it's job and renames most of the files but then randomly outputs the 'Operation Not Permitted' error while renaming the files.

**Error Output**

*mv: cannot move 'filename.extension' to 'newFilename.extension': Operation not permitted*

**The Script**

```sh
a=1
for i in *.<extension>; do
  newName=$(printf "%03d <filename>.<extension>" "$a") #03 = Amount of 0 Padding you want to add
  sudo mv -i -- "$i" "$newName"
  let a=a+1
done

```

Thank You in advance for any possible help.

Answer: 
It is rarely a good idea to have sudo inside scripts. Instead, remove the sudo from the script and run the script itself with sudo:

```
sudo myscript.sh

```

That way, all commands within the script will be run with root privileges and you only need to give the password once when launching the script.
Instead of putting sudo in the script remove it and run the script using sudo.

```
sudo script.sh

```

If that still doesn't work make sure your user id is in the sudoers file so you will have the necessary root privileges.