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?

2

5 Answers 5

5

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
3
  • 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
    – mmmmmm
    Commented Feb 23, 2015 at 19:05
  • Good one, updated my answer :) Commented Feb 23, 2015 at 19:10
  • Position of -d is important here, also I've added a check to prevent mv from overwriting existing files.
    – nohillside
    Commented Feb 23, 2015 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
1
  • This worked perfectly for me! thanks!
    – Wes Larson
    Commented Jun 28, 2019 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.

0

Devin Stagner 2016's answer works perfectly, but I have updated it on 2022-08-20 because -d has been deprecated (now -depth is used), also that switch needs to go after the path (.) being searched. Added [ ] to enclose $file and $target_name when displaying the information through the echo command, so that it's more obvious what was removed from the filename.

#!/bin/bash

IFS=$'\n'
for file in $(find . -depth -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
0

You must log in to answer this question.

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