Bash convert all files to lowercase
Which one is it? I'm assuming Bash? By cmd I mean "command line" terminal — Kop. Duplicate of: serverfault. Add a comment. Active Oldest Votes.
Improve this answer. Studer Studer 1, 9 9 silver badges 16 16 bronze badges. Christopher Karel Christopher Karel 6, 1 1 gold badge 26 26 silver badges 34 34 bronze badges. This fails for files that have spaces in their names. This also fails for me if a subfolder contains capital letters.
This is mild variation to Christopher 's answer, find. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked 0. Related Hot Network Questions.
Server Fault works best with JavaScript enabled. Accept all cookies Customize settings. It has the advantage that if you cd to any directory within your own home folder that is it will change all files and folders to lower case recursively please use with caution. It is a brilliant command line fix and especially useful for those multitudes of albums you have stored on your drive.
You can specify a directory in place of the dot. I hope this solution proves useful the one thing this command does not do is replace spaces with underscores - oh well another time perhaps. I am focusing on converting alphabets between a-z from upper case to lower case. Any other characters should just be printed in stdout as it is For Bash before 4 My Mac still uses Bash 3.
I used the corrected version of ghostdog74 's answer to create a more portable version. I read comments about setting the result to a var, but that is not really portable in Bash , since we can't return strings. Printing it is the best solution. Then use printf again to convert the number back to a char. And this is the working version with examples. Please note the comments in the code, as they explain a lot of stuff:.
I am using this for some case-insensitive CLI options, for example. If using v4, this is baked-in. If not, here is a simple, widely applicable solution. Other answers and comments on this thread were quite helpful in creating the code below.
I've timed 1, iterations of this approach for a small string 25 characters and a larger string characters , both for lowercase and uppercase conversions. Since the test strings are predominantly lowercase, conversions to lowercase are generally faster than to uppercase. I've compared my approach with several other answers on this page that are compatible with Bash 3.
My approach is far more performant than most approaches documented here, and is even faster than tr in several cases. Timing results for 1, iterations of characters consisting of the poem "The Robin" by Witter Bynner :.
The approach is simple: while the input string has any remaining uppercase letters present, find the next one, and replace all instances of that letter with its lowercase variant. Repeat until all uppercase letters are replaced. For Bash versions earlier than 4. In spite of how old this question is and similar to this answer by technosaurus. I had a hard time finding a solution that was portable across most platforms That I Use as well as older versions of bash.
I have also been frustrated with arrays, functions and use of prints, echos and temporary files to retrieve trivial variables. This works very well for me so far I thought I would share. My main testing environments are:. Simple C-style for loop to iterate through the strings. For the line below if you have not seen anything like this before this is where I learned this.
To store the transformed string into a variable. For the Bash command line and depending on locale and international letters, this might work assembled from the answers from others :. Case modification. This expansion modifies the case of alphabetic characters in parameter.
The pattern is expanded to produce a pattern just as in pathname expansion. Each character in the expanded value of parameter is tested against pattern , and, if it matches the pattern, its case is converted. The pattern should not attempt to match more than one character. If pattern is omitted, it is treated like a? To keep things simple I didn't add any set -e support or any error checking really Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to convert a string to lower case in Bash? Ask Question. Asked 11 years, 11 months ago.
Active 1 month ago. Viewed 1. Improve this question. See also : stackoverflow. Add a comment. Active Oldest Votes. Improve this answer. Am I missing something, or does your last example in Bash actually do something completely different? It only works for the capitalized letters and skips the already-lowercased letters.
How does one get the output into a new variable? Ie say I want the lowercased string into a new variable?
Show 24 more comments. Edit: Corrected tilde behavior to match Bash 4. Benjamin W. Dennis Williamson Dennis Williamson k 87 87 gold badges silver badges bronze badges. That's using bash 4. HubertKario: That's weird. It's the same for me in Bash 4. It's a bug and I've reported it. It will probably exhibit the same failure. So the problem is at least partly not Bash's. DennisWilliamson: Yeah, I've noticed that too see comment to Shuvalov answer.
HubertKario: The Bash maintainer has acknowledged the bug and stated that it will be fixed in the next release. Show 6 more comments. Zombo 1. I do have correct locale set and locale files generated. Have any idea what could I be doing wrong? Some of the other suggestions did not. Why is [:upper:] needed? The same question why [:upper:] is needed. Peter Mortensen Ignacio Vazquez-Abrams Ignacio Vazquez-Abrams k gold badges silver badges bronze badges. I am still a beginner I strongly recommend the sed solution; I've been working in an environment that for some reason doesn't have tr but I've yet to find a system without sed , plus a lot of the time I want to do this I've just done something else in sed anyway so can chain the commands together into a single long statement.
The bracket expressions should be quoted. In tr [A-Z] [a-z] A , the shell may perform filename expansion if there are filenames consisting of a single letter or nullgob is set.
I've been doing a lot of cross-platform shell scripting lately, and with the requirement that nothing extra be installed it makes things very tricky! However I've yet to encounter a system without sed — Haravikk. Note that tr [A-Z] [a-z] is incorrect in almost all locales.
0コメント