Skip to content

Recovering from failures

Ayooluwa Isaiah edited this page Feb 2, 2023 · 9 revisions

F2's conflict detection should prevent the most common failures for any renaming operation. However, despite these checks, the renaming a file or directory may still fail for any of the following reasons:

  • The source file is being used by another program.
  • The source file no longer exists due to it being moved or changed in another program.
  • Insufficient permissions to rename the file.

When the renaming of a file fails, the entire operation is not halted. Rather, the program will proceed to the next file in the list. All the failed renaming operations are aggregated and presented at the end of the process along with the reason why they failed.

Here's an example that attempts to rename the following JPEG files in a directory. A few of these files have been made immutable through the chattr command on Linux so that the renaming operation fails for those files.

tree
├── kevin-wong-tt7aK2yV7xo-unsplash.jpg
├── nathan-anderson-7TGVEgcTKlY-unsplash.jpg
├── photo-1434907652076-85f8401482c3.jpg
├── photo-1510272839903-5112a2e44bc6.jpg
├── photo-1521579971123-1192931a1452.jpg
├── samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg
└── valentin-salja-VMroCCpP648-unsplash.jpg

Make some of the files immutable:

sudo chattr +i kevin-wong-tt7aK2yV7xo-unsplash.jpg nathan-anderson-7TGVEgcTKlY-unsplash.jpg valentin-salja-VMroCCpP648-unsplash.jpg

Here's the command to rename the files and the output:

f2 -r "unsplash-image-%03d" -ex
┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐
| ORIGINAL                                          | RENAMED                | STATUS                  |
| **************************************************************************************************** |
| photo-1434907652076-85f8401482c3.jpg              | unsplash-image-003.jpg | ok                      |
| photo-1510272839903-5112a2e44bc6.jpg              | unsplash-image-004.jpg | ok                      |
| photo-1521579971123-1192931a1452.jpg              | unsplash-image-005.jpg | ok                      |
| samuele-errico-piccarini-t4OxCpKie70-unsplash.jpg | unsplash-image-006.jpg | ok                      |
| valentin-salja-VMroCCpP648-unsplash.jpg           | unsplash-image-007.jpg | operation not permitted |
| nathan-anderson-7TGVEgcTKlY-unsplash.jpg          | unsplash-image-002.jpg | operation not permitted |
| kevin-wong-tt7aK2yV7xo-unsplash.jpg               | unsplash-image-001.jpg | operation not permitted |
└──────────────────────────────────────────────────────────────────────────────────────────────────────┘

If errors are detected during the renaming operation (as above), the entire operation is printed out with the successes above and failures below. The directory now looks like this:

tree
├── kevin-wong-tt7aK2yV7xo-unsplash.jpg
├── nathan-anderson-7TGVEgcTKlY-unsplash.jpg
├── unsplash-image-003.jpg
├── unsplash-image-004.jpg
├── unsplash-image-005.jpg
├── unsplash-image-006.jpg
└── valentin-salja-VMroCCpP648-unsplash.jpg

If you prefer to revert the changes that succeeded, you can use the --undo or -u flag:

f2 -u # dry-run
f2 -ux # revert the changes

Everything is now restored to the previous state so you can fix the underlying issue before attempting the file renaming again.

Notes:

  • The undo command must be executed in the same directory as the original renaming operation.
  • You can only revert the last renaming change that was performed in the current directory.

Learn more about how undo works.

Clone this wiki locally