Ajinkya
Ajinkya

Reputation: 81

Search and Replace File and Folder name

I have the following file structure :

.
├── data
│   └── test_sample.txt
└── demo_test
    ├── data.php
    ├── index_test.php
    └── Test.php

I want to replace 'test' with 'demo'. The final file structure will be

.
├── data
│   └── demo_sample.txt
└── demo_demo
    ├── data.php
    ├── index_demo.php
    └── Test.php

How can I achieve this via a shell script.

Thanks in Advance.

Upvotes: 1

Views: 2576

Answers (1)

neric
neric

Reputation: 4221

You need to do it in two phases:

  1. Rename the directories
  2. Rename the files


cd yourDir
find . -type d -name "*test*" | while read f; do mv $f $(echo $f | sed 's/test/demo/'); done
find . -type f -name "*test*" | while read f; do mv $f $(echo $f | sed 's/test/demo/'); done

Find lists all the files and directories that matches "test" we then process the ouput of the command line by line

Upvotes: 5

Related Questions