How do I rename all folders and files to lowercase on Linux?
A concise version using the "rename"
command:
find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
This avoids problems with directories being renamed before files and trying to move files into non-existing directories (e.g. "A/A"
into "a/a"
).
Or, a more verbose version without using "rename"
.
for SRC in `find my_root_dir -depth`
do
DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
if [ "${SRC}" != "${DST}" ]
then
[ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
fi
done
P.S.
The latter allows more flexibility with the move command (for example, "svn mv"
).
Why is __FILE__ uppercase and __dir__ lowercase?
I think that is because __FILE__
is a parse-time constant whereas __dir__
is a function and returns File.dirname(File.realpath(__FILE__))
For more details, see This discussion
Uppercase/lowercase filenames regarded as same
No, it is not just ASCII. NTFS volumes store the mapping in a hidden special file named $UpCase
. This means that the actual mapping can be different on different volumes on the same machine (if there are different NTFS versions on said volumes).
Windows itself handles case sensitivity in multiple ways.
- When applications opens a file they can pass a POSIX flag to request different semantics.
- The NT API allows the caller to specify case handling on names it passes to the object manager.
- Windows 10 allows you to turn off case-sensitivity on a folder with
fsutil file setCaseSensitiveInfo ...
.
Visual Studio - Renaming Files From Uppercase to Lowercase
Windows as operating system is ascendant of the operating system where there was no difference in lower and uppercase. As such at today's state Windows treats the files with same letters as the same though technically it can remember and display lower and upper cases in file names.
Overall it means your request is not natural in Windows. Maybe someone can provide you with some hack, but if you want to resolve this problem quickly move your project to the Mac where this works differently at the operating system level, perform your operation in Visual Studio for Mac and then you can continue to use Windows if you prefer.
EDIT: Actually I can tell you one hack for Windows. First rename file to whatever you want (like add 1 at the end) and then rename it to the desired file name. It will work properly.
rename filenames from uppercase to lowercase
You just need to give the full path of the files and pass that to the rename function, it should work.
Try the below code:
import os
path = 'p:/TeSt/'
for file in os.listdir(path):
os.rename(path + file, path + file.lower())
then = os.listdir(path)
print(then)
output:
['001-movie.txt', '004-kkkflfasf.txt', '002-movie.txt', '003-pics.txt']
NOTE: Forwadslash (/
) has been used in order to avoid using (\
) which is a special character in python. You can always replace /
with \\
in windows.
Opening files in directory, storing read values in lowercase list python
You are initializing Lcase_content = []
as a list. But then you redefine/overwrite it Lcase_content = content.lower()
as String
Instead use Lcase_content.append(content.lower())
import os
filenames = os.listdir('.')
#Create empty list to store file contents
Lcase_content = []
for filename in filenames:
if filename.endswith(".txt"):
with open(os.path.join('.', filename)) as file:
content = file.read()
Lcase_content.append(content.lower())
print(Lcase_content)
git case-insensitive directory renamed
To remove the directory from git, but not filesystem
git rm -r --cached folderNameToRemove
Add the directory to .gitignore, and remember to do a git push after that.
For similar scenarios, checkout this brilliant thread
Python: How to change a filename to lowercase but NOT the extension
This one handles filenames, paths across different operating systems:
import os.path
def lower_base_upper_ext(path):
"""Filename to lowercase, extension to uppercase."""
path, ext = os.path.splitext(path)
head, tail = os.path.split(path)
return head + tail.lower() + ext.upper()
It leaves possible directory names untouched, just the filename portion is lower-cased and extension upper-cased.
Related Topics
How to Validate Exits and Aborts in Rspec
(Ruby) Getting Net::Smtp Working with Gmail...
Supporting Ruby 1.9's Hash Syntax in Ruby 1.8
Circular Dependency Detected While Autoloading Constant User
How to Run a Single Test in Minitest
Rvm Ruby 1.9.1 Install Can't Locate Zlib But Its Runtime and Dev Library Are There
Foreman Only Shows Line with "Started with Pid #" and Nothing Else
Including One Erb File into Another
How to Decompress Gzip String in Ruby
Rails 3.1 Pipeline - Exclude JavaScript File
Change the Binding of a Proc in Ruby
What's the Point of Argv in Ruby
Ruby: Why Does Puts Call To_Ary
Understanding the Gemfile.Lock File
Understanding Ruby .Class and .Ancestors Methods
Ruby and "You Must Recompile Ruby with Openssl Support or Change the Sources in Your Gemfile"