how do i convert file names to lower case and remove the underscore from the name?

User Name

Remember Me?

Password

Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

how do i convert file names to lower case and remove the underscore from the name?

Hey everyone

do any of you know how to convert a set of file names in a directory, to lowercase and remove the underscores from their names? Is there a special shell script i could use or a special command? also how do i go about running it?

To convert underscores to spaces:
If you have python installed on your computer, this script will do it:

Code:

#!/usr/bin/python
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# underscore within the filename with a space
import os, sys
if len(sys.argv) == 1:
filenames = os.listdir(os.curdir)
else:
filenames = sys.argv[1:]
for filename in filenames:
if '_' in filename:
newfilename = filename.replace('_', ' ')
print "Renaming", filename, "to", newfilename, "..."
os.rename(filename, newfilename)

just copy and paste the above in a text file and make it executable. Then place it in the directory with the files you wish to change and click it. All of the underscores will convert to spaces. This code is runs faster than a comparable bash shell script and runs on windows (with python installed) too.

A common way of replacing characters in the shell is to use variable expansion.
For example: "${filename// /_}" will replace all spaces in "$filename" with underscores. The "//" doubleslash will do it globally in the variable, and not the first one.
"${filename// /}" will remove spaces. You can use character classes as well:
"${filename//[ _]/}". This will remove all spaces and underscores.

Combining this with the earlier post:
for i in *; do mv "$i" "$(echo ${i//_/} | tr '[A-Z]' '[a-z]')"; done

#!/usr/bin/python
# the following script will traverse through all of the files
# within the directory where it is placed and replace each
# space within the filename with an underscore
import os, sys
if len(sys.argv) == 1:
filenames = os.listdir(os.curdir)
else:
filenames = sys.argv[1:]
for filename in filenames:
if ' ' in filename:
newfilename = filename.replace(' ', '_')
print "Renaming", filename, "to", newfilename, "..."
os.rename(filename, newfilename)

This code will attempt to convert all filenames to lowercase (even those that are already lowercase), which could translate into a lot of wasted cycles (ie. if all of the files in the directory are already lowercase), but it actually runs faster than trying to determine its case as in the previous code (by using the if statement). Enjoy.

It already does that (almost). Just run the script followed by the directory path and the files in the directory you wish to change.

Example: Let's say you have a folder in you home directory called "folder" in it you have three files: ONE 1.txt, TWO 2.txt, and THREE 3.txt. We'll call your script "myscript", and it is located in your home directory.