How to cd to a folder with spaces in its name

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to browse to a folder that has spaces on its name.

For example, let's say you have a folder on your computer that has a very long name, and you need to browse to this folder from your terminal without having to change its name.  And when you try to use the cd command and the folder name, you get the error " too many arguments" in the terminal, this happens because cd doesn't know if you are trying to browse to a folder or pass an argument.

Create a folder with spaces in name

To create a folder with spaces in name, you can use the mkdir command and the folder name in quotes, for example

mkdir 'Folder with spaces in name'
💡
The command above, will create a folder called "Folder with spaces in name". Notice how the folder name is in the quotes, the difference between using quotes and not, is that you use quotes only when the folder has spaces in name.

Note: If you run the command above without quotes, mkdir will create 5 folders, so make sure to use quotes only when the folder that you want to create has spaces in name.

cd to a folder with spaces in name

Just like you use quotes to create a folder, you need to use quotes when also browsing to this folder. For example, to cd to this folder "Folder with spaces in name" you need to run

cd 'Folder with spaces in name'

cd to absolute path

Above we showed an example how you could browse to a folder with spaces in name, but how do you browse to an absolute path when the folder contains spaces in name? For example, the folder that we created above is located on /home/owlhowto/,  to browse to the folder you need to specify the full path while also using quotes for the folder

cd /home/owlhowto/'Folder with spaces in name'

If you can't remember the folder name, you can use the ls command in the same path where the folder is located so you can copy its name without having to type it manually

ls /home/owlhowto

Alternatively, you can cd into the directory first

cd /home/owlhowto

And then run the ls command

ls

Delete a folder with spaces in name

If you want to delete a folder that has spaces in name, you can do so by using the rm command

rm -r 'Folder with spaces in name'

💡
Note : rm -r command removes the folder and all the content located in this folder. Alternatively, you can use the rmdir command which is a safer command to use, because it would delete only empty directories. 

Copy a folder with spaces in name

If you want to copy a folder with spaces in name to another directory on your computer, use the command below

cp -r 'Folder with spaces in name' /path-destination
💡
This command will copy the folder Folder with spaces in name to the destination path. Make sure to replace Folder with spaces in name with your folder name, and /path-destination with your path. 

For example, lets say you want to copy this folder to your Desktop, to do so you need to set /Desktop as the destination path

sudo cp -r 'Folder with spaces in name' /Desktop

Note: You need root permission in order to be able copy files

Move a folder with spaces in name

To move a folder with space in name, use the following command

sudo mv 'Folder with spaces in name' /destination-path

Conclusion

In this tutorial, you learned how to cd into a folder which has spaces in name. We also explained, how you can create, move, copy and delete a folder with space in name.