You are not logged in.

#1 2017-08-27 17:54:28

alma ata
Member
Registered: 2017-08-27
Posts: 26

[solved] Replace string from multiple filenames in different folders

i have this structure of folders and ffiles
 

  
    ./CD1:
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 1).cue'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 1).flac'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 1).log'

    ./CD2:
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 2).cue'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 2).flac'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 2).log'

    ./CD3:
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 3).cue'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 3).flac'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 3).log'

    ./CD4:
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 4).cue'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 4).flac'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 4).log'

    ./CD5:
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 5).cue'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 5).flac'
    'Gyorgy Ligeti - THE LIGETI PROJECT (Disc 5).log'

I need to replace the string "Gyorgy" with the string "György"

i made this little script

NUOVADIR=$DIR/nuovo # la directory coi file rinominati
cd $DIR                   # vai alla directory di lavoro
mkdir -p nuovo          #crea la sottodirectory, se non esiste già


# inizia il ciclo
for file in *
do
        nuovofile=$(echo "$file" | sed -r 's/Gyorgy/György/g')
        cp -r "$file" "$NUOVADIR/$nuovofile"

done

but it don't work, or at least, it works only if i run the script in the specific folder containing the files
but there's the problem that the cycle keep on going checking the new files created by the script itself

if i run the script from the folers containing the subfolders it don't work

what's wrong?

Last edited by alma ata (2017-09-10 18:11:13)

Offline

#2 2017-08-27 17:57:45

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [solved] Replace string from multiple filenames in different folders

man find

You can replace your script with a single find command.

Last edited by Slithery (2017-08-27 17:59:56)


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2017-08-27 18:00:22

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

slithery wrote:
man find

You can replace your entire script with a single find command.

really? this would be nice
but how can the find command replace a string contained in tha filename field

Offline

#4 2017-08-27 18:00:50

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [solved] Replace string from multiple filenames in different folders

Because globs are not expanded into subdirectories, unless you use shopt globstar. See `man bash` for details on globstar.

Also yeah, find can do this quite easily as well.

...

You shouldn't be targeting every single file unconditionally anyway.

Last edited by eschwartz (2017-08-27 18:02:05)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#5 2017-08-27 18:04:42

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

Eschwartz wrote:

Because globs are not expanded into subdirectories, unless you use shopt globstar. See `man bash` for details on globstar.

i'll see

Also yeah, find can do this quite easily as well.

how? if i knew how, i wouldn't  write here



You shouldn't be targeting every single file unconditionally anyway.

definitely

that's one of the problems i met but being this one my first script ever made, i need some help making it work

Last edited by alma ata (2017-08-27 18:06:35)

Offline

#6 2017-08-27 18:13:54

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [solved] Replace string from multiple filenames in different folders

Using find -exec and bash variable string manipulation (you can use the full power of sed instead of "${0/find/replace}" of course):

find . -name "*Gyorgy* -exec bash -c 'mv $0 ${0/Gyorgy/György}' {} \;"

Using globstar and rename:

shopt -s globstar
rename Gyorgy György **/*Gyorgy*

Last edited by eschwartz (2017-08-27 18:14:32)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#7 2017-08-27 18:35:25

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,772

Re: [solved] Replace string from multiple filenames in different folders

Is that safe? Modifying that over which I am iterating always makes me itch.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#8 2017-08-27 18:47:49

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

Eschwartz wrote:

Using find -exec and bash variable string manipulation (you can use the full power of sed instead of "${0/find/replace}" of course):

find . -name "*Gyorgy* -exec bash -c 'mv $0 ${0/Gyorgy/György}' {} \;"

Using globstar and rename:

shopt -s globstar
rename Gyorgy György **/*Gyorgy*

thanks, but I have to learn the syntax of these commands
I understand just a few

you can use the full power of sed instead of "${0/find/replace}" of course

do you mean that i have used sed in a less powerful/wrong way?
how can i use sed in the right way in that case

Offline

#9 2017-08-27 19:20:09

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [solved] Replace string from multiple filenames in different folders

ewaller wrote:

Is that safe? Modifying that over which I am iterating always makes me itch.

It's perfectly safe as long as you don't try adding more elements to iterate over *midloop*. But this doesn't modify the iterator itself, it just creates a loop with a fixed set of elements, and *then* modifies them.

alma ata wrote:

you can use the full power of sed instead of "${0/find/replace}" of course

do you mean that i have used sed in a less powerful/wrong way?
how can i use sed in the right way in that case

Sure you have used sed in a less powerful way. That doesn't mean you've used it in the wrong way, it just means the examples you gave have no need of some of the wilder things you can do with sed.

You used sed as a simple find-replace regular expression engine, but that is only one of the many things you can do with sed, and it is something that in context can also be done in pure bash (you aren't even using regular expressions, just static pattern replacement).

Here is a good guide to sed: http://www.grymoire.com/Unix/Sed.html
It will teach you a lot of really cool things, but chances are you don't need most of them, at least at the moment. But you can use it for transliterating the way `tr` does, you can put stuff in the hold buffer and do really complex things with branching control flow, you can insert the contents of some file into your output... and most of this is only relevant for cases that are more complex than merely modifying filenames. tongue

sed is a programming language, in theory there are a lot of things you could potentially want to do with it.

Last edited by eschwartz (2017-08-27 19:21:54)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#10 2017-09-10 11:35:38

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

I have another problem

how can I remove some charachters with rename? I want to substitute a string with anything, but how can i tell it to rename?

Offline

#11 2017-09-10 13:44:57

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [solved] Replace string from multiple filenames in different folders

Pass an empty string as the "replacement",

man rename

literally has an example

Offline

#12 2017-09-10 13:51:05

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

wow that was easy

i'm so stupid i didn't think for a solution like this

Offline

#13 2017-09-10 13:58:18

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 21,657

Re: [solved] Replace string from multiple filenames in different folders

Don't forget to mark as solved by editing your initial post, and maybe post the final commands you used for closure.

Offline

#14 2017-09-10 17:15:04

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

V1del wrote:

Don't forget to mark as solved by editing your initial post, and maybe post the final commands you used for closure.

i'm not able to type letters on the subject
i can only delete letters, but not add

Last edited by alma ata (2017-09-10 17:15:15)

Offline

#15 2017-09-10 18:02:42

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [solved] Replace string from multiple filenames in different folders

The title is probably too long, try using "[SOLVED]Replace string from multiple filenames in different folders".


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#16 2017-09-10 18:11:59

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

done did big_smile

Offline

#17 2017-09-11 13:06:40

alma ata
Member
Registered: 2017-08-27
Posts: 26

Re: [solved] Replace string from multiple filenames in different folders

it's always about rename so i ask here

how to deal with characters like "-"

I tried to do something like this

rename "-" "- " *

but rename doesn't accept the character

it says

rename: opzione non valida -- " "

opzione non valida: invalid option

Last edited by alma ata (2017-09-11 13:06:56)

Offline

#18 2017-09-12 01:32:17

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [solved] Replace string from multiple filenames in different folders

The standard for passing strings that begin with a "-" character and making sure a program does not think it is actually an option, is to use a double dash e.g.

rename -- "-" "- " *

Similarly to remove a file named "-weird.txt":

rm -- -weird.txt

See Guideline #10 here: http://pubs.opengroup.org/onlinepubs/96 … #tag_12_02


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#19 2017-09-12 12:49:06

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [solved] Replace string from multiple filenames in different folders

Awwww, I'm late to the party! wink But here's another super simple solution:

Use the "vidir" program, from the "moreutils" package! Changing filenames is as easy as using your favorite text editor!

Just run:

vidir CD*/*

"vidir" will use whatever text editor you have set in your "$EDITOR" variable. You will see the filenames listed in the text editor. If you make any changes to the filenames or directories and SAVE and exit the text editor, vidir will make those changes.

So, you can just use "find and replace" in your text editor to easily rename those files!

It's so easy!! big_smile

EDIT: Does your favorite text editor have "column mode"? That makes it even easier!

Last edited by drcouzelis (2017-09-12 12:51:28)

Offline

Board footer

Powered by FluxBB