Rename file using regex


up vote
0
down vote
favorite
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?
command-line rename regex
add a comment |Â
up vote
0
down vote
favorite
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?
command-line rename regex
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 like101-270265-02:00:09-M00.wav
? What if a file is101-270218-15:00:09-M00.wav
, should it become101-270209-09:00:08-M00.wav
, or101-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 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'.
â user680858
Feb 27 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 at 15:49
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
command-line rename regex
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?
command-line rename regex
command-line rename regex
asked Feb 27 at 21:08


Isaà Madueño
32
32
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 like101-270265-02:00:09-M00.wav
? What if a file is101-270218-15:00:09-M00.wav
, should it become101-270209-09:00:08-M00.wav
, or101-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 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'.
â user680858
Feb 27 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 at 15:49
add a comment |Â
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 like101-270265-02:00:09-M00.wav
? What if a file is101-270218-15:00:09-M00.wav
, should it become101-270209-09:00:08-M00.wav
, or101-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 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'.
â user680858
Feb 27 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 at 15:49
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 at 21:24
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 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'.
â user680858
Feb 27 at 21:42
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'.
â user680858
Feb 27 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 at 15:49
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 at 15:49
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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:
- Recursive bash script to collect information about each file in a directory structure
$ 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 at 15:53
Hi, @IsaÃMadueño, it should be something like thisrename -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 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 at 21:45
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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:
- Recursive bash script to collect information about each file in a directory structure
$ 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 at 15:53
Hi, @IsaÃMadueño, it should be something like thisrename -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 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 at 21:45
add a comment |Â
up vote
0
down vote
accepted
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:
- Recursive bash script to collect information about each file in a directory structure
$ 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 at 15:53
Hi, @IsaÃMadueño, it should be something like thisrename -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 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 at 21:45
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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:
- Recursive bash script to collect information about each file in a directory structure
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:
- Recursive bash script to collect information about each file in a directory structure
edited Feb 27 at 23:54
answered Feb 27 at 23:35


pa4080
12.3k52256
12.3k52256
$ 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 at 15:53
Hi, @IsaÃMadueño, it should be something like thisrename -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 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 at 21:45
add a comment |Â
$ 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 at 15:53
Hi, @IsaÃMadueño, it should be something like thisrename -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 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 at 21:45
$ 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 at 15:53
$ 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 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 at 21:37
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 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 at 21:45
Or:
rename -n -E 's/(27022018-01:)(d+)/$1.($2+5)/e; s/27022018-01/27022018-08/' /tmp/rec/*/*.wav
â pa4080
Feb 28 at 21:45
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1010410%2frename-file-using-regex%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 is101-270218-15:00:09-M00.wav
, should it become101-270209-09:00:08-M00.wav
, or101-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 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'.
â user680858
Feb 27 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 at 15:49