How to Rename & Move Files with Spaces in Name at Command Line
If you’re new to the Mac command line you may have come across a situation where you’re trying to interact with a file that has spaces in the name, for example “This File.txt” but as you probably discovered, you can’t simply type the file name if there are spaces within the file name, or the command to move, rename, copy, or otherwise interact with the file will fail to execute.
There are a few ways to interact with files from Terminal that have spaces in the file names, but one is arguably easier to remember and use than the other.
We’re focusing on the Mac here, but this works with basically any unix command line you’ll encounter, whether it’s MacOS, Linux, Windows WSL, etc.
Interact with File with Spaces in Name in Command Line with Quotation Marks
The simplest way to interact with a file that has spaces in the file name is by using quotation marks.
For example, if we’re trying to move a file that is called “Sample File.pdf” the command to use would look like the following:
mv "Sample File.pdf" ~/Path/To/Destination/
Or to rename a the “Sample File.pdf” would look like the following:
mv "Sample File.pdf" "Sample File 2.pdf"
Modify Files with Spaces in Name from Terminal by Escaping
You can also escape the spaces with a backslash, but for most users this is not as simple as using quotation marks. This would look like the following:
mv Sample\ File.pdf ~/Path/To/Destination/
Notice how the \ is located before the space in the file name.
Again, for most users, using quotation marks is the simplest and most direct way, but you’re free to use the escape slash method too.
My preferred method is to simply draw the fileName from the Finder into Terminal’s Command Line. It arrives there with its full Path Name and backslashed special characters – like Spaces (any Blanks).