Linux dir with spaces

How do I navigate to folders with spaces in their names? I get «no such file or directory» when I try [duplicate]

I’m completely new and lost in Ubuntu 12.04, I’m having trouble navigating to my Sublime folder which I know is in my /opt folder. Yet I’ve tried to no avail to navigate into it via Terminal window. The directory is colored in blue, no idea what this means. but I am able to access the folder using the GUI explorer.

k@k-Ubuntu:~$ cd /opt k@k-Ubuntu:/opt$ ls Sublime Text 2 k@k-Ubuntu:/opt$ cd Sublime Text 2 bash: cd: Sublime: No such file or directory k@k-Ubuntu:/opt$ cd /Sublime Text 2 bash: cd: /Sublime: No such file or directory 

2 Answers 2

Go inside the /opt directory via terminal and then run below command,

k@k-Ubuntu:~$ cd /opt k@k-Ubuntu:/opt$ cd "Sublime Text 2" 

As the folder you want to connect has spaces in the name, you must surround the name with quotes in order for the Shell to read it correctly (as one name). In other case it will read only the Sublime and this doesn’t exist.

Another way to avoid this issue, is to use Tab Completion. This is a feature that will help you to auto-fill weird names and/or long names. Read here about Tab Completion and learn it. It is very useful(in Ubuntu is pre-installed).

A third way for names with spaces is the backslash \ . Above command with quotes could be

the backslash followed by a space explicitly denotes a space.

A suggestion could be: Do not create folders in Linux with space in name. Prefer something like Sublime-Text-2 or Sublime_Text_2

Источник

How to access files/directories with spaces in the name? [duplicate]

Through terminal I can’t access files or directories with a spaces in their names. The cd command says no such file or directory . Is there any way to do it or should I rename all files with spaces?

Читайте также:  Вход в линукс без пароля и логина

2 Answers 2

To access a directory having space in between the name use \ to access it. You can also use Tab button to auto completion of name.

guru@guru-Aspire-5738:~$ cd /media/Data/My\ Data/ guru@guru-Aspire-5738:/media/Data/My Data$. 

To to use files with spaces you can either use the escape character or youse the double quotes.

\ is called escape character, used to not expansion of space, so now bash read the space as part of file name.

Now to rename files, it’s so easy to rename all files with spaces and replace space with underscore:

for file in * ; do mv "$f" "$" ; done 

look at answer here there is a script to rename all files and dirs recursively.

The script is:(All rights go to its owner)

#!/bin/bash # set -o xtrace # uncomment for debugging declare weirdchars=" &\'" function normalise_and_rename() < declare -a list=("$") for fileordir in "$"; do newname="$]/_>" [[ ! -a "$newname" ]] && \ mv "$fileordir" "$newname" || \ echo "Skipping existing file, $newname." done > declare -a dirs files while IFS= read -r -d '' dir; do dirs+=("$dir") done < <(find -type d -print0 | sort -z) normalise_and_rename dirs[@] while IFS= read -r -d '' file; do files+=("$file") done < <(find -type f -print0 | sort -z) normalise_and_rename files[@] 

Источник

How to Tackle Filenames With Spaces in Linux

Spaces in the file names could be tricky, specially for new Linux users. Learn how to deal with them.

The one thing you'll notice that files in Linux usually do not contain names. Your teacher or colleague use underscore instead of spaces in file and directory names.

It's not that you cannot use spaces in file names in Linux terminal. It's just that it creates additional pain and that's why you should avoid it wherever possible.

Why? Let me show that with examples. You know the generic syntax for Linux commands:

command [options] argument1 argument2

In here, the arguments are separated by spaces. If you try to use filenames with spaces directly, it will be treated as separate arguments rather than just one argument.

Spaces in file name create problem in Linux

In the above screenshot, when I try to use cat agatha books command, it doesn't understand that agatha books is a single argument. It treats agatha and books as different filenames.

Читайте также:  Linux права удаленного доступа

How do you deal with spaces in filename, then? There are two ways:

Wrap the entire filename between quotes:

Escape every space using backslash key:

Tab completion often works with spaces as well. Your terminal may show the file name with space escaped with backslash if you press tab key for the filename.

Read a file with spaces in filename

To use a filename with spaces in it, you can wrap it in quotes like this:

cat "file name with spaces"

You may also escape every space with backslash but it is more work and more confusing than the previous method:

cat file\ name\ with\ spaces

Basically, you put a \ before every space in the filename.

You could also use single quotes instead of double quotes.

cat 'file name with spaces'

Read files with spaces in their names

Single quotes ignore any special characters. Double quotes ignores all except $, back quotes and baclslashes. More on it in some other tutorial.

Create a file with space in filename

Now, you need to type space in terminal to create the filename here. Use backslash or quotes again.

Similar to the previous section, you can create new files with spaces in the filename using quotes:

touch "file name with spaces"
touch file\ name\ with\ spaces

Creating new files with space in their names in Linux

Dealing with space in folder name

You can create a directory with space in its name the same way you create a file.

Now, if you want to switch to this directory, you'll have a path with spaces.

But that should not be a problem for you anymore. To cd into a directory with space, use quotes or backslash again.

Basically, whenever you have to deal with spaces in names, you use quotes or backslash keys.

Suppose you have to copy a file my file from this new dir . Here's what you can do:

Dealing with folders with spaces in their name in Linux

Now it starts to get confusing a bit, right? There are backslashes and forward slashes. It could intimidate a new user or even a seasoned one if there are way too many of those slashes.

It gets even messier when there are backslashes in the filename. Then you'll be seeing double backsplashes.

This is the reason why you should try and avoid using spaces or other special characters in file names. To separate the words in a file name, use underscore.

touch a_very_long_file_name_with_too_many_words

This makes the filenames easier to read and you won't have to make the extra effort to deal with those spaces in the filenames.

Читайте также:  Linux run deb file

Источник

How to Read a Filename with Spaces in Linux

It's not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces.

While it is not recommended to have file names with spaces, let's discuss how to manage filename with spaces in a Linux system.

In this guide, we cover how to create, read and copy a file that has spaces in its filename.

1. Creating file names with spaces

To create files with spaces in file names, run the command as shown

touch'firstname secondname'

For example, to create a file called 'personal docs' use the syntax below

create and view files with spaces

If you want to view such a file with space in the file name, use the same principle of enclosing the file names inside the quotation marks.

2. Read a File with spaces in filename

You can use 'cat' command or open the document using your preferred text editor such as vim, nano or gedit.

Alternatively, you can use the syntax below

cat file\ name\ with\ spaces

Let's add some text to the 'personal docs' file

echo "Hello guys! Welcome to Linux" >> 'personal docs'

To view the file execute the command below

cat file names with spaces

3. Creating directory names with spaces

To create directory names with space in between use the syntax below

mkdir firstname\ secondname

Please note the space after the backslash

For example, to create a directory called 'personal files' run

create directory names with spaces

4. Navigating to a directory with spaces in the directory name

To navigate to a directory with spaces in its directory name, use the syntax below

To navigate to the directory 'personal files' execute the command below

cd into directory with directory name with spaces

5. Copying a directory with spaces in the directory name

To copy a directory with spaces in its directory name to a different location use the syntax below

cp -R directory\ name /destination/path
cp -R 'directory name' /destination/path/

For example to copy 'personal files' to /home/james path execute

copy directory with spaces in directory name

another method to copy directory with spaces in directory name

Conclusion

In this guide, we learned how to manage filename with spaces. Thanks for taking the time to read this article and please leave your comments.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector