4

Problem with mac is that it allows space character at end of file or folder. If this file or folder with space character in end is beeing copied to FTP using Filezilla or Dropbox then mac makes folders with "Foldername_WhiteSpaceConflict" To get rid of this problem all files and folders with space character needs to be renamed without space character. What solution could be the best using terminal or some program?

  • Automator might be useful, if you're not a programmer. – james.garriss Feb 23 '15 at 20:55
4

Here's the script to find and remove trailing space from files and dirs:

#!/bin/bash                                                                                                                                                                                                        

IFS=$'\n'
for file in $(find -d . -name "* ")
do
  target_name=$(echo "$file" | sed 's/[ \t]*$//')
  if [ "$file" != "$target_name" ]; then
      if [ -e "$target_name" ]; then
          echo "WARNING: $target_name already exists, file not renamed"
      else
          echo "Move $file to $target_name"
          mv "$file" "$target_name"
      fi
  fi
done
  • 1
    What happens if you have a file ending in space inn a dir ending in space - won't the file have a wrong name after the dir has been renamed – user151019 Feb 23 '15 at 19:05
  • Good one, updated my answer :) – Mateusz Szlosek Feb 23 '15 at 19:10
  • Position of -d is important here, also I've added a check to prevent mv from overwriting existing files. – nohillside Feb 23 '15 at 19:16
1

I had to fix the script a bit more. For files or folders ending in the letter 't', it would also delete that letter. And the call to check if the file exists wasn't working properly so changed "target_name" to $target_name and it works now.

#!/bin/bash                                                                                                                                                                                                        

IFS=$'\n'
for file in $(find -d . -name "* ")
do
  target_name=$(echo "$file" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
  if [ "$file" != "$target_name" ]; then
      if [ -e $target_name ]; then
          echo "WARNING: $target_name already exists, file not renamed"
      else
          echo "Move $file to $target_name"
          mv "$file" "$target_name"
      fi
  fi
done
  • This worked perfectly for me! thanks! – Wes Larson Jun 28 at 23:56
1

There is an application for Mac called 'Name Mangler' , this will do everything you could every want to do to a file/folder name, and you can even create your own code and use it in the app. There is a free trial which gives you 4 unlimited attempts, and you can revert the names back if you make a mistake.

0

Something like this should work:

ruby -e "Dir.glob('* ').each { |name| File.rename name, name.chomp(' ') }"

This will remove trailing spaces from every file/dir in the pwd.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .