You are not logged in.
Hi,
I am trying to write a script that will substitute spaces in file names into underscores. mv command does not work, as it expects a directory as the last argument for some reason. I read about rename command and so I tried the following;
rename 's/\ /_/g' ${1}
This produces an error stating it does not have enough arguments for some reason. Upon reading the man page I found out that the correct way of writing the command is the following (which works but not completely);
rename \ _ ${1} # There is a space after backslash.
This works but the problem is it only replaces the first ever blank space in the name of the file. Searching the internet, I only found the first mentioned use of the command.
How do I make it so that the command replaces all of the spaces in the file name?
I like to do things a certain way, even if there's a simpler solution available. (No one will hire me, I know.)
Offline
There are two rename commands. The good one is in the perl-rename package and, appropriately, is `perl-rename 's/\ /_/g' ${1}`, the mediocre one comes from util-linux and is installed by default.
Some linux distributions call it "prename" or "file-rename", and/or offer an option to choose between the two for the /usr/bin/rename file.
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
for target in *, do mv "$target" "${target// /_}"; done
#edit thanks seth
Last edited by jasonwryan (2020-07-31 20:38:35)
Offline
for target in *, do mv "$target" "${target// /_}"; done
Thank you. That worked.
I like to do things a certain way, even if there's a simpler solution available. (No one will hire me, I know.)
Offline