LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 08-15-2009, 02:36 AM   #1
venkat_eg
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
same filenames in a directory with different case


sir, my directory having files with same names but letter case is different. when i copy the files, i want to over write the files already existing with the same name. here i want to ignore the case sensitive of the letter.
 
Old 08-15-2009, 04:53 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Hello Venkat

On *n*x computers, unlike Windows, file names which differ in case are different; foo, Foo, fOO etc. are different mames. So copying Foo into a directory will not replace foo.

If it's possible, the easiest for you would be to change all the filenames to the same case.

Here's a script from the Advanced Bash Scripting Guide that says it will do it
Code:
#!/bin/bash
#
#  Changes every filename in working directory to all lowercase.
#
#  Inspired by a script of John Dubois,
#+ which was translated into Bash by Chet Ramey,
#+ and considerably simplified by the author of the ABS Guide.


for filename in *                # Traverse all files in directory.
do
   fname=`basename $filename`
   n=`echo $fname | tr A-Z a-z`  # Change name to lowercase.
   if [ "$fname" != "$n" ]       # Rename only files not already lowercase.
   then
     mv $fname $n
   fi  
done   

exit $?
I'm a little worried by some aspects of this script like it renames directories as well as files (maybe that's OK) and if you start with Foo, FOO and foo then you will end up with only one of them or an error depending on how your mv works.

For the script-sperts: I don't think fname=`basename $filename` does anything useful, it's functionally equivalent to fname=filename.

Working on a script and will post later ...

Edit:

Oops! The Advanced Bash Scripting Guide already noted defects in the above scripts and gives this improvement
Code:
# The above script will not work on filenames containing blanks or newlines.
# Stephane Chazelas therefore suggests the following alternative:


for filename in *    # Not necessary to use basename,
                     # since "*" won't return any file containing "/".
do n=`echo "$filename/" | tr '[:upper:]' '[:lower:]'`
#                             POSIX char set notation.
#                    Slash added so that trailing newlines are not
#                    removed by command substitution.
   # Variable substitution:
   n=${n%/}          # Removes trailing slash, added above, from filename.
   [[ $filename == $n ]] || mv "$filename" "$n"
                     # Checks if filename already lowercase.
done

exit $?
If you start with Foo, FOO and foo then this improved script still means you will end up with only one of them or an error depending on how your mv works.

Best

Charles

Last edited by catkin; 08-15-2009 at 04:58 AM.
 
Old 08-15-2009, 05:16 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
This one prints error messages when foo and FOO both exist in the current directory
Code:
#!/bin/bash

# Rename all files in current directory to lowercase names.
# Assumes no control characters in the file names.
# OK with whitespace in the file names.

shopt -s extglob
shopt -s nocaseglob

for file in *
do
	if [[ "$(echo "$file"?([[:ctrl:]]))" = "$file" ]];then
		newfile="$( echo -n "$file" | /usr/bin/tr '[:upper:]' '[:lower:]' )"
		if [[ "$newfile" != "$file" ]]; then
			echo /bin/mv "$file" "$newfile"
		fi
	else
		echo "These files generate duplicate file names when folded to lower case: " $file?([[:ctrl:]])
	fi
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script for converting filenames to proper case? Yalla-One Programming 9 02-03-2010 03:37 AM
script to list the filenames which are in lower case naveensankineni Programming 2 03-12-2008 08:09 AM
replacing all spaces in filenames of a directory merlin23 Linux - Newbie 1 01-11-2005 08:52 AM
Making all the filenames in a directory lowercase? minm Linux - Newbie 4 12-24-2004 01:50 AM
convert filenames case griv Linux - General 6 01-18-2002 12:10 AM


All times are GMT -5. The time now is 02:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration