I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?
It's not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces. While it is not recommended to have file names with spaces, let's discuss how to manage filename with spaces in a Linux system.
It could intimidate a new user or even a seasoned one if there are way too many of those slashes. It gets even messier when there are backslashes in the filename. Then you'll be seeing double backsplashes. This is the reason why you should try and avoid using spaces or other special characters in file names.
This should do it:
for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With