Manage files or directories with spaces in name - Ubuntu

Most users who work with command-line are accustomed to put either hyphen or underscore in place of spaces in file and directory names. They may also completely do away with spaces by not placing any character between words. Following are the common examples –

helloworld.py
hello-world.py
hello_world.py

But, if you have been working through GUI then you would have a habit of using spaces. That way, things look arranged. Though both the approaches are absolutely fine, but while accessing them through command-line we may face a bit of trouble with files or directories which have spaces in between their names.

Manage files or directories with spaces in names – Ubuntu

There are two methods which can be utilized –

  1. either use backslash or,
  2. put the file or directory name in quotes.

Method I. Starting with, how to use backslash? Let’s say we have a file –

hello world.py

And, we want to move the file to Downloads/ directory (). So, open a terminal and issue the following –

mv hello\ world.py /home/$USER/Downloads/

Overall, we use backslash to escape a special character.

Method II. The other method is to put the file or directory names inside quotes. So, continuing with the above hello world.py example. To move the file –

mv "hello world.py"  /home/$USER/Downloads/

These two methods can be used if we want to copy, rename files or directories. Even with cd command-line utility as well.

In conclusion, two methods we have covered here – one was with backslash and other was through quotes to manage files or directories with spaces in names.

Additional Info –

There is one more method (tab space) which can be of use if we are not working with a number of files or directories. So, just type the first word or letter of a file and hit the tab space. What you would notice is, it would put a backslash in the file or directory name on its own.

Similar Posts