I allowed Calibre access to my Library and it decided to change all my folder names adding (123) after every folder. (Replace 123 with random numbers.)

What would be best command to use on Linux (Debian) terminal to remove all " (???)" strings from folder names?

  • 1
    A sane way is for example to use: find . -type d -exec bash -c 'name=${1//[0-9]}; echo "$1 -> $name"' _ {} \; if all works, substitute echo with mv and remove ->. – Valentin Bajrami Jul 4 at 12:23
  • Almost but no cigar. ./The Princess Bride (362) -> ./The Princess Bride () – SamTzu Jul 4 at 12:32
  • 3
    I see. Try this version. find . -type d -execdir bash -c 'dest=${1//[0-9_() ]/}; [[ -d $dest ]] || echo -- "$1" "$dest"' _ {} \; not tested though but again, change echo to mv if you are sure! – Valentin Bajrami Jul 4 at 12:50
  • Give the man a cigar. That is the correct command. – SamTzu Jul 4 at 13:33

The rename tool (available in rename package in Debian) lets you rename files using a Perl regexp.

Assuming your folder contains only directories, you can use rename this way :

rename 's/ \(\d\d\d\)$//' *

If you have files and directories, and you only want to rename directories, you can do this :

find . -mindepth 1 -depth -type d -exec rename 's/ \(\d\d\d\)$//' "{}" \;
  • What is the error message you get from find? – Kusalananda Jul 4 at 14:06
  • @Kusalananda find: './The Princess Bride (123)': No such file or directory – N-Mi Jul 4 at 14:11
  • 1
    Yes, because it tries to enter a directory that you now have renamed. Adding -depth to the find command would alleviate that by doing a depth-first traversal instead. – Kusalananda Jul 4 at 14:14
  • Indeed, this fixes the error message. I edited my answer accordingly. – N-Mi Jul 4 at 14:22
  • @N-Mi did you mean -maxdepth 1 ? – Valentin Bajrami Jul 4 at 14:35

For future reference...

If you wish to fix folders in your Library that Calibre messed up you can use this command:

cd to-your-parent-folder

find . -type d -execdir bash -c 'dest=${1//[0-9_()]/}; [[ -d $dest ]] || mv -- "$1" "$dest"' _ {} \;

Best test it first though by replacing "mv" with "echo".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.