Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change file and directory names with find?

Tags:

linux

bash

I changed project name and now I have many files an directories with old name. How to replace these names with find?

find . -name "*old_name*" -exec ???
like image 703
Nips Avatar asked Nov 02 '22 20:11

Nips


2 Answers

This find should work for you:

find . -name "old_name" -execdir mv "{}" new_name +

This will find files with the name old_name from the current dir in all sub directories and rename them to new_name.

like image 169
anubhava Avatar answered Nov 09 '22 09:11

anubhava


Below is what I have used in the past. The biggest gotcha is the RHEL rename (c) vs Debian rename (perl) - They take different options. The example below uses RHEL c based rename command. Remove the '-type f' to also rename the directories.

find . -type f -name "*old_name*" -print0 | xargs -0 -I {} /usr/bin/rename "old_name" "new_name" {}
like image 25
Josh Avatar answered Nov 09 '22 09:11

Josh