Dealing with files with spaces in the name

Hello, I'm a computer science major and I'm having problems dealing with file names with spaces in them. Particularly I'm saving a file name in a variable and then using the variable in a compare function i.e.
a='te xt.txt'
b='file2.txt'
cmp $a $b
If anyone could help me with this particular problem I would greatly appreciate it. Thank you.

O i should probably mention i'm running ubuntu 9.04 with the standard terminal

Hi.

As in your example, you quoted a="te xt.txt". Use the same with cmp

cmp "$a" "$b"

Thank you so much. I tried quoting with single quotes, but the double quotes really helped :smiley:

---------- Post updated at 04:18 PM ---------- Previous update was at 04:02 PM ----------

Now I'm having a secondary problem... if the file starts with a "-" cmp treats it as if it was altering the command but really its the subject.
so:
a="-file.txt"
b="file2.txt"
cmp "$a" "$b"

Hi again.

Try:

cmp -- "$a" "$b" 

Or

cmp ./"$a" ./"$b"

Hmm.... i tried that and i don't think it works... its also causing a new problem. I'll post my entire script so it is easier to see what I'm doing.

#!
#
#This script will search out duplicate files and remove duplicates
#
#directory=NULL
#echo "Wht directory would you like to remove duplicates from?"
#read directory
cd ~/Desktop/test_remove
for i in *
do

    for j in *
    do
        if [ "$i" != "$j" ]
        then            
            cmp -s -- "$i" "$j" 
            c=$?
            if [ $c == 0 ]
            then
                if [ "$j" > "$i" ]
                then
                    rm "$j"
                else
                    rm "$i"
                fi 
            fi    
        fi
    done
done
exit 1

The new problem i'm having is some of the duplicates are no longer getting deleted but rather the files are just being emptied. i'm using a folder full of varying text files (some duplicates some not) but some of the duplicates are being returned with the text being missing since i've added the --

Hi.

Please use code tags - it means I can read your code more easily.

The -- you use with cmp applies to other programs too whenever you refer to one of the files

This means rm too.

rm -- "$j"

-- means that no more options follow, when most UNIX commands see this, they stop looking for things like -f -a -b, etc.

You can also use

rm ./"$j"

I'm sorry... I don't know what code tags are.

Tried adding -- before the rm commands but it still has the same problems as before

#!
#
#This script will search out duplicate files and remove duplicates
#
#directory=NULL
#echo "Wht directory would you like to remove duplicates from?"
#read directory
cd ~/Desktop/test_remove
for i in *
do
    for j in *
    do
        if [ "$i" != "$j" ]
        then            
            cmp -s -- "$i" "$j" 
            c=$?
            if [ $c == 0 ]
            then
                if [ "$j" > "$i" ]
                then
                    rm  -- "$j"
                else
                    rm -- "$i"
                fi 
            fi    
        fi
    done
done
exit 1

Hi.

That was interesting!

#This script will search out duplicate files and remove duplicates
#
#directory=NULL
#echo "Wht directory would you like to remove duplicates from?"
#read directory
cd ~/Desktop/test_remove
for i in *
do
  for j in *
  do
    if [ "$i" != "$j" ]
    then
      cmp -s -- "$i" "$j"
      c=$?
      if [ $c == 0 ]
      then
        if [[ "$j" > "$i" ]]
        then
          rm -- "$j" 2> /dev/null
        else
          rm -- "$i" 2> /dev/null
        fi
      fi
    fi
  done
done
exit 1

I got it to work with [[ ]] on the $j > $i if statement.

Not sure I'd use this approach to the problem, but it seems to wok.

Two things to note. It won't work (or will give errors) if you have directories in the test_remove directory; and i and j are expanded in the beginning to the file list so you will try to delete each file twice (which is why I put 2> /dev/null on the rm commands)

Thank you once again. I also will have to deal with directories... but simply ignore them. i already am running cmp -s for silent output... but it still outputs the line "cmp: test: Is a directory" multiple times because of the loop. Do you know anyway to get rid of that output?

Use 2> /dev/null to throw the errors away, as it is like for the rm commands. Or "2> /dev/null 1>&2" to throw everything away

Thank you very much for all the help with my project. I do appreciate your willingness to advise a novice. Have a good day :slight_smile: