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?

  • What do you want to change it to? Should the hour become the year? What do the other directories look like? Can you also have files like 101-270265-02:00:09-M00.wav? What if a file is 101-270218-15:00:09-M00.wav, should it become 101-270209-09:00:08-M00.wav, or 101-270209-18:00:09-M00.wav, or something else? Please [edit] your question and clarify exactly how you need the files to be renamed. – terdon Feb 27 '18 at 21:24
  • It seems to me the 'rename' command will not be appropriate if you say there are 'all those files inside of all those directories'. I think you will have to consider writing a script that enters a directory and on a per-file basis splits a file name into several variables, change the variable you need changing, then create the new filename in another variable and then do a 'mv oldfile newfile'. –  Feb 27 '18 at 21:42
  • All those .wav files has wrong hour, but the date is right, so, taking the date as reference of this specific day, I'll change the hour recursively of all those .wav files, that are inside of those directories. – Isaí Madueño Feb 28 '18 at 15:49

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
  • 29,831
  • $ rename -n 's/27022018-01/27022018-08/' /tmp/rec//.wav What if I want increase 5 mins near the hour? how could it be? -> $ rename -n 's/27022018-01:{2}/27022018-08:{+5}/' /tmp/rec//.wav I'm new with those Perl regex. – Isaí Madueño Feb 28 '18 at 15:53
  • Hi, @IsaíMadueño, it should be something like this rename -n 's/(27022018-01:)(\d+)/$1.(${2}+5)/e' /tmp/rec/*/*.wav, but I do not know the full answer. This will add +5 to the second variable, but the first part $1=27022018-01: will not be changed. The next is more close, maybe: rename -n 's/(27022018-)(01)(:)(\d+)/$1.($2+7).$3.($4+5)/e' /tmp/rec/*/*.wav – pa4080 Feb 28 '18 at 21:37
  • Or: rename -n -E 's/(27022018-01:)(\d+)/$1.($2+5)/e; s/27022018-01/27022018-08/' /tmp/rec/*/*.wav – pa4080 Feb 28 '18 at 21:45