Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find a pattern in files and rename them [closed]

Tags:

linux

bash

I use this command to find files with a given pattern and then rename them to something else

find . -name '*-GHBAG-*' -exec bash -c 'echo mv $0 ${0/GHBAG/stream-agg}' {} \; 

As I run this command, I see some outputs like this

mv ./report-GHBAG-1B ./report-stream-agg-1B mv ./reoprt-GHBAG-0.5B ./report-stream-agg-0.5B 

However at the end, when I run ls, I see the old file names.

like image 652
mahmood Avatar asked Mar 08 '13 09:03

mahmood


People also ask

How do I find and replace in bulk file names?

To batch rename files, just select all the files you want to rename, press F2 (alternatively, right-click and select rename), then enter the name you want on the first file. Press Enter to change the names for all other selected files.


2 Answers

You are echo'ing your 'mv' command, not actually executing it. Change to:

find . -name '*-GHBAG-*' -exec bash -c 'mv $0 ${0/GHBAG/stream-agg}' {} \; 
like image 143
kamituel Avatar answered Sep 29 '22 10:09

kamituel


I would suggest using the rename command to perform this task. rename renames the filenames supplied according to the rule specified as a Perl regular expression.

In this case, you could use:

rename 's/GHBAG/stream-agg/' *-GHBAG-* 
like image 36
anumi Avatar answered Sep 29 '22 10:09

anumi