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
# 8  
Old 08-13-2014
Assuming you are happy with your code so far...
A longhand line by line removal of the trailing space.
OSX 10.7.5, deafult bash terminal...
Code:
Last login: Wed Aug 13 19:23:39 on ttys000
AMIGA:barrywalker~> text=".txt"
AMIGA:barrywalker~> txt="This is a line with a trailing space "
AMIGA:barrywalker~> txt="${txt% }"
AMIGA:barrywalker~> txt=$txt$text
AMIGA:barrywalker~> echo "$txt"
This is a line with a trailing space.txt
AMIGA:barrywalker~> _

# 9  
Old 08-13-2014
Quote:
Originally Posted by vipertech
...
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.
You need to run find again to get at the new folder name.
# 10  
Old 08-13-2014
So does it make sense then for me to run the script with loop that first goes and changes all of the folder names and then later in the script another loop that then goes through and changes all of the files? Something like this:

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

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

# 11  
Old 08-13-2014
You could do the files first and then the directories, like so:
Code:
{
  find . ! -type d -name "* " ......
  find . -type d -name "* " ......
} |
while  ...
do 
  ....
done

--
@RudiC, vipertech:
Also note that it is best to use quotes around the inner expansion so that if it contains characters that are special to the outer expansion they will not be interpreted as such:

Code:
mv "$FN" "${FN%"${FN##*[^ ]}"}"

This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 08-13-2014
My two loop idea doesn't work anyways because it still will not find sub-folders of sub-folders already renamed.
# 13  
Old 08-13-2014
Try find -depth, which will process folder contents before the folder itself. This should avoid cutting off the branch you are standing on, so to speak.
# 14  
Old 08-13-2014
You could try find with the -depth option
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, Databse_update.sh... (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 ab.name.11.first ab.name.12.second ab.name.13.third ---------- 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