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' {} \;
How to replace double spaces with one space in filenames (also subdirectories) (CloudLinux Server release 6.10)
find -iname \*.* | rename -v "s/\s{2}/ /g"
This is the final command which helped me out. I used perl rename, see answer by Gilles
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
Remove whitespaces from filenames in Linux
You could do something like this:
IFS="\n"
for file in *.jpg;
do
mv "$file" "${file//[[:space:]]}"
done
Replace white space with underscore and make lower case - file name
rename 's/ +\././; y/A-Z /a-z_/'
Or, combined with find
:
find /temp/ -depth -name "* *" -exec rename 's/ +\././; y/A-Z /a-z_/' {} +
To target only files, not directories, add -type f
:
find /temp/ -depth -name "* *" -type f -exec rename 's/ +\././; y/A-Z /a-z_/' {} +
Shortening the names
Would it be possible to rename the file with the last three characters
of the original file for example from big Dog.txt to dog.txt?
Yes. Use this rename
command:
rename 's/ +\././; y/A-Z /a-z_/; s/[^.]*([^.]{3})/$1/'
Replace parentheses and spaces in filenames with underscore
You can use:
find /tmp/ -depth -name "*[ ()]*" -execdir rename 's/[ )]/_/g; s/\(//g' "{}" \;
Rename files with spaces to underscore in all subcategories
Thanks to @Barmar. I used command from answer https://unix.stackexchange.com/questions/223182/how-to-replace-spaces-in-all-file-names-with-underscore-in-linux-using-shell-scr/223185#223185
Just make sure its in two lines
Related Topics
Graphical Diff Programs for Linux
Setting Environment Variable Globally Without Restarting Ubuntu
Get Current Time in Hours and Minutes
Run a Command at a Specific Time
Docker Load and Save: "Archive/Tar: Invalid Tar Header"
Jmeter - Loopback Address Error When Launching Jmeter-Server on Linux
Can't Close a Scpi(Telnet) Session with Echo "^]" When I Use It in a Script
Awk - Count Each Unique Value and Match Values Between Two Files
How to Measure Separate CPU Core Usage for a Process
Best Practices for Git Repositories on Open Source Projects
How to Convert an Ssl Certificate in Linux
Linux: How to Know the Module That Exports a Device Node
Ubuntu: Using Curl to Download an Image