7

I have a few hundred files named hortstu01-01-14 then hortstu01-02-14... Do you get the idea?

Well half of the files are named hortstu_01-03-14 then hortstu_01-04-14.

This messes up sorting the files by name and getting them in the order I'd prefer them to be. So I'd like to rename the ones without the underscore to hortstu_

Is it possible to do this in a batch operation? If so how?

3 Answers 3

11

Open a terminal. Firstly, double check you have the perl rename, by typing man rename. At the bottom, it should say perl v5.18.2 or similar. (I think Ubuntu always ships the perl rename, but other distros ship other versions.)

If it's the correct version, then cd to the directory that contains these files. Then run

 rename 's/^hortstu/hortstu_/' hortstu[!_]*

Explanation

  • This will rename all files that match hortstu[!_]*. This means files that start with hortstu, and do not have _ next.
  • It will then rename them by
    • finding ^hortstu (i.e. hortstu at the beginning of the string)
    • replacing it with hortstu_.
  • N.B. the format for hortstu[!_]* is a bash glob. The format for ^hortstu is a regular expression.
3
  • I have version perl v5.14.2 will this be a problem?
    – hortstu
    Apr 10, 2014 at 4:12
  • That should work fine. As long as it's perl-based it's fine.
    – Sparhawk
    Apr 10, 2014 at 4:22
  • Yes it worked thanks for the help and I upvoted your answer as well as selecting it as the correct answer.
    – hortstu
    Apr 10, 2014 at 4:23
4

Or a Bash one liner navigate to the folder with the files and then run this in terminal:

for file in hortsu*;do echo "$file" "${file/_/-}";done;

Basically what this does is for files with the name 'hortsu' followed by any characters '*', echo the name of "$file" and also the name of file with all underscores replaced with dashes. When you feel comfortable with that just replace echo with cp to copy the files to the new name.

A pythonic solution.

Try this in a safe environement, maybe copy one or two files over to a new folder to test how it works.

Save the following code in gedit and give it a name with a file extension of '.py' something like rename_files_in_current_directory.py:

# copy this code into gedit and save it to the directory with the files in it
import os
# for files in the current directory ('.')
for file_names in os.listdir('.'):
    if '_' in file_names:
        # using os.system use cp (copy) to copy file_names to file-names by replacing
        # underscore with dash
        os.system('cp '+file_names+' '+file_names.replace('_','-'))

Make sure python is installed. 'sudo apt-get install python'

Navigate to the folder in terminal with the script and the files and then run:

python the_name_given_to_the_python_script.py

Note:

You can delete the dashes in the above commands and scripts will remove the underscores.

for file in hortsu*;do cp "$file" "${file/_/}";done;

or in the python script change:

file_names.replace('_','-') to file_names.replace('_','')

4
  • Thanks. I didn't try your methods, since the first answer seemed simpler, and it was first, but I appreciate your answer and upvoted it.
    – hortstu
    Apr 10, 2014 at 4:23
  • I was just trying to show you a couple more ways to go about it. Instead of adding an underscore, it removed them. It wasn't exactly what you were looking for, but it would allow you to sort the files as you wanted.
    – jmunsch
    Apr 10, 2014 at 4:25
  • Also (easily fixable), this answer copies instead of moving/renaming.
    – Sparhawk
    Apr 10, 2014 at 5:18
  • @hortstu Right! Instead of using cp you could use mv to rename files as well.
    – jmunsch
    Apr 10, 2014 at 5:35
4

For those who prefer GUI applications or just like the ability to preview the result, you might want to install Thunar. It's the main file manager of XFCE; you find it in the repositories.

When you select multiple files in Thunar and rename them (via context menu or F2), you get an elaborate dialog which support all kinds of rename operations. You can solve your problem like so:

enter image description here

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.