Linux Move Files With Spaces: Easy Terminal Fix
In this article
To move a file with spaces in Linux, you must quote the filename or escape the spaces with a backslash, for example mv "my file.txt" /target/ or mv my\ file.txt /target/. Without this, the shell treats each word as a separate argument and the command fails.
Why Spaces Break Commands
In Linux, the shell (like Bash) splits command-line input on spaces by default. When you type mv my file.txt /target/, the shell sees three separate arguments: mv, my, and file.txt. The mv command then tries to move two files (my and file.txt) into /target/, which usually results in an error like mv: cannot stat 'my': No such file or directory. Quoting or escaping tells the shell to treat the space as part of the filename, not as a separator.
Three Ways to Handle Spaces
You have three reliable options, each useful in different situations:
- Double quotes:
mv "my file.txt" /target/— the most common and readable method. Works for most filenames, including those with special characters. - Single quotes:
mv 'my file.txt' /target/— similar to double quotes, but prevents any variable expansion inside the quotes (e.g.,$HOMEstays literal). - Backslash escape:
mv my\ file.txt /target/— escapes just the space, so the shell treats it as part of the word. Useful when you want to avoid quotes in scripts or when you're already inside a quoted context.
All three work identically for filenames with spaces. Choose based on what else is in the filename (e.g., if it contains a dollar sign, single quotes are safer).
Moving Multiple Files With Spaces
When moving several files that contain spaces, you can quote each one individually: mv "file one.txt" "file two.txt" /target/. For many files, use a wildcard with quotes around the pattern: mv *.txt /target/ — the shell expands the wildcard before passing arguments, and each matched filename is passed as a separate argument, so spaces are preserved. If you need to move files that match a pattern but have spaces, the wildcard approach is clean and avoids manual quoting.
Using Tab Completion to Avoid Typing
Instead of typing quotes manually, use tab completion. Type the first few characters of the filename (e.g., mv my) and press Tab. The shell automatically completes the filename and escapes spaces with backslashes, giving you mv my\ file.txt. This is faster and reduces typos. Tab completion works in most Linux terminals and is the recommended way to handle spaces interactively.
Common Errors and How to Fix Them
Here are typical mistakes and their fixes:
- Error:
mv: cannot stat 'my': No such file or directory— you forgot to quote. Add quotes or backslashes. - Error:
mv: target 'file.txt' is not a directory— the shell split the name, somvthinks you're moving two files. Quote the source. - Filename has spaces and a trailing space — rare but possible. Use tab completion to include it, or quote the entire name including the trailing space.
- Using variables: If you store a filename in a variable, always quote the variable:
file="my file.txt"; mv "$file" /target/. Without quotes, the variable's value is split on spaces.
If a command fails, check the exact error message. It usually tells you which part of the filename the shell misinterpreted.
Moving Files With Spaces in Graphical File Managers
In GUI file managers like Nautilus (GNOME), Dolphin (KDE), or Thunar (XFCE), spaces are handled automatically — you just select the file and drag or use the context menu. No quoting is needed. The issue only arises when using the command line or scripts. If you're automating file moves in a script, always quote your variables and filenames to avoid breakage.
Frequently Asked Questions
How do I move a file with spaces to a directory that also has spaces?
Quote both the source and destination. For example: mv "my file.txt" "/home/user/My Documents/". The shell treats each quoted string as a single argument, so spaces in both paths are handled correctly.
Can I use wildcards with spaces?
Yes. The shell expands wildcards before passing arguments. For example, mv *.txt /target/ moves all .txt files, even if they contain spaces, because each matched filename is passed as a separate argument. Just don't put quotes around the wildcard itself, or it will be treated literally.
What's the difference between single and double quotes here?
Double quotes allow variable expansion (e.g., "$HOME/file"), while single quotes treat everything literally. For filenames, single quotes are safer if the name contains $, backticks, or other special characters you want to keep as-is. For most filenames with spaces, either works.
Why does tab completion add a backslash instead of quotes?
The shell's default completion uses backslash escapes because they are unambiguous and can be used in any context. It's the same as typing the backslash manually. You can still use quotes if you prefer; both achieve the same result.
How do I move a file with spaces in a script?
Always quote variables and filenames in scripts. For example: file="my file.txt"
mv "$file" /target/. This prevents the shell from splitting the variable's value on spaces. Never write mv $file /target/ without quotes.