Published on

Handling file or directory names with spaces

Authors
  • avatar
    Name
    hwahyeon
    Twitter

When a file or directory name contains spaces, commands that take file paths as arguments (such as pwd, cd, or ls) may not work as expected. For instance, if there is a directory named "hello world", the following commands will result in an error.

cd hello world
ls hello world

This is because the cd and ls commands interpret "hello" and "world" as two separate arguments, leading to a command error. To address this, you can use one of the following methods.

  1. Wrap the path in single('') or double quotes(""):
cd 'hello world'
cd "hello world"
  1. Escape the space with a backslash(\):
cd hello\ world
  1. Just use underscores(_) or hyphens(-) instead of spaces in file or directory names.