FelixFrog
FelixFrog

Reputation: 61

Multiple and recursive file renaming in bash

In Linux I have a folder with the following structure:

.
├── a
│   ├── 00000400000000030bfd.dat
│   ├── 10000400000000030bfd.dat
│   ├── 20000400000000030bfd.dat
│   ├── etc....
├── b
│   ├── 00000401000000030bfd.dat
│   ├── 10000401000000030bfd.dat
│   ├── 20000401000000030bfd.dat
│   ├── etc....
├── c
│   ├── 00000402000000030bfd.dat
│   ├── 10000402000000030bfd.dat
│   ├── 20000402000000030bfd.dat
│   ├── etc....
├── d
│   ├── etc....
├── e
│   ├── etc....
├── f

And so on until the "p" folder. I want to rename every .dat file in every directory to .html file with a bash script. How i can do it?

Upvotes: 0

Views: 128

Answers (2)

choroba
choroba

Reputation: 241868

Use a loop.

for file in {a..p}/*.dat ; do
    mv "$file" "${file%.dat}.html"
done

${file%.dat} removes .dat from the end of the value of the $file.

Upvotes: 2

Greg Tarsa
Greg Tarsa

Reputation: 1642

Here is a version that uses all traditional commands:

find log -name "*.dat" | 
  sed 's/.log$//;s/mv &.dat &.html/' |
  bash

Essentially, the find creates the target name list, the sed makes the names generic and then generates a mv command that does the rename and then pipes the results to bash for execution.

The bash command can be omitted to merely get a list of the mv commands for eyeball testing. You can also add a -x to the bash command to get a log of each mv command executed.

Upvotes: 0

Related Questions