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: Pārsaukt vairākus failus uzreiz

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 . Pieņemsim, ka jums ir direktorija ar simtiem failus ar nepareizu faila nosaukumu, un jūs vēlaties nomainīt ik filename satur testējamo ar prod. (this is a contrived example). (tas ir izdomāts piemērs). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Mēs varam viegli izdarīt ar "par" komandu bash kopā ar mazliet bash labestību. Today we'll learn how to replace text in a variable in a for loop. Šodien mēs uzzinātu, kā nomainīt tekstu mainīgo lielumu ar cilpu.

The “for” command works like this: "Par" komanda darbojas šādi:

for var in <files>;do <command> $var;done lai var ar <files>; do <command> $ var; darīts

You can replace <files> with any file match pattern, such as * or *.txt, and you can replace <command> with any linux command. Jūs varat nomainīt <files> ar jebkuru failu atbilst modelim, piemēram, * vai *. txt, un jūs varat nomainīt <command> ar jebkuru linux komandu. The command will be run in sequence on each of the files matched by the file match pattern. Komanda tiks veikta secīgi par katru failu saskaņota ar failu spēles modelī.

This is where the bash variable handling makes it even more interesting. Šī ir vieta, kur bash mainīgo apstrādi padara vēl interesantāku. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax: Nevis tikai kaut ko līdzīgu "mv $ var", mēs varam aizstāt tekstu filename izmantojot šo sintaksi:

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

So now, if we run this command on our directory: Tāpēc tagad, ja mēs palaist šo komandu Mūsu katalogs:

for f in *;do mv $f ${f/test/prod};done kur f in *; do mv $ f $ (f / test / prod); darīts

For each file matched by *, bash will execute a command similar to this: Katram failam saskaņota ar * bash izpildīs komandu, kas ir līdzīgs šim:

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. Es esam noskaidrojuši, ka zināšanas par apvalks ir nenovērtējams, lietojot serveros vai tikai, lai pārvaldītu jūsu attēlu kolekcijā, un ir ietaupījuši man stundas par to, kas ir citādi roku darbu.

And yes, I realize there are a number of tools that can accomplish renaming of multiple files. Un jā, es saprotu, ka ir vairāki instrumenti, ko var paveikt pārdēvēšanu no vairākiem failiem.

This article was originally written on 07/17/07 Tagged with: Šis pants tika sākotnēji rakstīts uz 07/17/07 tagged with: Ubuntu Ubuntu

Daily Email Updates Daily Email Updates

You can get our how-to articles in your inbox each day for free. Jūs varat saņemt mūsu how-to rakstus savā pastkastītē katru dienu par brīvu. Just enter your name and email below: Vienkārši ievadiet jūsu vārdu un e-pasta zemāk:


Name: Vārds:
Email: E-pasts:

Comments (5) Komentāri (5)

  1. skeptic skeptiķis

    thanks, really nice article, it has saved me some time and it will alot more in the future. paldies, tiešām jauks raksts, tas ir ietaupījuši man kādu laiku, un tas būs daudz vairāk nākotnē.

  2. sachadon sachadon

    Hey… thanks a lot for this article … this really saved lot of my time …. Hei ... thanks daudz par šo rakstu ... tas patiešām saglabāti daudz mana laika .... i reallly appreciate it … i reallly appreciate it ...

  3. felipe alvarez Felipe Alvarez

    You are a GOD. Jums ir Dievs. This is what I have been looking for. Šis ir tas, ko esmu meklējis. Can this functionality exist outside of “for loop”. Vai šī funkcija pastāvēt ārpus "attiecībā uz cilpas". How good is Bash's regex engine? Cik labi ir Bash's regex dzinējs? Is it full featured? Tas ir pilnīgi Featured?

    “ls [^a}*” finds all files that begin with anything BUT letter “a”. "Ls [^) *", atrod visus failus kas sākas ar kaut ko, bet burtu "". What else is possible? Kas vēl ir iespējams?

  4. Keith Keith

    This is great, and I use it frequently for changing filename suffixes. Tas ir liels, un es to izmantot bieži, lai mainītu faila nosaukumu galotnes.

    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. Tomēr, ja jums ir iestrēdzis ar filenames kas satur atstarpes, nepieciešams citēt ap mainīgo un rezerves vārda, kā redzams piemērs zemāk.

    for f in *;do mv “$f” “${f/\.oga/.ogg}”;done kur f in *; do mv "$ f" "$ (f / \ .oga / .ogg)", parakstīts

  5. scripter scripter

Leave a Comment Leave Comment




Leave your Atstājiet savu friendly draudzīgs comment here. komentārus šeit.

If you have a computer help question, Ja Jums ir dators palīdzēt jautājumu, click here to leave it on the forums click here to atstāt uz forumu instead. vietā.

Note: Your comment may not show up immediately on the site. Piezīme: Jūsu komentārs var neparādīties pat tieši uz vietas.

Our Friends Our Friends
Getting Started Getting Started


About How-To Geek Par How-To Geek
What Is That Process? Kas ir šis process?
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.