LinuxQuestions.org
Visit Jeremy's Blog.
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, 01: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, 03:53 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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 03:58 AM.
 
Old 08-15-2009, 04:16 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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 02:37 AM
script to list the filenames which are in lower case naveensankineni Programming 2 03-12-2008 07:09 AM
replacing all spaces in filenames of a directory merlin23 Linux - Newbie 1 01-11-2005 07:52 AM
Making all the filenames in a directory lowercase? minm Linux - Newbie 4 12-24-2004 12:50 AM
convert filenames case griv Linux - General 6 01-17-2002 11:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration