Remove trailing space from file and folder names

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove trailing space from file and folder names
# 1  
Old 08-13-2014
Remove trailing space from file and folder names

I have a folder that contains many sub folders and files. This tree has to be backed up to an archive system. According to the tech support, one of the archives is failing to back up due to the possibility of trailing spaces on file and folder names. Therefore, I would like to have a script parse through this directory tree and remove trailing spaces from file and folder names. I would like to avoid replacing spaces with underscores and I would like to avoid removing spaces from the middle of the names.

For example, I have many folders that might be named something like this:

12345 Company Name

So assuming the above folder had a trailing space, I would want to remove JUST the trailing space.

Can I get some assistance with a script that could do that?

Moderator's Comments:
Mod Comment Without CODE tags, we can't tell the difference between one space and multiple spaces or tabs, and we can't see trailing whitespace characters. This post is confusing because there are no trailing spaces on your sample filename.

Last edited by Don Cragun; 08-13-2014 at 02:33 PM.. Reason: Add CODE tags.
# 2  
Old 08-13-2014
Can you please tell us what you have attempted so far?
# 3  
Old 08-13-2014
I'm a mediocre script writer so the only thing I've managed to do so far is use a script I found that replaces all spaces with underscores. So my example folder I listed above ends up looking like this:


I managed to get that script to remove ALL spaces and change the name to this:


But I don't want that either.

Last edited by Don Cragun; 08-13-2014 at 02:33 PM.. Reason: Add CODE tags.
# 4  
Old 08-13-2014
Please use code tags as required by forum rules!

Not sure I understand what you are asking for. A listing of files in a directory with trailing spaces removed? One or many spaces? A script to rename all those files? In subdirectories as well? How far need the required assistance go?

Hint: you can get quite far using bash's parameter expansion (here: Remove matching prefix/suffix pattern):
FN="12345 Company Name          "

echo ">${FN%${FN##*[^ ]}}<"
>12345 Company Name<

echo mv \""$FN"\" \"${FN%${FN##*[^ ]}}\"
mv "12345 Company Name          " "12345 Company Name"

# 5  
Old 08-13-2014
RudiC, thank you for the response. My apologies for forgetting the code. Didn't think it was necessary for just folder name examples.

I want to be able to parse through a folder, find all subdirectories and files under that folder that might have a trailing space and then remove that trailing space. So the script would basically be a rename script.

So far, I managed to successfully use a script that either renames the file or folder via replacing spaces with underscores or removing all of the spaces. I want to rename all found files and folders and only remove any trailing spaces.
# 6  
Old 08-13-2014
How far do you get with the hint I gave?
# 7  
Old 08-13-2014
I think this is working for me

find . -iname "* *" -printf '%p\0' | while IFS= read -r -d '' FN; do
	FNnew=`echo "${FN%${FN##*[^ ]}}"`
	echo "$FN" "$FNnew"
	mv "$FN" "${FN%${FN##*[^ ]}}"

I am running tests on a test folder structure now. The only thing it doesn't seem to do is remove the trailing space on files that have a dot extension.

for example:

this is a test .txt

my script will not remove the space after the word test. But, I believe this will not impact my larger issue and I can live without removing spaces on files like that. I'm more concerned with outside trailing spaces.

Thank you very much for the help. Any other tips are appreciated.

---------- Post updated at 02:27 PM ---------- Previous update was at 02:15 PM ----------

Actually, I have discovered a problem with this. If a folder containing a trailing space is fixed and renamed, any files with trailing spaces found under this folder are not fixed because the folder name is no longer found.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove leading and trailing spaces from a file

Hi, I am trying to remove leading and trailing spaces from a file using awk but somehow I have not been able to do it. Here is the data that I want to trim. 07/12/2017 15:55:00 |entinfdev |AD ping Time ms | .474| 1.41| .581|green |flat... (9 Replies)
Discussion started by: svajhala
9 Replies

2. Shell Programming and Scripting

Remove the leading and trailing date from a CSV file

I'm a newbie to shell scripting. Can anyone help with the below requirement ? The leading and trailing date of a files to be removed. 2017-07-12_gmr_tag_log_20170711.csv 2017-07-12_gmr_call_log_20170711.csv 2017-07-12_gmr_outgoing_log_20170711.csv I'm looking for output like... (7 Replies)
Discussion started by: shivamayam
7 Replies

3. UNIX for Beginners Questions & Answers

Remove or truncate trailing nulls from file

I want to remove from a file the trailing null characters (0x00) and stop doing so when a different byte is found (which should not be deleted), and either put the result to the same file or a different one. Any ideas? (9 Replies)
Discussion started by: Tribe
9 Replies

4. Shell Programming and Scripting

Remove trailing space in Gawk

Hi, I have simply made a shell script to convert *.csv to *.xml file. Xml file is required for input to one tool. But i am getting space after last field. How can i remove it. Shell script is as follows :- if then echo "" echo "Wrong syntax, (6 Replies)
Discussion started by: stillrules
6 Replies

5. Shell Programming and Scripting

Checking Multiple File existance in a UNIX folder(Note: File names are all different)

HI Guys, I have some 8 files with different name and extensions. I need to check if they are present in a specific folder or not and also want that script to show me which all are not present. I can write if condition for each file but from a developer perspective , i feel that is not a good... (3 Replies)
Discussion started by: shankarpanda003
3 Replies

6. Shell Programming and Scripting

Remove trailing space

Hi I am trying to remove trailing space from a string. value=${value%% } It is not working. What might be the issue with the above snippet. (7 Replies)
Discussion started by: munna_dude
7 Replies

7. Shell Programming and Scripting

Renaming File Names in a folder/Dir

Hi Team, I'm new to Unix shell scripting . I've the following requirement A folder contains the list of files with the following format ---------- I have to rename the above file to like below ... (6 Replies)
Discussion started by: smile689
6 Replies

8. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

9. Shell Programming and Scripting

re: removing trailing space from lines

Not sure why this thread was closed without any explanation, but you can do what you're asking with sed 's/]*$//g' < sourceFile > destFile (1 Reply)
Discussion started by: oombera
1 Replies

10. UNIX for Dummies Questions & Answers

Getting all file names in a folder.

Hi All, I need help to create a shell script that does following: - do ls -1 and let all file names in the current folder - do wc for each file and if wc > 0 then send a mail. I am new to unix and do not know how to get filename one by one in a loop, so that wc can be done. I have done rest... (5 Replies)
Discussion started by: adadevil
5 Replies
Login or Register to Ask a Question

Featured Tech Videos