Sitemap
Dhiraj Chaurasiya

Dhiraj Chaurasiya

Flutter and DevOps Enthusiast

Space concern in Linux File System

3 min readDec 21, 2025

--

From switching windows to Linux distros there are several thing you get to know. One of them which I faced often on switching to ubuntu was “Space in file names” is a big concern for linux based system even when you have NTFS/exFAT format for media drives. This blog shares what I got to know and the possible way I tried to solve the issue and somehow helped too.

Press enter or click to view image in full size

Spaces in File Names

  • Spaces in file names are allowed but inconvenient. Why? Because the shell treats spaces as separators, so you must quote or escape them as shown below:
cd "my file name"
cd my\ file\ name

Hence this is what chatgpt said to me: Spaces are a usability problem — not a technical limitation (except on NTFS/exFAT like your case).

| Since I had recently switched to ubuntu with all my files and folders same as earlier in NTFS drive, so it was surely not possible trying to rename each files and folders of the drive. So why not trying to automate this task of renaming them? So thought of writing and running a script to change the filename containing spaces with underscore “_”.

Get Dhiraj Chaurasiya’s stories in your inbox

Join Medium for free to get updates from this writer.

so got this below script to automate:

find . -depth -name "* *" -exec sh -c '
for filepath; do
newpath=$(echo "$filepath" | tr " " "_")
mv "$filepath" "$newpath"
done
' sh {} +

But again got a good catch, I got to realize that this script would only solve the partial problem of the issue. What about the files which look identical after removing spaces? Well in that case the above script will keep both of the file with same name as demonstrated below:

# Original
"My File.txt"
"My File.txt"

# Conversion
"My_File.txt"
"My_File.txt"

Again I thought of improving the script solving the above issue. Let’s try the windows style i.e. the files with same name would have the file name followed by a number as shown below:

"file one .txt" 
"file one.txt"

To:
file_one.txt
file_one1.txt

A suggested command to searching the files with spaces in their names:

find . -depth -name "* *" -exec echo mv "{}" "$(echo {} | tr ' ' '_')" \;

Hence the updated script to rename the files containing spaces would look like:

#!/bin/bash

find . -depth -name "* *" | while read path; do
dir=$(dirname "$path")
base=$(basename "$path")

# Replace spaces with underscores
newbase=$(echo "$base" | tr -s ' ' '_' | tr ' ' '_')
newpath="$dir/$newbase"

# If name exists, append numbers
if [ -e "$newpath" ]; then
counter=1
while [ -e "${newpath%.*}$counter.${newpath##*.}" ]; do
counter=$((counter + 1))
done
newpath="${newpath%.*}$counter.${newpath##*.}"
fi

mv "$path" "$newpath"
done

Conclusion

That was something new I got to learn since switching to ubuntu. Hope this might help you and put a satisfaction smile on your face too if you’re having such similar experience. Well, thank you for reading. (Poor at closing a blog :).

--

--