Subscribe to How-To Geek Mag-subscribe sa Paano-Upang Geek

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: Palitan ng pangalan ng Maramihang Mga file Sa sandaling

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 . Ipagpalagay natin na ikaw ay may isang direktoryo sa daan-daang mga file na may maling pangalan ng file, at nais mong palitan ang lahat ng filename na naglalaman ng mga pagsubok na may mag-udyok. (this is a contrived example). (ito ay isang contrived halimbawa). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Maaari naming madaling gawin ito na may "para sa" command sa Bash, pinagsama sa isang maliit na piraso ng Bash kabutihan. Today we'll learn how to replace text in a variable in a for loop. Today namin malaman kung paano palitan ang teksto sa isang variable sa isang para sa loop.

The “for” command works like this: Ang "para sa" command na gumagana tulad nito:

for var in <files>;do <command> $var;done para sa var sa <files>; gawin <command> $ var; tapos na

You can replace <files> with any file match pattern, such as * or *.txt, and you can replace <command> with any linux command. Maaari mong palitan ang <files> sa anumang pattern file na tugma, tulad ng * o *. txt, at maaari mong palitan ang <command> sa anumang utos ng linux. The command will be run in sequence on each of the files matched by the file match pattern. Ang command ay tatakbo sa pagkakasunod-sunod sa bawat isa sa mga file na katugma ng pattern file na tumutugma.

This is where the bash variable handling makes it even more interesting. Ito ay kung saan ang Bash variable na paghawak ay gumagawa ito ng mas maraming mga kagiliw-giliw. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax: Sa halip na lamang ng paggawa ng isang bagay tulad ng "mv $ var", maaari naming palitan ang teksto sa filename na gamit ang syntax:

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

So now, if we run this command on our directory: Kaya ngayon, kung tatakbo namin ang utos na ito sa aming direktoryo ng:

for f in *;do mv $f ${f/test/prod};done para sa mga f *; gawin mv f $ $ (f / test / mag-udyok); tapos na

For each file matched by *, bash will execute a command similar to this: Para sa bawat file na katugma sa pamamagitan ng *, Bash ay magpatakbo ng isang utos na katulad nito:

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. Ako nahanap na kaalaman sa shell ay napakahalaga kapag ang pagbibigay ng mga server o para lamang sa pamamahala ng iyong file collection, at may naka-save na ako ng oras ng kung ano ang gusto ay sa kabilang banda ay manwal na trabaho.

And yes, I realize there are a number of tools that can accomplish renaming of multiple files. At oo, napagtanto ko diyan ay isang bilang ng mga kasangkapan na maaaring ganapin renaming ng maramihang mga file.

This article was originally written on 07/17/07 Tagged with: Ang artikulong ito ay orihinal na isinulat sa 07/17/07 Tagged with: Ubuntu Ubuntu

Daily Email Updates Araw-araw na Updates Email

You can get our how-to articles in your inbox each day for free. Maaari kang makakuha ng aming kung-paano na mga artikulo sa iyong inbox sa bawat araw para sa libre. Just enter your name and email below: Ilagay lamang ang inyong pangalan at email sa ibaba:


Name: Pangalan:
Email: Email:

Comments (5) Comments (5)

  1. skeptic may pag-aalinlangan

    thanks, really nice article, it has saved me some time and it will alot more in the future. thanks, really nice article, ito ay may na-save ako ng ilang oras at ito ay isang pulutong pa sa hinaharap.

  2. sachadon sachadon

    Hey… thanks a lot for this article … this really saved lot of my time …. Hey ... thanks a lot para sa article na ito ... talagang naka-save na pulutong ng aking oras .... i reallly appreciate it … i reallly pahalagahan ito ...

  3. felipe alvarez Felipe Alvarez

    You are a GOD. Ikaw ay isang GOD. This is what I have been looking for. Ito ay kung ano ako ay naghahanap. Can this functionality exist outside of “for loop”. Puwede ang functionality na ito umiiral sa labas ng "para sa loop". How good is Bash's regex engine? Paano mabuting ay regex engine's Bash? Is it full featured? Ay ito ang buong itinampok?

    “ls [^a}*” finds all files that begin with anything BUT letter “a”. "Ls [^ isang) *" na nahahanap ang lahat ng mga file na nagsisimula sa kahit ano PERO titik "a". What else is possible? Ano pa ang maaari?

  4. Keith Keith

    This is great, and I use it frequently for changing filename suffixes. Ito ay malaki, at gamitin ko ito para sa mga madalas na pagpapalit ng filename suffixes.

    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. Subalit, kung ikaw ay mapagmataas na may filename na naglalaman ng mga puwang, kailangan mong i-quote sa paligid ng variable at ang kapalit na expression, tulad ng ipinakita sa halimbawa sa ibaba.

    for f in *;do mv “$f” “${f/\.oga/.ogg}”;done para sa mga f *; gawin mv "$ f" "$ (f / \ .oga / .ogg)"; tapos na

  5. scripter scripter

Leave a Comment Iwanan ng isang Puna




Leave your Iwanan ang iyong mga friendly friendly comment here. puna dito.

If you have a computer help question, Kung ikaw ay may isang computer ng tulong tanong, click here to leave it on the forums i-click dito upang mag-iwan ito sa forums instead. sa halip ng.

Note: Your comment may not show up immediately on the site. Tandaan: Ang iyong puna ay maaaring hindi magpapakita agad sa site.

Our Friends Ang aming mga Friends
Getting Started Pagsisimula


About How-To Geek Tungkol sa Paano-Upang Geek
What Is That Process? Ano ba ang Proseso Iyon?
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.