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: Premenovanie viacerých súborov naraz

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 . Povedzme, že máte adresár sa stovky súborov s názvami súborov zle, a chcete vymeniť každý súbor obsahujúci test s prod. (this is a contrived example). (to je neprirodzený príklad). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Môžeme ľahko urobiť s "na" príkaz bash, v kombinácii s trochou bash dobroty. Today we'll learn how to replace text in a variable in a for loop. Dnes sa dozvieme, ako nahradiť text v premennej v cykle for.

The “for” command works like this: "Pre" príkaz funguje takto:

for var in <files>;do <command> $var;done pre var v <files>; do <příkaz> $ var; urobiť

You can replace <files> with any file match pattern, such as * or *.txt, and you can replace <command> with any linux command. Môžete nahradiť <files> so vzorom súbor zápas, ako * alebo *. txt a môžete nahradiť <příkaz> s linux príkazom. The command will be run in sequence on each of the files matched by the file match pattern. Príkaz bude prebiehať v poradí na každom zo súborov zodpovedať vzoru súbor zápas.

This is where the bash variable handling makes it even more interesting. Toto je miesto, kde bash variabilný manipuláciu robí ešte zaujímavejšie. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax: Namiesto toho, aby práve to, niečo ako "mv $ var", môžeme nahradiť text v názve súboru pomocou tejto syntaxe:

${var/originaltext/replacetext} $ (Var / Originaltext / replacetext)

So now, if we run this command on our directory: Takže teraz, keď sme spustenie tohto príkazu na našom adresári:

for f in *;do mv $f ${f/test/prod};done pre f in *; do mv $ f $ (f / test / prod); urobiť

For each file matched by *, bash will execute a command similar to this: U každého súboru zodpovedajúcemu *, bude bash spustiť príkaz podobný tomuto:

mv test.config prod.config mv 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. Zistil som, že znalosť shellu je neoceniteľné pri správe serverov, alebo len pre správu v albumoch, a zachránila ma hodín, čo by inak bola ručná práca.

And yes, I realize there are a number of tools that can accomplish renaming of multiple files. A áno, uvedomujem si, existuje rad nástrojov, ktoré môžu dosiahnuť premenovanie viacerých súborov naraz.

This article was originally written on 07/17/07 Tagged with: Tento článok bol pôvodne napísaný na 07/17/07 Tagged s: Ubuntu Ubuntu

Daily Email Updates Denný Svářeč

You can get our how-to articles in your inbox each day for free. Môžete si naše jak-na články vo vašej schránky každý deň zadarmo. Just enter your name and email below: Stačí zadať svoje meno a e-mail nižšie:


Name: Meno:
Email: E-mail:

Comments (5) Komentáre (5)

  1. skeptic skeptik

    thanks, really nice article, it has saved me some time and it will alot more in the future. vďaka, naozaj pekný článok, má ma zachránil nejaký čas a bude oveľa viac v budúcnosti.

  2. sachadon sachadon

    Hey… thanks a lot for this article … this really saved lot of my time …. Ahoj ... díky moc za tento článok ... to naozaj zachránil veľa času .... i reallly appreciate it … reallly já si toho vážim ...

  3. felipe alvarez Felipe Alvarez

    You are a GOD. Ty si Boh. This is what I have been looking for. To je to, čo som hľadal. Can this functionality exist outside of “for loop”. Môže byť táto funkcia existujú mimo "na slučke". How good is Bash's regex engine? Ako kvalitná je Bash je regex motor? Is it full featured? Je to plne vybavený?

    “ls [^a}*” finds all files that begin with anything BUT letter “a”. "Ls [^) *" nájde všetky súbory, ktoré začínajú písmenom niečo VUT v Brne "a". What else is possible? Čo je ešte možné?

  4. Keith Keith

    This is great, and I use it frequently for changing filename suffixes. To je skvelé, a používam ho často pre zmenu prípony súboru.

    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. Avšak, ak ste sa lepia so súbormi, ktoré obsahujú medzery, je potrebné citovať okolo premenné a nahradenie prejavu, ako je uvedené v príklade nižšie.

    for f in *;do mv “$f” “${f/\.oga/.ogg}”;done pre f in *; do mv "$ f" "$ (f / \. oga /. ogg)"; urobil

  5. scripter Scripter

Leave a Comment Zanechať komentár




Leave your Zanechajte svoj friendly priateľský comment here. komentár tu.

If you have a computer help question, Ak máte počítač pomôže otázku, click here to leave it on the forums kliknite tu nechať to na fórach instead. miesto.

Note: Your comment may not show up immediately on the site. Poznámka: Váš komentár sa nezobrazia okamžite na mieste.

Our Friends Naši priatelia
Getting Started Začíname


About How-To Geek O tom, ako-To Geek
What Is That Process? Čo je to za proces?
svchost.exe svchost.exe
jusched.exe jusched.exe
dwm.exe Dwm.exe
ctfmon.exe ctfmon.exe
wmpnetwk.exe wmpnetwk.exe
mDNSResponder.exe mDNSResponder.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.