1

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?

SamTzu
  • 71
  • 1
  • 12
  • 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 04 '19 at 12:23
  • Almost but no cigar. ./The Princess Bride (362) -> ./The Princess Bride () – SamTzu Jul 04 '19 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 04 '19 at 12:50
  • Give the man a cigar. That is the correct command. – SamTzu Jul 04 '19 at 13:33

2 Answers2

0

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\)$//' "{}" \;
N-Mi
  • 56
  • 4
  • What is the error message you get from `find`? – Kusalananda Jul 04 '19 at 14:06
  • @Kusalananda `find: './The Princess Bride (123)': No such file or directory` – N-Mi Jul 04 '19 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 04 '19 at 14:14
  • Indeed, this fixes the error message. I edited my answer accordingly. – N-Mi Jul 04 '19 at 14:22
  • @N-Mi did you mean `-maxdepth 1` ? – Valentin Bajrami Jul 04 '19 at 14:35
  • @ValentinBajrami I use `mindepth` to prune `'.'` from find's output, but `maxdepth` can also be used in this context : `find . -mindepth 1 -maxdepth 1 -type d -exec rename 's/ \(\d\d\d\)$//' '{}' \;` The difference between this and my answer, is if you want to rename recursively or not. – N-Mi Jul 04 '19 at 14:48
0

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".

SamTzu
  • 71
  • 1
  • 12