You are not logged in.
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
man find
You can replace your script with a single find command.
Last edited by Slithery (2017-08-27 17:59:56)
Offline
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
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
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
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
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
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
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.
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.
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
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
Pass an empty string as the "replacement",
man rename
literally has an example
Offline
wow that was easy
i'm so stupid i didn't think for a solution like this
Offline
Don't forget to mark as solved by editing your initial post, and maybe post the final commands you used for closure.
Offline
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
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
done did
Offline
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
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
Awwww, I'm late to the party! 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!!
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