Remove leading and trailing whitespace directories

The problem is that Mac OS X lets folders get names like " Foo Bar /".

This is what I could fix on my own and it works for most of the problems.

for d in */ ; do echo "$d" | xargs done

Result: "Foo Bar /".

The only problem it leaves that last space between the directory name and the slash.

Can someone help get rid of that last space?

-------------Problems Reply------------

Try this for a bash-only approach:

dir="some dir /"
fixed=${dir/ \///}
echo $fixed
some dir/

Bash string manipulation:

SUBSTRING REMOVAL BEGINNING OF STRING:

${string#substring} - remove shortest substring match from beginning

${string##substring} - remove longest substring match from beginning

SUBSTRING REMOVAL END OF STRING:

${string%substring} - remove shortest matching substring from tail

${string%%substring} - remove longest matching substring from tail

SUBSTRING REPLACEMENT:

${string/substring/replacement} - replace first match of substring

${string//substring/greplacement} - replace all matches of substring

${string/#substring/replacement} - replace substring at front of string

${string/%substring/replacement} - replace substring at tail of string

The problem is that Mac OS X lets folders get names like " Foo Bar /"

This is a Unix/BASH issue too. All characters except NUL and forward slash are valid characters in file names. This includes control characters like backspace and UTF8 characters.

You can use ${WORD%%filter} and ${WORD##filter} to help remove the extra spaces on the front and end of files. You might also want to substitute white space with an underscore character. Unix shell scripts usually work better if file names don't contain white space:

for file in *
do
new_file=$(tr -s " "<<<"$file")
mv "$file" "$new_file"
done

The tr -s " " uses the tr command to squeeze spaces out of the file name. This also squeezes beginning and ending spaces on Mac OS X too. This doesn't remove tab characters or NLs (although they could be added to the string.

The for file in * does assign the file names correctly to $file. The <<< is a Bashism to allow you to redirect a string as STDIN. This is good because tr only works with STDIN. The $(...) tells Bash to take the output of that command and to interpolate it into the command line.

This is a fairly simply script and may choke on other file names. I suggest you try it like this:

for file in *
do
new_file=$(tr -s " "<<<"$file")
echo "mv '$file' '$new_file' >> output.txt
done

Then, you can examine output.txt to verify that the mv command will work. Once you've verifies output.txt, you can use it as a bash script:

$ bash output.txt

That will then run the mv commands you've saved in output.txt.

What about using sed like this:

name=" Foo Bar /"
echo "$name" | sed 's/^\s*\(.*\)\s*$/\1/g' | tr -s ' '

The sed expression removes all spaces before and after your name, while tr squeezes them to a maximum of one.

Category:bash Views:0 Time:2017-09-11

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.193 (s). 10 q(s)