Link to home
Create AccountLog in
Avatar of Evan Cutler
Evan CutlerFlag for United States of America

asked on

unix bulk remove spaces from folder names

Greetings,
I am writing a bash script that deals with folders.  Come to find out, that spaces are a natural enemy in my specific problem.

Is there a way to bulk rename all file names, specifically to remove spaces with underscores?

If no spaces, no movement.

Thanks
Evan
SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ThomasMcA2
ThomasMcA2

Or, enclose your variable in quotes so the spaces don't break the code, like this:

cd "$myPath"
mv "$myFile" "$myFolder"
Avatar of Evan Cutler

ASKER

hi skullnobrains...
thank you so much, but I am not getting any response from teh echos.  it just returns back to prompt.

Is it a Unix vs linux thing?  I am on Solaris.
THanks.

For ThomasMcA2, thank you, but I need to load the folder names into a Bash Array, and spaces in a Bash Array is a delimiter....that causes an issue.

Thanks.
it should work fine on solaris as well

btw, i forgot quotes as well (stupidly tested it to replace "a" with "b" in /tmp) but you should have something echoing nevertheless

find /path/to/root/dir -exec sh -c 'test "{}" = `echo {} | sed s/[[:space:]]/_/g` || echo mv "{}" `echo {} | sed s/[[:space:]]/_/g`' \;

did you properly replace the /path/to/root/dir with the required path ?
try the find command only (removing -exec and all that follows) just to make sure the files are there in the first place

post some background such as the command you typed, the contents of the folderr you tested it on, and the output if you want better help

---

btw, i agree that your script should likely be changed to properly allow spaces

bash allows you to quote elements of the array

$ a=("az az" truc)
$ echo $a
az az
$ echo ${a[1]}
truc
$ echo ${a[0]}
az az

since i don't use bash-isms including arrays usually (hardly find them ever usefull : there is always another way and i have a personal grudge agains bash specifically), this should help better than my explanations
http://mywiki.wooledge.org/BashGuide/Arrays
Depending on the number and depth through which you have to search using find | while read a, do
Using the sed or tr can be used to replace spaces with underscore.
Tr / / /_/
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Hi Guys...
ok...

for skullnobrains.  I did the find . as you suggested, and it found all of the folders and subfolders.  I beleive the problem is where the sed s/[[:space:]]/_/g` parts are.

I think it's not finding spaces using [[:space:]].  any ideas?

for Tintin...great idea...but I'm getting a -print0 return failure, so I removed it and tried again...but if the subfolder is in a folder with a space, the same error occurs...

I appreciate it guys...This is awesome stuff.

Evan
The -print0 adds a null string at the end of an entry.

using Tintin's script.  Prepend the mv line with echo
echo mv "$file" "${file// /_}"

IMHO it is better to have the quotes and not needed them.
it is hard to help without copy-pastes of the command you type and their output

as far as i know, the sed in solaris should understand [[:space:]] and the find command should also understand -exec and {}

would you mind pasting this command, change the path and only the path, and paste the output ?

find /path/to/root/dir -exec sh -c 'echo {} ; test "{}" = `echo {} | sed s/[[:space:]]/_/g` || echo mv "{}" `echo {} | sed s/[[:space:]]/_/g`' \;

and check what shell and shell version corresponds to "sh". try with bash instead of sh in case you are using a weird shell

note that i hardly understand why you'd rather change the files than the script
Thanks guys.
Because my folders are always two deep, I used Tintin's solution twice.  it ran perfect.
Thanks.