Shell Geek: Rename Multiple Files At Once Shell Geek: Rename iš karto kelis failus
Let's say you have a directory with hundreds of files with the wrong file names, and you'd like to replace every filename containing test with prod . Tarkime, jūs turite šimtus bylų, kuriose klaidingą failų pavadinimų kataloge ir norite pakeisti kas Filename yra bandymas su prod. (this is a contrived example). (tai yra išprovokuoti pavyzdys). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Galime lengvai padaryti su "už" komanda bash, kartu su šiek tiek bash gėris. Today we'll learn how to replace text in a variable in a for loop. Šiandien mes jums sužinoti, kaip pakeisti tekstą kintamąjį už linijos.
The “for” command works like this: Už "komandą works like this:
for var in <files>;do <command> $var;done for var in <files>; daryti <command> $ var, padaryta
You can replace <files> with any file match pattern, such as * or *.txt, and you can replace <command> with any linux command. Jūs galite pakeisti <files> su bet failas rungtynės modelį, tokių kaip * ar *. txt, ir jūs galite pakeisti <command> su Linux komandos. The command will be run in sequence on each of the files matched by the file match pattern. Komanda bus paleista iš eilės apie visus failus suderinta failas rungtynės modelis.
This is where the bash variable handling makes it even more interesting. Tai kur bash kintamojo tvarkymo tampa dar įdomesnis. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax: Vietoj to, kad tiesiog daro kažką panašaus į "mv $ var", mes galime pakeisti teksto failo naudojant šią sintaksę:
${var/originaltext/replacetext} $ (var / Originaltext / replacetext)
So now, if we run this command on our directory: Taigi dabar, jeigu mes paleisti šią mūsų kataloge komandą:
for f in *;do mv $f ${f/test/prod};done dėl F *; do mv $ f $ (f / test / prod); Priimta
For each file matched by *, bash will execute a command similar to this: Kiekvieno failo suderinta * bash bus įvykdyti komandą panašus į šį:
mv test.config prod.config V. test.config prod.config
I've found that knowledge of the shell is invaluable when administering servers or just for managing your file collection, and has saved me hours of what would have otherwise been manual work. Radau, kad žinių apvalkalas yra neįkainojama, kai administravimo serverių ar tiesiog valdant savo bylų kolekcijos, ir išgelbėjo mane valandas, kas galėtų kitaip buvo rankų darbas.
And yes, I realize there are a number of tools that can accomplish renaming of multiple files. Ir taip, aš suprantu, yra daug priemonių, kurios gali atlikti pervadinti kelis failus.

Daily Email Updates Dienos paštas Atnaujinimai
You can get our how-to articles in your inbox each day for free. Galite gauti mūsų kaip prie straipsnių į Jūsų pašto dėžutę kasdien nemokamai. Just enter your name and email below: Tiesiog įveskite vardą ir elektroninio pašto adresą žemiau:



thanks, really nice article, it has saved me some time and it will alot more in the future. Ačiū, tikrai gražus straipsnis, ji išgelbėjo mane šiek tiek laiko ir jis bus daug daugiau ateityje.
Hey… thanks a lot for this article … this really saved lot of my time …. Ei ... Ačiū už šiame straipsnyje daug ... tai tikrai sutaupyti daug mano laiko .... i reallly appreciate it … I reallly appreciate it ...
You are a GOD. Jūs esate Dievas. This is what I have been looking for. Tai, ką aš ieškojau. Can this functionality exist outside of “for loop”. Ar ši funkcija yra ne "už linijos". How good is Bash's regex engine? Kaip gera yra Bash's regex variklis? Is it full featured? Tai visiškai Teminiai?
“ls [^a}*” finds all files that begin with anything BUT letter “a”. "LS [^) *" suras visus failus, kurie prasideda Anything But raidė "A". What else is possible? Ką dar galima?
This is great, and I use it frequently for changing filename suffixes. Tai puikus, ir aš naudoju jį dažnai keisti Filename priesagos.
However, if you are stuck with filenames that contain spaces, you need to quote around the variable and the replacement expression, as shown in the example below. Tačiau, jei jūs negalite išeiti su varduose, kuriuose yra tarpų, jums reikia pacituoti apie kintama ir pakeitimo išraiška, kaip parodyta žemiau esančiame pavyzdyje.
for f in *;do mv “$f” “${f/\.oga/.ogg}”;done dėl F *; do mv "$ f" "$ (f / \ .oga / .ogg)"; Priimta
renaming multiple files at once pervadinti kelis failus vienu metu