0

I've little issues trying to change the name of several amount file names using rename command.

I have 1 main directoy, and so many others inside, after this, the whole content are .wav files.

/rec/101/101-27022018-01:00:09-M00.wav

I want to change the 'hour' of the file;

101-27022018-01:00:09-M00.wav

Using the same date as reference

101-27022018-01:00:09-M00.wav

I've tried something like this:

rename 's/27022018-01/27022018-08/' *.wav -v -n

The question is, what if I want to change the name of all those files inside of all those directories? How it would be?

1 Answers1

0

If we have directory structure with two levels and there are not files within the first level:

$ tree /tmp/rec
/tmp/rec
├── 101
│   ├── 101-27022018-01:00:09-M00.wav
│   └── 101-27022018-01:00:09-M01.wav
├── 102
│   ├── 101-27022018-01:00:09-M00.wav
│   └── 101-27022018-01:00:09-M01.wav
└── 103
    ├── 101-27022018-01:00:09-M00.wav
    └── 101-27022018-01:00:09-M01.wav

$ rename -n 's/27022018-01/27022018-08/' /tmp/rec/*/*.wav

rename(/tmp/rec/101/101-27022018-01:00:09-M00.wav, /tmp/rec/101/101-27022018-08:00:09-M00.wav)
rename(/tmp/rec/101/101-27022018-01:00:09-M01.wav, /tmp/rec/101/101-27022018-08:00:09-M01.wav)
rename(/tmp/rec/102/101-27022018-01:00:09-M00.wav, /tmp/rec/102/101-27022018-08:00:09-M00.wav)
rename(/tmp/rec/102/101-27022018-01:00:09-M01.wav, /tmp/rec/102/101-27022018-08:00:09-M01.wav)
rename(/tmp/rec/103/101-27022018-01:00:09-M00.wav, /tmp/rec/103/101-27022018-08:00:09-M00.wav)
rename(/tmp/rec/103/101-27022018-01:00:09-M01.wav, /tmp/rec/103/101-27022018-08:00:09-M01.wav)

When the directory structure is more complex we can use the bash globstar option:

$ tree /tmp/rec
/tmp/rec
├── 101
│   ├── 00
│   │   └── 101-27022018-01:00:09-M00.wav
│   ├── 01
│   │   └── 101-27022018-01:00:09-M00.wav
│   ├── 101-27022018-01:00:09-M00.wav
│   └── 101-27022018-01:00:09-M01.wav
├── 102
│   ├── 00
│   │   └── 101-27022018-01:00:09-M00.wav
│   ├── 01
│   │   └── 101-27022018-01:00:09-M00.wav
│   ├── 101-27022018-01:00:09-M00.wav
│   └── 101-27022018-01:00:09-M01.wav
└── 103
    ├── 00
    │   └── 101-27022018-01:00:09-M00.wav
    ├── 01
    │   └── 101-27022018-01:00:09-M00.wav
    ├── 101-27022018-01:00:09-M00.wav
    └── 101-27022018-01:00:09-M01.wav

9 directories, 12 files

$ shopt -s globstar
$ rename -v 's/27022018-01/27022018-08/' /tmp/rec/**/*.wav

/tmp/rec/101/00/101-27022018-01:00:09-M00.wav renamed as /tmp/rec/101/00/101-27022018-08:00:09-M00.wav
/tmp/rec/101/01/101-27022018-01:00:09-M00.wav renamed as /tmp/rec/101/01/101-27022018-08:00:09-M00.wav
/tmp/rec/101/101-27022018-01:00:09-M00.wav renamed as /tmp/rec/101/101-27022018-08:00:09-M00.wav
/tmp/rec/101/101-27022018-01:00:09-M01.wav renamed as /tmp/rec/101/101-27022018-08:00:09-M01.wav
...

References:

pa4080
  • 30,621