Sunday, May 5, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 614  / 1 Year ago, mon, february 6, 2023, 12:02:08

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?


More From » filesystem

 Answers
7

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.


[#26103] Tuesday, February 7, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
landarre

Total Points: 254
Total Questions: 96
Total Answers: 109

Location: Burundi
Member since Sun, Apr 16, 2023
1 Year ago
landarre questions
;