I have a load of files(mp3, wav, txt, doc) that have been created in MS Windows and they have spaces in their names. eg The file of whoever.doc
I would like to rename them all at once, replacing the space with an underscore or dot.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI have a load of files(mp3, wav, txt, doc) that have been created in MS Windows and they have spaces in their names. eg The file of whoever.doc
I would like to rename them all at once, replacing the space with an underscore or dot.
The shell can do this pretty easily (here assuming ksh93, zsh, bash, mksh, yash or (some builds of) busybox sh for the ${var//pattern/replacement}
operator):
for file in *.doc *.mp3 *.wav *.txt
do
mv -- "$file" "${file// /_}"
done
Change the *.doc
... glob to match whatever files you're interested in renaming.
To rename all of the files in the current directory that currently have spaces in their filenames:
for file in *' '*
do
mv -- "$file" "${file// /_}"
done
You might also consider adding a "clobber" check:
for file in *' '*
do
if [ -e "${file// /_}" ]
then
printf >&2 '%s\n' "Warning, skipping $file as the renamed version already exists"
continue
fi
mv -- "$file" "${file// /_}"
done
Or use mv
's -i
option to prompt the user before overriding a file.
Aside from what's been mentioned, the program detox might be of some interest. It's designed specifically for doing this type of thing, and handles lots of stuff other than just spaces. Provided you don't have any diacritical marks, parentheses, or other odd stuff in the filenames, the following should do exactly what you want if run at the top of the directory tree:
detox -r .
Note that that will also fix the names of directories, so do not run that on the Users
directory from a Windows system, otherwise you're liable to break things (Windows requires specific names for certain folders in the user directories, and this will change those folders' names).
sudo apt install detox
was all it took to get started.
Jul 17, 2022 at 10:12
You can use sed
, e.g.:
mv -i "${original_file}" "$(echo "${original_file}" | sed 's/ /_/g')"
Here's an example.
Create a file with spaces in its name:
touch "/tmp/test file with spaces"
Rename the file:
mv -i \
"/tmp/test file with spaces" \
"$(echo "/tmp/test file with spaces" | sed 's/ /_/g')"
Here is the new name of the file:
test_file_with_spaces
You could do the same thing with tr
instead, i.e.:
mv -i "${original_file}" "$(echo "${original_file}" | tr ' ' _)"
Or using Bash substring replacement:
mv -i "${original_file}" "${original_file// /_}"
Or using the rename
command:
rename "s/ /_/g" "${original_file}"
rename s/\ /_/ *
I can’t remember right now whether the white space needs escaping or not with rename.
(Or swap *
for whatever extension.)
rename 's/ /_/g' *
myself. Or even rename 'y/ /_/' *
Apr 3, 2019 at 17:54
g
to the end of the string. This is what worked for me: rename 's/\ /_/g' *
The correct answer is given above by Austin Hemmelgarn, use:
detox -r .
Before running the detox
command preview changes to be made with the option -n
like this:
detox -n -r .
Running the detox
command from the home directory as the starting-point, and excluding all hidden files and directories, and excluding particular directories such as a timeshift
directory for example, use the form:
find ~ -path '*/\.*' -prune -o -path */timeshift/* -prune -o -exec detox -n {} \;
To actually run the above command remove the -n
Replace all spaces with underscores in mp3
, txt
, wav
, and doc
filenames using qmv and vim:
qmv -f do --editor="vim -c '%s/ /_/g' -c 'wq'" \
*.mp3 *.txt *.wav *.doc
qmv is a part of renameutils.
if rename s/\ /_/ *
not work use rename -v ' ' '_' *