Tuesday, May 21, 2024
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 1803  / 3 Years ago, sat, october 9, 2021, 5:57:55

There are gazillions of questions and answers regarding how to rename files.
I'm new to Linux and this myriad of possible ways and different results and different configurations confuses me.


For example:



  • rename works, but not recursively

  • grep works recursively, but can't be used to search for file and directory names, instead of file contents

  • find works, but has a long ugly syntax when you want to search using regular expressions.


And also a lot of answers on ask ubuntu and stackoverflow do not work for me and I don't know how to debug and troubleshoot them.


So, forgive me to ask it one more time.


I'm searching for a clean, memorizable way of:


Renaming files recursively using simple regular expression.


I already do it for the content of files using this syntax:


grep -rl search_regex | xargs sed -i 's/old/new/g'


It works without a lot of frustration. It works out of the box. It just works. I'm searching for a workable solution that is neat and clean. Can you help me please?


More From » command-line

 Answers
4

You should be able to more or less keep the same format you had with grep and sed, though I think you'll still need a tool like find to supply grep with files to search through.


Edit: updated with information from the comments. thanks @pLumo and @terdon for showing it would fail for files with newline characters in the name.


find /some/directory -print0 | grep -zE "search_regex" | rename -0 's/old/new/g'

[#1448] Monday, October 11, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hreadsoplet

Total Points: 220
Total Questions: 108
Total Answers: 105

Location: Serbia
Member since Fri, Jun 3, 2022
2 Years ago
hreadsoplet questions
Fri, Nov 11, 22, 14:00, 2 Years ago
Wed, May 25, 22, 18:11, 2 Years ago
Thu, Jan 20, 22, 09:26, 2 Years ago
;