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
# 15  
Old 08-14-2014
Thank you everyone for your replies and assistance. Here is a code that I ended up with that works quite well. It is a combination of information I learned here and some scripts I found scattered on the net.

Code:
IFS='
'
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`
echo "Max dir depth:" $j
for (( i=0; i<=j ; i++ ));
do
	find . -mindepth $i -maxdepth $i -iname "* *" -printf '%p\0' | while IFS= read -r -d '' FN;
	do
		FNnew=`echo "${FN%${FN##*[^ ]}}"`
		echo "$FN" "$FNnew"
		mv "$FN" "${FN%${FN##*[^ ]}}"
	done
done

It works quite well and I understand what is going on. I do however have some questions about the first line. Could someone brief me on exactly what the
Code:
IFS='
'

line is doing? I have seen that line used in other scripts and I'm not 100% certain what its function is. I believe it has been written differently in other scripts as well. Like this:

Code:
IFS='\n'

# 16  
Old 08-14-2014
Taken from man bash:
Quote:
IFS The Internal Field Separator that is used for word splitting after expansion and to split lines into words with the read
builtin command. The default value is ``<space><tab><newline>''.
The two forms that you post are equivalent, setting IFS to the <LF> char. But it escapes me to what avail in your code snippet...
# 17  
Old 08-14-2014
RudiC, thanks. I took that code from somewhere on the net and just left that line in. I guess in this particular case, it is unnecessary.

---------- Post updated at 01:25 PM ---------- Previous update was at 11:06 AM ----------

Update to my script:

This line did not calculate the tree depth properly:

Code:
j=`find $1 -printf "%d\n" | sort -u | tail -n 1`

I updated it to this and it works

Code:
find . -printf '%d\n' | sort -n  | tail -1

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