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.

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:



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ē.
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 ...
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?
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
renaming multiple files at once pārdēvēt vairākus failus uzreiz