Desktop 디렉토리에 "ls"를 입력 할 때 왜 여분의 공간이 있습니까?


27

호기심 때문에 Desktop 디렉토리에 ls을 입력 할 때 추가 공간이 필요한지 궁금합니다.비교 ...

~/Desktop$ ls
 file1 file2 file3

... to ...

~/Documents$ ls
file1 file2 file3

데스크톱에 ls을 입력하면 줄의 시작 부분에 파일 이름의 일부가 아닌 추가 공간이 생깁니다.줄의 시작 부분 에이 여분의 공간이있는 다른 예제 위치를 찾을 수 없습니다.다른 사람이 이것을 얻습니까?이유가 있습니까?

NB : Ubuntu Bionic Beaver LTS 릴리스와 함께 제공되는 기본 터미널을 사용하고 있습니다.

85

The extra space appears when you have file names in your directory needing ' around them (e.g. file names with spaces or other special characters).

~/mytmp$ ls
 a           'file(abc)'         proyecto3.csv   test         Test.sh
 b           'file(ab,c?).mp4'   rootfile        Test1.txt    Test.zip
 F26         'file(abc?).mp4'    scr             Test2.test   vowels
 Feb          guess              script          Test2.txt
'Feb 26 xx'   hw                 something       test.rar

After removing the 'strange' files I get:

~/mytmp$ ls
a    guess  proyecto3.csv  script     test        Test2.txt   Test.sh
b    hw     rootfile       something  Test1.txt   test.rar    Test.zip
F26  loop   scr            speak      Test2.test  testscript  vowels

53

I'm pretty sure file1 file2 file3 is not what's exactly shown on your screen. You omitted precious information (the actual filenames) that might easily be relevant.

My assumption is that you have a filename that contains a space or other special character.

Newer version of coreutils's ls quote such filenames, typically in single quotes. Plus, if there's at least one file requiring such quoting, it adds a space in front of all other filenames so that they align up "nicely" if they happen to be underneath each other, that is, the single quote mark (apostrophe) is ignored for alignment.

See --quoting-style and -N in the manual page of ls for some more details, and the section "Formatting the file names" in its info page for even more details. You might e.g. consider aliasing ls to ls -N.