Linux file names with spaces

How to output only file names (with spaces) in ls -Al?

then output only files and directories without spaces. If file or directory have spaces like «personal domain» it will be only word «personal». I need very simple solution. Maybe there is better solution than awk.

If you need something that will work in any *nix try avoiding using ls -Al | while . A simple and more reliable way is for string in *; do echo «$string»; done .

Why do you need ls ? Parsing ls and «work in all *nix and will not break if something happened» do not go well together.

10 Answers 10

You really should not parse the output of ls . If this is a homework assignment and you are required to, your professor does not know what they’re talking about. Why don’t you do something like this:

The good.

for n in *; do printf '%s\n' "$n"; done 

. the bad.

If you really really want to use ls , you can make it a little bit more robust by doing something like this:

. and the ugly

If you insist on doing it the wrong, dangerous way and just have to use a while loop, do this:

ls -Al | while IFS= read -r string; do echo "$string" | awk -F':1* ' '/:/'; done 

Seriously though, just don’t.

drwx—— 2 s162103 studs 3 oct. 9 2012 .ssh drwxr-xr-x 3 s162103 studs 6 oct. 25 09:02 .subversion drwxrwxrwt 3 s162103 studs 3 nov. 15 2012 .TempoItems sometimes i’ve got date without time, only year and gawk return empty

@AlexZern why do you need the -l switch anyway? If all you want is the file names, just run ls -A . Using -l just makes your life harder since you now have to parse the output.

because I need additional information like access, date, owner,etc. And I wrote a huge script ,which works well, except print file and dir names with spaces.

OK then. What we have here is an XY problem. ls is just not the best way of getting what you need. Why don’t you post a new question explaining the information you want to collect and we can suggest ways of doing it. Alternatively, you can use your script as is, and use one of the suggestions above to print the name only. But seriously, don’t parse ls , just tell us exactly what you are trying to do in a new question.

Is there some reason that ls -A1 * won’t work?

$ touch file1 file2 file\ with\ spaces $ ls -Al total 0 -rw-r--r-- 1 bahamat bahamat 0 Mar 30 22:31 file1 -rw-r--r-- 1 bahamat bahamat 0 Mar 30 22:31 file2 -rw-r--r-- 1 bahamat bahamat 0 Mar 30 22:31 file with spaces $ ls -A1 file1 file2 file with spaces $ 

* Note: that’s a capital letter A and the number one.

I wonder why no one mentioned this simple command:

@msciwoj it just takes any strings separated by whitespaces and prints them sorted separated by endlines

It works here because ls reverts to ls -1 (single columnd output) when its output doesn’t go to a terminal (and disables non-printable character quoting/replacement; @Kusalananda, your ls is not POSIX compliant if it still does). ls output is already sorted. So piping it to sort will at best do nothing, and at worse mangle the output (if file names contain newline characters) or fail for filenames that are not text. ls -a | cat would be better. Or even better ls -A1 as already mentioned (and because you generally do want the replacements/quoting when displayed on a terminal)

Читайте также:  Максимальное количество файловых дескрипторов linux

Источник

How to reference filename with spaces in Linux

For Linux, the user terminal is the most crucial program to manage almost everything in the operating system. But many people do not prefer the command line because they assume that it is a very technical program and require particular expertise to use it. Though knowing it is pretty handy.

Various reasons keep a new Linux user away from the command line, one of them handling files in the terminal. Files and directories in Linux can have different names, and some names can have “spaces” in them. So what’s the big deal? The issue is, the terminal reads the “space” differently; for instance, if your directory has the name “new folder,” the terminal will assume two different directories, i.e., “new” and “folder.” Accessing such files in the terminal can become really frustrating. Luckily, there are various methods to deal with the files/folders with spaces in their names.

This guide is focusing on how to reference filename with spaces in Linux with different approaches. So let’s begin:

How to access files/directories in the terminal with spaces in the name:

In this section, we will be creating a file first with a “space” in its name; then, we will learn what errors we face while referring to it and then how to access it correctly in the terminal:

How to create a file and directory with space in its name:

Creating a file in Linux with “space” in its name is straightforward, open terminal, and run the command mentioned below:

The file can either be created by using apostrophes or quotation marks. The procedure of creating a directory is quite similar:

You can verify it by using the “ls” command in the terminal.

How to read a file with space in its name:

Before we learn the correct way to read a file with “space” in its name, let’s identify the error it can give. So when you try to read the above-created file (my file), you will get an error:

Now, let’s see what happens when you try to write something to the file:

As it can be seen that the above command, instead of writing the “my file,” creating a new file by the name of “my” and saving text to it. So, how to access such a file? Well, there are two approaches:

So first of all, let’s insert some text into the above-created file using ;“\”:

Источник

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.

Читайте также:  Code to flowchart linux

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[@] 

Источник

Handling filenames with spaces in Linux

It’s normal that we make files and directories (or we can say folders) in our machines to keep them organized, so when we need to, we can easily search for them. Sometimes we save them with the names having spaces, for example, we save a file with the name “my file” now in this case the Linux terminal will create an error. Can files not be saved with spaces in Linux? Yes! we can but they will be accessed differently in the terminal.

This write-up is focussing on what errors we face while accessing files and directories with space in their names and how to avoid such errors.

How to create a file with spaces in its name in Linux

To understand how to reference a filename with spaces in Linux, we will consider an example. First, we will open the terminal.

Then create a file with the name “my file” by using the touch command:

Now see the file is being created or not by using the “ls” command. We observed that instead of one, two files have been created, one with “my” and the second with the “file” name.

To use spaces in the name we use either quotes (‘ ’) or escape sequence (\). Now we again make another file using (‘ ’) and another using (\).

touch 'my file' touch test\ file 

Now again use the “ls” command to view files.

Files have been created. Let’s check if we get the same errors in the creation of a directory using space or not. We will create a directory using space with the mkdir command.

We will view whether the directory has been created or not using the “ls ” command. It created two directories instead of one.

We can rectify this in the same way as we did in the file creation method by using (‘ ‘) or (\). Again make a directory using this (‘ ‘) or (\).

Now we will check out the results.

How to reference filename with spaces in Linux

So we can see that the directory has been created according to our requirements. Now if we want to view the contents of the file, we simply use cat and file name with spaces; it will give us an error that the directory is not available.

We should use “\”. For example, we want to view the contents of a test file by using the cat command.

The file is empty so it displays no results but the command runs successfully. We can also open files by using apostrophes (‘ ’) or quotations (“ ”) as:

cat "test file" [OR] cat 'test file' 

Delete filenames with Spaces in Linux

Similarly, you can also delete a file and directory with space in their name by using apostrophes ( ‘ ’ ), quotation marks (“ ”) or escape sequence ( \ ).

Similarly, you can delete a directory with spaces in the name.

Conclusion

We create files and directories, without bothering to focus on the names that can have spaces. The Linux terminal treated files and folders differently that have spaces in their names. So this article solved the problem. If we want to name a file or directory with spaces we can do it by using apostrophes ( ‘ ’ ) , quotation marks (“ ”) or escape sequence ( \ ).

Читайте также:  Kali linux rolling sources list

Источник

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.

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.

Источник

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