Skip to content

Ls

ls는 POSIX 및 단일 유닉스 규격에 규정되어 있는 유닉스 계열 운영 체제의 명령어 가운데 하나로, 'list segments'의 약자이며, 파일의 목록을 표시하는 기능을 수행하는 명령어이다. 도스에서의 dir과 유사한 명령어이다.

Flags

  • ls -a: list all files including hidden file starting with '.'
  • ls --color: colored list [=always/never/auto]
  • ls -d: list directories - with ' */'
  • ls -F: add one char of */=>@| to enteries
  • ls -1: 한 줄로 출력하고 싶다면

심볼릭 링크 경로 읽는 방법

pwd 같은걸로는 못읽는다.

  • readlink 사용: readlink -f {symlinkName}
  • Python 사용: python -c 'import os.path; print(os.path.realpath("symlinkName"))'
  • realpath 사용: realpath {symlinkName}

Format

예시로 다음과 같다:

drwxr-xr-x@ 19 yourname  staff   646  2 20 08:42 gdrive

각 항목들에 대한 정보

  • d - 첫 번째 글자는 파일 형태이다.
  • rwxr-xr-x - 파일 형태 및 사용 권한 정보.
    • 세 글자씩, 묶음으로 소유자 권한 (Owner), 그룹 권한 (Group), 익명 권한 (Anonimouse)
    • r = Read
    • w = Write
    • x = Execute
  • @ - 확장 정보 존재 여부, #What is at('@')항목) 참조.
  • 19 - 하드 링크의 개수. 자세한 내용은 inode 항목 참조.
  • yourname - Owner 이름
  • staff - Group 이름
  • 646 - 파일 크기. -h옵션이 없다면 byte 단위.
  • 2 20 08:42 - 따로 지정하지 않으면 파일 생성 시간.
    • 만약 생성 시간이 여섯달 이전 것이면, 시간이 생략되고, 파일의 연도가 포함된다.
  • gdrive - 파일 이름.

What is at('@')

macOS에서 ls -l를 사용할 경우 아래와 같이 @으로 표시된 항목이 출력된다.

yournameui-MacBook-Pro:~ yourname$ ls -l
total 8
drwx------@  3 yourname  staff   102  2  6 10:11 Applications
drwx------+  4 yourname  staff   136  2 14 15:36 Desktop
drwx------+  3 yourname  staff   102  2  6 10:00 Documents
drwx------+ 17 yourname  staff   578  2 20 09:52 Downloads
drwx------@ 62 yourname  staff  2108  2 14 08:46 Library
drwx------+  3 yourname  staff   102  2  6 10:00 Movies
drwx------+  3 yourname  staff   102  2  6 10:00 Music
drwx------+  3 yourname  staff   102  2  6 10:00 Pictures
drwxr-xr-x  26 yourname  staff   884  2 17 14:23 Project
drwxr-xr-x+  5 yourname  staff   170  2 10 12:47 Public
drwxr-xr-x   3 yourname  staff   102  2 10 12:44 bin
drwxr-xr-x@ 19 yourname  staff   646  2 20 08:42 gdrive
-rw-r--r--   1 yourname  staff  2268  2 13 12:28 java0.log
drwxr-xr-x  28 yourname  staff   952  2 14 16:31 torch

이 항목은 man 페이지에서 아래와 같이 확인할 수 있다.

If the -l option is given, the following information is displayed for each file: 
file mode, number of links, owner name, group name, number of bytes in the file, abbreviated month, day-of-month file was last modified, hour file last modified, minute file last modified, and the pathname.
In addition, for each directory whose contents are displayed,
the total number of 512-byte blocks used by the files in the directory is displayed on a line by itself,
immediately before the information for the files in the directory.

If the file or directory has extended attributes, the permissions field printed by the -l option is followed by a '@' character.
Otherwise, if the file or directory has extended security information (such as an access control list),
the permissions field printed by the -l option is followed by a '+' character.

한마디로 확장 속성(extended attributes)이 존재할 경우 해당 문자가 출력된다는 것을 알 수 있다. 확장속성을 확인하고 싶다면 -@옵션을 추가하면 된다.

yournameui-MacBook-Pro:~ yourname$ ls -l@
total 8
drwx------@  3 yourname  staff   102  2  6 10:11 Applications
    com.apple.quarantine      25
drwx------+  4 yourname  staff   136  2 14 15:36 Desktop
drwx------+  3 yourname  staff   102  2  6 10:00 Documents
drwx------+ 17 yourname  staff   578  2 20 09:52 Downloads
drwx------@ 62 yourname  staff  2108  2 14 08:46 Library
    com.apple.FinderInfo      32
drwx------+  3 yourname  staff   102  2  6 10:00 Movies
drwx------+  3 yourname  staff   102  2  6 10:00 Music
drwx------+  3 yourname  staff   102  2  6 10:00 Pictures
drwxr-xr-x  26 yourname  staff   884  2 17 14:23 Project
drwxr-xr-x+  5 yourname  staff   170  2 10 12:47 Public
drwxr-xr-x   3 yourname  staff   102  2 10 12:44 bin
drwxr-xr-x@ 19 yourname  staff   646  2 20 08:42 gdrive
    com.apple.FinderInfo      32
-rw-r--r--   1 yourname  staff  2268  2 13 12:28 java0.log
drwxr-xr-x  28 yourname  staff   952  2 14 16:31 torch

Add Color to the Terminal

ls명령어에 색상값을 적용하고 싶을 경우 아래와 같이 진행하면 된다.
bash에 자동으로 적용되도록 하기 위해, 우선 홈 디렉토리에 .bash_profile파일을 생성한다.
이 후 아래와 같이 환경변수를 추가한다.

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

See also

Favorite site

References


  1. Ls_command_manual.pdf