How to remove space character (whitespace) from end of filenames and foldernames?
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?
Solution 1:
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
Solution 2:
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
Solution 3:
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.