LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   same filenames in a directory with different case (http://www.linuxquestions.org/questions/linux-general-1/same-filenames-in-a-directory-with-different-case-747704/)

venkat_eg 08-15-2009 01:36 AM

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.

catkin 08-15-2009 03:53 AM

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

catkin 08-15-2009 04:16 AM

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



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