How to replace spaces in file names using a bash script
Use rename
(aka prename
) which is a Perl script which may be on your system already. Do it in two steps:
find . -name "* *" -type d | rename 's/ /_/g' # do the directories first
find . -name "* *" -type f | rename 's/ /_/g'
Based on Jürgen's answer and able to handle multiple layers of files and directories in a single bound using the "Revision 1.5 1998/12/18 16:16:31 rmb1" version of /usr/bin/rename
(a Perl script):
find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;
Linux - Replacing spaces in the file names
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
Replace spaces in all files in a directory with underscores
find . -type f -exec sed -i -e 's/ /_/g' {} \;
find
grabs all items in the directory (and subdirectories) that are files, and passes those filenames as arguments to the sed
command using the {} \;
notation. The sed
command it appears you already understand.
if you only want to search the current directory, and ignore subdirectories, you can use
find . -maxdepth 1 -type f -exec sed -i -e 's/ /_/g' {} \;
Script to remove spaces in all files and folders?
Try this
ls | grep " " | while read file_name
do
mv "$file_name" "$(echo $file_name | sed -E 's/ +//g')"
done
sed -E
is so that you can use some simple regex, and / +/
so it can work in case of multiple consecutive spaces such as
. And /g
so it replaces every occurrences such as foo baa .txt
.
Is there an efficient way to replace spaces with _ in filenames? (thousands of files)
for f in "$(find . -name '* *')"; do mv $f $(echo $f | sed 's/\ /_/'); done
will do it. There should be a better way using find's -exec
option as well.
EDIT:
If the function is put in a script, then find can be used directly:
cat <<EOF > space_to_underscore
#!/usr/bin/env bash
mv "$1" "$(sed 's/\ /_/' <(echo "$1"))"
EOF
chmod +x space_to_underscore
find . -name '* *' -exec ./space_to_underscore {} \;
This will be faster than using a for loop.
Eliminate spaces in filename and rename (cywgin)
Solution
Swap $jay
and $jay2
. The mv
command uses the first argument as the source and the second argument as the destination:
mv sourceFile destinationFile
Don't forget to quote, since you have spaces:
mv "$jay" "$jay2"
Alternative
If you have rename
installed, you can replace your script with the following command:
rename 's/ //g' *
The s/ //g
means substitute (s
) space (/ /
) with the empty string (//
) globally (g
).
The wildcard *
specfies the files to be renamed, that is all files in the working directory.
sed replace spaces in directory names
sed
is meant for search and replacement on files and not on directories in Linux/Unix. The -i
flag in sed
is used to make the text replacement on-the-fly on a file, the action simply does not make sense for a directory. You probably meant to change the name of the directory using sed
on the filename and eventually use mv
to rename the actual directory with the replaced string.
But you could just use mv
in the first place with shell native features to replace white space with a -
character.
for directory in **; do
if [[ -d $directory ]] && [[ -w $directory ]]; then
mv -- "$directory" "${directory// /-}"
fi
done
Related Topics
Finding Number Is Even/Odd in Assembly
Assembling 32-Bit Binaries on a 64-Bit System (Gnu Toolchain)
Use Expect in a Bash Script to Provide a Password to an Ssh Command
What's the Magic of "-" (A Dash) in Command-Line Parameters
How to Recursively Grep All Directories and Subdirectories
How to Compile a 32-Bit Binary on a 64-Bit Linux Machine With Gcc/Cmake
Docker Can't Connect to Docker Daemon
Asynchronous Io Io_Submit Latency in Ubuntu Linux
Redirect Stderr/Stdout of a Process After It's Been Started, Using Command Line
Multiline Bash Command in Jenkins Pipeline
Facing an Error "*** Glibc Detected *** Free(): Invalid Next Size (Fast)"
What's the Best Way to Send a Signal to All Members of a Process Group
What Is Double Dot(..) and Single Dot(.) in Linux
Peak Memory Usage of a Linux/Unix Process
Use Sudo With Password as Parameter
How to Set Environment Variable For Everyone Under My Linux System
How to Pipe Initial Input into Process Which Will Then Be Interactive