Enter File or Directory with Space & Special Character in its Name?

Everyone loves to assign fancy names to their files or directory, including special characters, numbers, and spaces. Special characters and numbers are good to have in the name of the files or directories even though they are not recommended.

However, having space in a file or directory name is intriguing. The cd command is usually used to enter inside the directory and does not know what to do with the name containing the spaces and special characters.

It considers the word before and after space and special character as two different arguments or possibilities to solve it. For example, I have a directory named TREND OCEANS in my current path.

trendoceans@linux:~$ ls
'TREND OCEANS'

Now, as shown below, if I try to enter the directory, it will throw a “bash: cd: too many arguments” error.

trendoceans@linux:~$ cd TREND OCEANS
bash: cd: too many arguments

The cd command does not know what you want to do, but there are two different possibilities to solve this issue.

Mask the Spaces and all Other Special Characters

If you mask the spaces and special characters, then the terminal understands that you mean the space as a character and not as a separator or keyword.

Example 1:

To mask the TREND OCEANS directory, place “\” backslash before space to make the terminal understand it as a character, not a space.

trendoceans@linux:~$ cd TREND\ OCEANS
trendoceans@linux:~/TREND OCEANS$

Example 2:

To mask the TREND, OCEANS that includes special character as well as space, place “\” backslash before special character and space to make the terminal understand both as a character, not a special character or space as shown below.

trendoceans@linux:~$ cd TREND\,\ OCEANS
trendoceans@linux:~/TREND, OCEANS$

Place Quotes around the Name of Files or Directories

Contain placed inside a single or double quote considered as part of the container. You can use quotes around your file or directory name, as shown below.

Example 1:

Wrapping the directory or file name includes the space within its name, as shown below.

trendoceans@linux:~$ cd 'TREND OCEANS'
trendoceans@linux:~/TREND OCEANS$

Example 2:

Wrapping the directory or file name includes the special character and space within its name, as shown below.

trendoceans@linux:~$ cd 'TREND, OCEANS'
trendoceans@linux:~/TREND, OCEANS$

That’s all for now. If you have any issues regarding the Linux command line, type them in the comment section.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments