LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Why Do My Files Have Quotes in the Them When I Use ‘ls’?

Tags: , , , Date/Time: October 20, 2021 @ 12:00 am, by raindog308

Bin LSIf you’ve upgraded to a recent Linux version, you may have noticed a change in the output of your ls commands.

Specifically, files that have spaces or other special characters in them are quoted.

For example, when you run ‘ls -l’, your directory listings that used to look like this:

# ls -1
Screen Shot 1.png
Screen Shot 2.png
Screen Shot 3.png
Screen Shot 4.png

Now, depending on what version of Linux you’re running, they look like this (Using ls -l here for variety but ls -1 or plain ls will show the same):

# ls -l
total 37044
-rwxr--r-- 1 andrew andrew  9468684 Oct 18 09:41 'Screen Shot 1.png'
-rwxr--r-- 1 andrew andrew  8468468 Oct 18 09:41 'Screen Shot 2.png'
-rwxr--r-- 1 andrew andrew  9473048 Oct 18 09:41 'Screen Shot 3.png'
-rwxr--r-- 1 andrew andrew 10514923 Oct 18 09:41 'Screen Shot 4.png'

In fact, you may first encounter this when you copy something from Windows or macOS (where spaces in filenames are more common) to Linux, and then wonder if Linux has suddenly added quotes to your file names.

It has not.  The GNU Project did this to you.  Allow me to explain, and how you can eliminate this idiocy.

Starting with GNU coreutils version 8.25 (released in January 2016), the ‘ls’ command will quote filenames with special characters.  Because there’s often a lag between new releases of GNU software making it to Linux distros (going through testing, etc.) and then another delay between the time it’s available in the distro and the time you choose to upgrade, you may just now be seeing this change.

The stated rationales for this are:

Unambiguous output: For example, if you have a file called “a b” and a file called “c” and then do an ‘ls’, the output is not clear if there are two files (“a b” and “c”) or three files (“a”, “b”, “c”).

Easier copy/pasting: You can more easily copy/past file names since they’re already quoted.

It improves security: It doesn’t, but the GNU developers actually said that.

GNU developers have too much time on their hand and know more than you and who are you, peon, to question their mighty wisdom:  Oops, this isn’t actually a stated rationale but is probably the truth.

This change has not been popular, as it flies in the face of 50 years’ tradition.  In fact, bug-coreutils@gnu.org has gotten so many complaints that the developers created a very long page defending their decision, which in itself might give them a clue that this is a bad idea.  At any rate, the developers have made it clear that (a) they are not going to change it back, and (b) they are really not interested in community feedback.

How to Fix This

Replacing the GNU developers would be the best method but we’ll set that aside for the moment.

You have three options:

(1) Use ls -N.  The -N flag will revert to normal quoting.  You could even do something like this:

alias ls="ls -N"

in your .bash_profile.

(2) set the QUOTING_STYLE variable in your .bash_profile (or /etc/profile if you want to fix it for all users on the system):

export QUOTING_STYLE=literal

(3) Switch to a BSD system, such as OpenBSD, FreeBSD, or NetBSD.

 

No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *