Some Help With Nemo’s Bulk Renaming

MagicMint
  8 years ago
  6

The Basics

You’ve maybe wondered what’s the purpose of nemo’s bulk renaming facility if nobody tells you how to configure it to suit your needs frown

For the time being, i.e. in Rebecca, nemo relies upon some external utility for renaming multiple items within the file manager. The corresponding command is defined in nemo’s menu under Edit› Behavior› Bulk Rename. Depending on what you want to do, there are several packages which could serve this purpose, so this is just a short guide for two use cases.

Since the packages in question are all in the repository, you could install them as usual from the package manager — it’s just for sake of consistency with some unavoidable scripting that the command line installation is listed too smiley.

For renaming files and folders

Bulk renaming happens in nemo if you select multiple items like in the following picture and then either press F2, click on Edit› Rename … or right click on marked item› Rename …:

Bulk Selection
 

The easiest and fastest way to rename at once several files and folders marked this way is to use the (presumed) godfather of this facility, a basic and hence very lightweight file manager named thunar, so let’s install it, e.g. by typing in a terminal:

…$ sudo apt-get install thunar

Don’t be afraid, it won’t supersede nemo: we’ll just use it for renaming the marked items from within nemo. All we have to do is to tell nemo to use the corresponding function of thunar by configuring the bulk renaming command (cf. above; note that you might need to actualize nemo’s view hereafter by pressing F5) to:

thunar --bulk-rename %F

Then, after pressing F2, you should see the following window where you can rename all what’s on the list to anything you wish:

Bulk Renaming With Thunar
 

For renaming pictures and music files

However, if you want to mass rename multimedia files based on their metadata, i.e. the additional information such a file already contains, you should use another utility that performs this task really well. It’s called pyRenamer and it’s in the repositories too:

…$ sudo apt-get install pyrenamer

By establishing the following command for bulk renaming, you’ll have access to a whole lot of metatags for the field ’Renamed file name pattern’, as it is illustrated below:

pyrenamer %F

Renaming Based on Metatags
 

 

With pyRenamer, there are two caveats though:

  • It cannot rename folders;
  • It only takes directories as parameters, while nemo offers him a list of files to rename. This means that you have to redo the selection within pyRenamer in order to rename the files you want.

That is, pyRenamer always ignores nemo’s selection when it starts sad:

Unmarked Renaming With Pyrenamer
 

 

To make things worse, pyRenamer cannot guess on its own which directory nemo has called it from: it will start up unwaveringly in your home directory, unless you make up a small helper script by typing:

…$ sudo gedit /usr/local/bin/pyrenamer

Therein, paste in the following code and save the file:

#!/bin/bash
# /usr/local/bin/pyrenamer (MagicMint) O0116
# pyRenamer wrapper for Nemo

cmd="/usr/bin/pyrenamer"

if [[ $# != 0 ]]; then
 #Remove "file://" from the 1st name
 dir=/`dirname "$1" | cut -d'/' -f4-`
fi
eval "$cmd" --root "$dir"
#End of script

Now, make the script executable and you’re done:

…$ sudo chmod +x /usr/local/bin/pyrenamer

Combining both into a unique command

As you can see, thunar and pyRenamer are somewhat complementary, so you could as well use both of them with the cost of two extra mouse clicks. This also has the additional benefit to (partially) work around a bug in nemo that prevents it from building up the aforesaid file list parameter correctly as soon as there is a space or any other special character in a file or directory name.

The file name filter is called ’nemo_bulk_rename.sh’. You set it up through:

…$ sudo gedit /usr/local/bin/nemo_bulk_rename.sh

Its contents should be the following:

#!/bin/bash
# /usr/local/bin/nemo_bulk_rename.sh (MagicMint) O0121
# Bulk renaming back-end for nemo

help_msg() {
 cat <<-EOF
 `awk  'FNR==3 {print gensub("# ", "", 1)}' "$0"`
 Usage: `basename $0` [ -h | --help ] file_or_folder_names
 EOF
}

case $1 in
 -h | --help )
  help_msg ;;

 *) 
  # Choose utility to use
  selection=$(zenity --list --radiolist --title="Bulk Renaming Options" --column="Type" --column="Item" TRUE "files & folders" FALSE "pictures & music")
  [[ "${PIPESTATUS[0]}" != 0 ]] && exit

  # Work around nemo’s URL encoding bug
  for file do
   file=/`echo $file | cut -d'/' -f4-`
   found=false
   test -e "$file" || break
   found=true
  done;

  if [[ $found == false ]]; then
   while ! test -d "$file"; do
    file=`dirname "$file"`
   done
   zenity --warning --title="Wrong URL name by nemo" --text="Opening directory\n\t$file\ninstead."
   file="file://$file/."

  # Start the right utility w/ the right files w/in the right directory
  else
   file="$@"
  fi
  if echo $selection | grep -q "files"; then
   # For files & directories call thunar directly
   eval thunar --bulk-rename "$file"
  else
   # For pictures & music this should call the wrapper for pyrenamer
   eval pyrenamer "$file"
  fi
 ;;
esac
#End of script

Again, the script must be made executable:

…$ sudo chmod +x /usr/local/bin/nemo_bulk_rename.sh

Last but not least, the bulk renaming command for nemo will read:

nemo_bulk_rename.sh

Notice

The manual recovery steps depend on the utility chosen:

  • With thunar, you have to re-arrange the file list with the action buttons, i.e. by first clearing the list, and then re-adding the items to be renamed;
  • With pyRenamer, you can simply browse in the directory tree to the right folder.
Comments
Xerus 6 years ago

Using Mint 18.3, "thunar -B" does neither work for me


bartv 7 years ago

Thanks MintChip, Your -B comment finally got the thunar bulk rename working!


MintChip 8 years ago

Thanks for the excellent 'tute,' MagicMint!
In Mint 18.1 'thunar --bulk-rename %F'doesn't work. You have to drop the %F. I use -B instead of --bulk-rename (they're equivalent), so it becomes:
thunar -B


Rebel450 9 years ago

THANK YOU !


MagicMint 9 years ago

@hdzsroi #1: Thank you first for your commendation :-)

@hdzsroi #2: Please specify what do you mean by “not working”. I’m on 17.1 Cinnamon too, and besides the two renaming utilities, there should be nothing else you need to run the scripts. They should be installed and tested though, i.e. called from the menu first.

Try nemo_bulk_rename.sh from a terminal; it must be provided with URI filenames which you get from nemo when you Right click› Copy on an (or more) items. You can Right click› Paste them as parameters for the script into the terminal. (For pyrenamer, it will throw out some warnings.)


hdzsroi 9 years ago

Okay, I just checked all alternatives. The last one- nemo_bulk_rename.sh isn't working on my 17.1 Cinnamon. Help?


hdzsroi 9 years ago

I don't understand why nobody commented here to say "Thank You!" :)
Yep, I'm new to linux and been wondering what's that option about. I use FileBot for bulk renaming (awesome app btw) but they are for my multimedia files.
Just wanted to let you know that your explanation and descriptions are so neat. I hope your scripts are as good too, I am going to check and will give my feedback later maybe :)
Now heading to your other tutorials even though I'm not particularly in need ;)