I'm using the Perl rename tool to write a bash script that looks for any files in a path that has spaces in it (It does other stuff but trying to keep this simple). I'm using the -c switch to transform to lower case and a regular expression to change all spaces to dashes. The command completes with no errors.
rename -f -X -c 's/[ ]+/-/g' /Volumes/data/Users-Links/adrz/test-site/with\ spaces/*
The above transforms the files to lower case but does not replace spaces with dashes. I then tried with the -e switch in front of the expression.
rename -f -X -c -e 's/[ ]/-/g' /Volumes/data/Users-Links/adrz/test-site/with\ spaces/*
and get ...
Can't rename '/Volumes/data/Users-Links/adrz/test-site/with spaces/hello world.txt' to '/volumes/data/users-links/adrz/test-site/with-spaces/hello world.txt': No such file or directory
It seems to be acting on the directory name that has spaces as well as the files inside the directory. To note, If the path does not have spaces in it, it works fine.
I've tried:
I've been searching for weeks and I cannot work it out. Is this a bug in the tool or my mind?
Thanks.
Unless you're also trying to change the directory, you could use any of the following:
Run rename
from the directory in which the files reside.
sh -c 'cd /Volumes/.../with\ spaces; rename -f -X -c -e "s/[ ]+/-/g" *'
Only match spaces that aren't followed by a slash (/
).
rename -f -X -c -e 's{[ ]+(?!.*/)}{-}sg' /Volumes/.../with\ spaces/*
Break the path into its components and only operate on the file name portion.
rename -f -X -c -e '
use Basename qw( dirname basename );
$_ = dirname($_) . "/" . basename($_) =~ s/[ ]+/-/gr;
' /Volumes/.../with\ spaces/*
This is overkill in this situation, but it could be useful in situations where a simple modification (as in #2) isn't possible.