# Linux people:  Can you help me rename a bunch of files?

**URL:** <https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843>\
**Category:** Factual Questions\
**Created:** [February 2, 2017, 7:47pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843 "2017-02-02T19:47:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nars\_Glinley](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/nars_glinley/32/8177_2.png) [@Nars\_Glinley](https://boards.straightdope.com/u/Nars_Glinley)\
**Post date:** [February 2, 2017, 7:47pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/1 "2017-02-02T19:47:35Z")

</div>

Suppose that I have a directory listing that looks like this:

123CAT.sql  
456CAT.sql  
789CAT.sql  
XYZ.SQL

I would like to be able to run a script that replaces every instance of CAT with DOG in the filename only so that my directory now shows

123DOG.sql  
456DOG.sql  
789DOG.sql  
XYZ.sql

It only needs to work for the current directory. It doesn’t need to be recursive.

Any ideas? I’m currently using winscp and renaming them manually but there’s got to be an easier way.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [February 2, 2017, 7:51pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/2 "2017-02-02T19:51:57Z")

</div>

In a Linux (bash) shell:

```auto

for file in *CAT*; do
    mv $file ${file/CAT/DOG}
done

```

---

<div class="post-metadata">

**Author:** ![MrDibble](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mrdibble/32/114_2.png) [@MrDibble](https://boards.straightdope.com/u/MrDibble)\
**Post date:** [February 2, 2017, 8:23pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/3 "2017-02-02T20:23:36Z")

</div>

No need for a bash script, just run :

rename ‘s/CAT./DOG./’ \*.sql

in the folder. _rename_ should live in /usr/bin/ and should be on you $PATH so you can just run it as is. Run it with a _-n_ flag if you want a test run rather than the real thing.

---

<div class="post-metadata">

**Author:** ![MrDibble](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/mrdibble/32/114_2.png) [@MrDibble](https://boards.straightdope.com/u/MrDibble)\
**Post date:** [February 2, 2017, 8:31pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/4 "2017-02-02T20:31:27Z")

</div>

Of course, that answer depends on your flavour of Linux and whether you have Perl properly installed (that rename is just a symlink to Perl’s prename script)

---

<div class="post-metadata">

**Author:** ![Nars\_Glinley](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/nars_glinley/32/8177_2.png) [@Nars\_Glinley](https://boards.straightdope.com/u/Nars_Glinley)\
**Post date:** [February 2, 2017, 9:03pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/5 "2017-02-02T21:03:28Z")

</div>

Thanks guys (?). Both methods worked for me except for the rename -n option. This is Red Hat 7 and the rename man page only lists options for verbose, version, symlink and help.

I appreciate it.

---

<div class="post-metadata">

**Author:** ![markn\_1](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@markn\_1](https://boards.straightdope.com/u/markn_1)\
**Post date:** [February 2, 2017, 9:04pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/6 "2017-02-02T21:04:41Z")

</div>

nm

---

<div class="post-metadata">

**Author:** ![Nars\_Glinley](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/nars_glinley/32/8177_2.png) [@Nars\_Glinley](https://boards.straightdope.com/u/Nars_Glinley)\
**Post date:** [February 2, 2017, 9:07pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/7 "2017-02-02T21:07:57Z")

</div>

A simple “rename CAT DOG \*.sql” worked for me on Red Hat 7.

---

<div class="post-metadata">

**Author:** ![edwardcoast](https://avatars.discourse-cdn.com/v4/letter/e/cab0a1/32.png) [@edwardcoast](https://boards.straightdope.com/u/edwardcoast)\
**Post date:** [February 3, 2017, 3:44pm UTC](https://boards.straightdope.com/t/linux-people-can-you-help-me-rename-a-bunch-of-files/778843/8 "2017-02-03T15:44:15Z")

</div>

> [@Nars\_Glinley](#):
>
> A simple “rename CAT DOG \*.sql” worked for me on Red Hat 7.

I’m on CentOS 7 and rename is installed, but it might be part of a package that needs to be installed?
