[SOLVED] Replace whitespaces with dots "." on all files in folder
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What have you tried so far? The rename command should do the trick. Anyway, there are two versions of the command out there. On my current system (CentOS) it acts only on the first occurrence of the pattern.
In alternative you can try shell's parameter substitution, replacing every occurrence of a space with a dot, e.g.
Code:
for file in *" "*
do
echo mv $file ${file// /.}
done
where the echo statement is only for testing purposes. This does not replace a sequence of two or more spaces (if any) with a single dot, anyway.
Actually, I tried rename, but surprisingly it only replaces the first space with a dot.
I was surprised too, but accordingly to the man page it is the expected behaviour:
Code:
SYNOPSIS
rename from to file...
DESCRIPTION
rename will rename the specified files by replacing
the first occurrence of from in their name by to.
However this applies only on rename as an ELF executable provided by the util-linux package. Instead, on Debian-based systems the rename command is a perl script that accepts a perl expression in place of the two patterns, so that we can use something like:
Code:
rename 's/ +/./g' *" "*
where the g modifier does the trick. Looking at the last post from the OP, it seems the perl version is in action here.
I was surprised too, but accordingly to the man page it is the expected behaviour:
Code:
SYNOPSIS
rename from to file...
DESCRIPTION
rename will rename the specified files by replacing
the first occurrence of from in their name by to.
However this applies only on rename as an ELF executable provided by the util-linux package. Instead, on Debian-based systems the rename command is a perl script that accepts a perl expression in place of the two patterns, so that we can use something like:
Code:
rename 's/ +/./g' *" "*
where the g modifier does the trick. Looking at the last post from the OP, it seems the perl version is in action here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.