Rename file using regex

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








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?










share|improve this question





















  • 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










  • 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














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?










share|improve this question





















  • 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










  • 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












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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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










  • 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










  • 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










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





share|improve this answer






















  • $ 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










  • 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











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "89"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • $ 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










  • 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















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





share|improve this answer






















  • $ 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










  • 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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 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

















  • $ 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










  • 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

GRUB: Fatal! inconsistent data read from (0x84) 0+xxxxxx

`kcmshell` modules relation with `/usr/share/applications`

How to enroll fingerprints to Ubuntu 17.10 with VFS491