Important! This is an automatic machine translated page. If you can read english, you should Click Here to read the original English version of the article.

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.

This article was originally written on 07/17/07 Tagged with: Šis straipsnis buvo parašytas ant 07/17/07 Tagged with: Ubuntu Ubuntu

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:


Name: Vardas:
Email: Paštas:

Comments (5) Komentarai (5)

  1. skeptic skeptikas

    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.

  2. sachadon sachadon

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

  3. felipe alvarez Felipe Alvarez

    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?

  4. Keith Keith

    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

  5. scripter scripter

Leave a Comment Palikite komentarą




Leave your Palikite friendly draugiškas comment here. komentarus.

If you have a computer help question, Jei turite kompiuteryje Pagalba klausimą, click here to leave it on the forums Spauskite čia, jei norite palikti jį dėl forumai instead. vietoj.

Note: Your comment may not show up immediately on the site. Pastaba: Jūsų komentaras gali būti rodomas iš karto svetainėje.

Our Friends Mūsų draugai
Getting Started Paruo


About How-To Geek Apie How-To Geek
What Is That Process? Kas yra šis procesas?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe dwm.exe
ctfmon.exe Ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
wmpnscfg.exe wmpnscfg.exe
rundll32.exe rundll32.exe
wfcrun32.exe wfcrun32.exe
Ipoint.exe Ipoint.exe
Itype.exe Itype.exe
Wfica32.exe Wfica32.exe
Mobsync.exe Mobsync.exe
conhost.exe conhost.exe
Dpupdchk.exe Dpupdchk.exe Adobe_Updater.exe Adobe_Updater.exe

Copyright © 2006-2009 HowToGeek.com. Copyright © 2006-2009 HowToGeek.com. All Rights Reserved. All Rights Reserved.