리눅스 ls 명령으로 파일 디렉토리 정보 확인하기

윈도우Windows에서는 파일 탐색기, 맥macOS에서는 파인더를 사용하면 대상 경로의 파일과 폴더를 손쉽게 확인할 수 있는데요. 리눅스의 CLI 환경에서는 ls 명령을 이용해 대상 경로의 파일과 폴더의 정보를 확인할 수 있습니다.

ls는 리눅스의 기초가 되는 명령이고 그만큼 자주 사용하기도 하는데 옵션이 40여개 이상으로 많아 아주 간단하다고 할 수는 없지만 언제나 그렇듯 모든 옵션을 다 알 필요는 없고 아래 안내하는 내용만 알아둔다면 리눅스 활용에 도움이 될 것입니다

아래 설명은 comeinsidebox.com 에서 테스트 및 검증 후 작성 된 내용이지만 이것이 본문의 내용의 정확성이나 신뢰성에 대해 보증을 하는 것은 아니니 단순 하게 참고용으로 확인바랍니다.

튜토리얼 환경: Ubuntu Server 22.04LTS

기본 구문

ls [Flags] [directory]
ls [Flags] [path]

ls 명령의 기본적인 구문은 위와 같은 형식이며 옵션 [Flags]와 디렉토리 [directory], 경로 [path]는 선택사항입니다.

root@ez:/etc/nginx# ls
conf.d  koi-win  nginx.conf  sites-enabled
fastcgi.conf mime.types  proxy_params  snippets
fastcgi_params modules-available scgi_params uwsgi_params
koi-utf  modules-enabled sites-available win-utf
root@ez:/etc/nginx# 

위와 같이 ls 만 입력하면 현재 경로의 폴더와 파일 목록의 이름만 출력 됩니다.

Ubuntu에서 PHP를 설치하는 방법

자주 사용하는 옵션

ls 만으로는 해당 파일의 정보를 정확하게 알 수 없기 때문에 옵션을 사용해야 하며 별도의 경로를 지정하면 현재 경로가 아니더라도 해당 경로의 내용을 확인할 수 있습니다.

특정 경로의 폴더와 파일 목록 확인

현재 위치와 다른 경로, 루트 경로 그리고 부모 경로와 같이 특정 경로의 파일과 폴더 목록을 바로 확인할 수 있습니다.

다른 경로 디렉토리 목록 확인

ls [directory]

위와 같이 입력하면 해당 디렉토리의 파일과 폴더 목록이 출력됩니다.

root@ez:/etc/nginx# ls sites-available
default
root@ez:/etc/nginx# 

위 내용으로 /etc/nginx 경로의 sites-available 폴더에는 default 라는 항목만 하나 있는 것을 확인할 수 있습니다.

루트 디렉토리 목록 확인

ls /

위와 같이 입력하면 루트 디렉토리의 파일과 폴더 목록이 출력됩니다.

root@ez:/etc/nginx# ls /
bin etc lib32 lost+found opt run srv  tmp
boot home lib64 media  proc sbin swap.img usr
dev lib libx32 mnt  root snap sys  var
root@ez:/etc/nginx# 

프롬프트에 표시된 /etc/nginx 경로의 파일이 아닌 루트 경로의 파일 목록이 출력되는 것을 볼 수 있습니다.

부모 디렉토리 목록 확인

ls ..

위와 같이 입력하면 부모 디렉토리의 파일과 폴더 목록이 출력됩니다.

root@ez:/etc/nginx# ls ..
adduser.conf  kernel   profile.d
alternatives  landscape  protocols
apache2    ldap    python3
apparmor   ld.so.cache  python3.10
apparmor.d   ld.so.conf   rc0.d
apport   ld.so.conf.d  rc1.d
apt    legal   rc2.d
bash.bashrc   letsencrypt  rc3.d
bash_completion  libaudit.conf  rc4.d

(생략)

ImageMagick-6   passwd-   vmware-tools
init.d   perl    vtrgb
initramfs-tools  php   wgetrc
inputrc    pki   wpa_supplicant
iproute2   pm    X11
iscsi    polkit-1   xattr.conf
issue    pollinate  xdg
issue.net   profile   zsh_command_not_found
root@ez:/etc/nginx# 

/etc/nginx 경로가 아닌 /etc 아래 위치한 파일과 폴더 목록이 표시됩니다.

ls ../..

위와 같이 응용하면 현재 위치에서 두 단계 위 경로의 파일과 폴더 목록이 출력됩니다.

대용량 파일 토렌트 파일로 만들고 공유하는 방법 암호 설정까지

자세한 파일 목록 확인

ls -l

ls에서 -l 옵션을 사용하면 한줄에 하나의 항목을 자세하게 표시합니다. 아마도 가장 자주 사용하는 옵션이 아닐까 생각합니다.

root@ez:/etc/nginx# ls -l
total 64
drwxr-xr-x 2 root root 4096 Mar 7 09:35 conf.d
-rw-r--r-- 1 root root 1125 Jul 27 2022 fastcgi.conf
-rw-r--r-- 1 root root 1055 Jul 27 2022 fastcgi_params
-rw-r--r-- 1 root root 2837 Jul 27 2022 koi-utf
-rw-r--r-- 1 root root 2223 Jul 27 2022 koi-win
-rw-r--r-- 1 root root 3957 Aug 2 2022 mime.types
drwxr-xr-x 2 root root 4096 Nov 10 15:39 modules-available
drwxr-xr-x 2 root root 4096 Mar 6 17:11 modules-enabled
-rw-r--r-- 1 root root 1447 Jul 27 2022 nginx.conf
-rw-r--r-- 1 root root 180 Jul 27 2022 proxy_params
-rw-r--r-- 1 root root 636 Jul 27 2022 scgi_params
drwxr-xr-x 2 root root 4096 Mar 6 17:10 sites-available
drwxr-xr-x 2 root root 4096 Mar 6 17:10 sites-enabled
drwxr-xr-x 2 root root 4096 Mar 6 17:10 snippets
-rw-r--r-- 1 root root 664 Jul 27 2022 uwsgi_params
-rw-r--r-- 1 root root 3071 Jul 27 2022 win-utf
root@ez:/etc/nginx#

위와 같이 긴 목록 형식long listing format으로 출력되고 다음과 같은 정보들을 모두 확인할 수 있습니다.

  • 전체 파일, 폴더의 수량
  • 파일, 폴더, 링크 구분
  • 권한 수준
  • 링크된 개수
  • 소유자
  • 그룹 소유자
  • 바이트 단위
  • 마지막으로 수정한 날짜 및 시간
  • 파일 또는 폴더의 이름
참고: 일반적으로 `ls -l`은 `ll`이라는 별칭alias 으로 등록되어 `ll` 이라고 두 글자만 입력하면 동일한 결과를 얻을 수 있습니다.

데비안에서 alias로 별칭 등록해서 사용하기

리눅스에서는 bash 셸에서 alias를 사용하여 명령어를 단축하여 사용할 수 있습니다. 데비안 배포판에서는 다음과 같은 순서로 ll 명령에 ls -l이 동작하도록 alias를 등록할 수 있습니다.

  1. 터미널에서 vi ~/.bashrc 명령을 실행해 파일을 열어줍니다.
  2. alias ll='ls -l'이라고 작성합니다. 또는 alias 지정할 별칭='사용할 명령' 과 같은 형식으로 다른 명령과 옵션을 등록합니다.
  3. wq;로 파일을 저장하고 닫은 뒤 ll을 입력하면 ls -l 명령어가 실행됩니다.

alias를 통해 자주 사용하는 명령을 등록해두면 상당히 편리하게 사용할 수 있습니다.

참고: 이 방식은 현재 사용자 계정에서만 동작합니다. 모든 사용자 계정에서 ll을 사용하려면 /etc/bash.bashrc 파일에 alias를 추가해야 합니다.

레드햇에서 alias로 별칭 등록해서 사용하기

리눅스에서는 bash 셸에서 alias를 사용하여 명령어를 단축하여 사용할 수 있습니다. 레드햇 계열 배포판에서는 다음과 같은 순서로 ll 명령에 ls -l이 동작하도록 alias를 등록할 수 있습니다.

  1. 터미널에서 vi ~/.bash_profile 명령을 실행해 파일을 열어줍니다.
  2. alias ll='ls -l'이라고 작성합니다. 또는 alias 지정할 별칭='사용할 명령' 과 같은 형식으로 다른 명령과 옵션을 등록합니다.
  3. wq;로 파일을 저장하고 닫은 뒤 ll을 입력하면 ls -l 명령어가 실행됩니다.

alias를 통해 자주 사용하는 명령을 등록해두면 상당히 편리하게 사용할 수 있습니다.

참고: 이 방식은 현재 사용자 계정에서만 동작합니다. 모든 사용자 계정에서 ll을 사용하려면 /etc/profile 파일에 alias를 추가해야 합니다.

숨김 파일 포함한 전체 목록 확인

ls -a

이전 설명의 ls -l 옵션 다음으로 자주 사용하는 옵션으로, 숨겨진 파일과 폴더도 모두 포함된 상태의 목록을 확인할 수 있습니다. 리눅스에서는 파일과 폴더 명 앞에 .이 추가되어 있다면 숨김 파일로 간주되는데 이렇게 숨겨진 항목을 모두 확인할 수 있는 것 입니다.

root@ez:/etc/nginx# ls -a
.   koi-utf  nginx.conf  snippets
..   koi-win  proxy_params  uwsgi_params
.HiddenFiles conf.d   mime.types  scgi_params
win-utf  fastcgi.conf  sites-available fastcgi_params 
modules-available modules-enabled sites-enabled
root@ez:/etc/nginx# 

.HiddenFiles 과 같이 숨겨진 목록과 파일들이 존재한다면 모두 포함해서 표기 됩니다.

맥 윈도우 리눅스 CopyQ로 클립보드 저장하고 쉽게 재사용하기

숨겨진 파일 포함 자세한 목록 확인

ls -l -a
ls -a -l
ls -la
ls -al

다음과 같이 -l-a 옵션을 함께 사용하면 숨겨진 파일과 폴더를 포함한 전체 목록을 자세하게 확인할 수 있습니다. 필자의 경우 ls -la 라고 사용하며 이외의 다른 옵션들도 동일하게 조합해서 활용할 수 있습니다.

root@ez:/etc/nginx# ls -la
total 73
drwxr-xr-x 8 root root 4096 Mar 6 17:24 .
drwxr-xr-x 109 root root 4096 Mar 9 06:47 ..
-rw-r--r-- 2 root root 1096 Mar 10 09:40 .HiddenFiles
drwxr-xr-x 1 root root 4096 Mar 7 09:35 conf.d
-rw-r--r-- 1 root root 1125 Jul 27 2022 fastcgi.conf
-rw-r--r-- 1 root root 1055 Jul 27 2022 fastcgi_params
-rw-r--r-- 1 root root 2837 Jul 27 2022 koi-utf
-rw-r--r-- 1 root root 2223 Jul 27 2022 koi-win
-rw-r--r-- 1 root root 3957 Aug 2 2022 mime.types
drwxr-xr-x 2 root root 4096 Nov 10 15:39 modules-available
drwxr-xr-x 2 root root 4096 Mar 6 17:11 modules-enabled
-rw-r--r-- 1 root root 1447 Jul 27 2022 nginx.conf
-rw-r--r-- 1 root root 180 Jul 27 2022 proxy_params
-rw-r--r-- 1 root root 636 Jul 27 2022 scgi_params
drwxr-xr-x 2 root root 4096 Mar 6 17:10 sites-available
drwxr-xr-x 2 root root 4096 Mar 6 17:10 sites-enabled
drwxr-xr-x 2 root root 4096 Mar 6 17:10 snippets
-rw-r--r-- 1 root root 664 Jul 27 2022 uwsgi_params
-rw-r--r-- 1 root root 3071 Jul 27 2022 win-utf
root@ez:/etc/nginx# 

자세한 내용으로 숨겨진 항목이 포함된 모든 파일과 폴더 목록을 확인할 수 있습니다.

모든 옵션 확인

root@ez:/etc/nginx# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
 -a, --all   do not ignore entries starting with .
 -A, --almost-all   do not list implied . and ..
     --author   with -l, print the author of each file
 -b, --escape   print C-style escapes for nongraphic characters     --block-size=SIZE with -l, scale sizes by SIZE when printing them;
      e.g., '--block-size=M'; see SIZE format below -B, --ignore-backups  do not list implied entries ending with ~
 -c     with -lt: sort by, and show, ctime (time of last
      modification of file status information);
      with -l: show ctime and sort by name;
      otherwise: sort by ctime, newest first
 -C     list entries by columns
     --color[=WHEN]  colorize the output; WHEN can be 'always' (default
      if omitted), 'auto', or 'never'; more info below
 -d, --directory  list directories themselves, not their contents -D, --dired   generate output designed for Emacs' dired mode
 -f     do not sort, enable -aU, disable -ls --color
 -F, --classify   append indicator (one of */=>@|) to entries
     --file-type  likewise, except do not append '*'
     --format=WORD  across -x, commas -m, horizontal -x, long -l,
      single-column -1, verbose -l, vertical -C
     --full-time  like -l --time-style=full-iso
 -g     like -l, but do not list owner
     --group-directories-first
      group directories before files;
      can be augmented with a --sort option, but any
      use of --sort=none (-U) disables grouping
 -G, --no-group   in a long listing, don't print group names
 -h, --human-readable  with -l and -s, print sizes like 1K 234M 2G etc.
     --si    likewise, but use powers of 1000 not 1024
 -H, --dereference-command-line
      follow symbolic links listed on the command line
     --dereference-command-line-symlink-to-dir
      follow each command line symbolic link
      that points to a directory
     --hide=PATTERN  do not list implied entries matching shell PATTERN
      (overridden by -a or -A)
     --hyperlink[=WHEN]  hyperlink file names; WHEN can be 'always'
      (default if omitted), 'auto', or 'never'
     --indicator-style=WORD append indicator with style WORD to entry names:
      none (default), slash (-p),
      file-type (--file-type), classify (-F)
 -i, --inode   print the index number of each file
 -I, --ignore=PATTERN  do not list implied entries matching shell PATTERN
 -k, --kibibytes  default to 1024-byte blocks for disk usage;
      used only with -s and per directory totals
 -l     use a long listing format
 -L, --dereference  when showing file information for a symbolic
      link, show information for the file the link
      references rather than for the link itself
 -m     fill width with a comma separated list of entries
 -n, --numeric-uid-gid like -l, but list numeric user and group IDs
 -N, --literal   print entry names without quoting
 -o     like -l, but do not list group information
 -p, --indicator-style=slash
      append / indicator to directories
 -q, --hide-control-chars print ? instead of nongraphic characters
     --show-control-chars show nongraphic characters as-is (the default,
      unless program is 'ls' and output is a terminal)
 -Q, --quote-name   enclose entry names in double quotes
     --quoting-style=WORD use quoting style WORD for entry names:
      literal, locale, shell, shell-always,
      shell-escape, shell-escape-always, c, escape
      (overrides QUOTING_STYLE environment variable)
 -r, --reverse   reverse order while sorting
 -R, --recursive  list subdirectories recursively
 -s, --size    print the allocated size of each file, in blocks
 -S     sort by file size, largest first
     --sort=WORD  sort by WORD instead of name: none (-U), size (-S),
      time (-t), version (-v), extension (-X)
     --time=WORD  change the default of using modification times;      access time (-u): atime, access, use;
      change time (-c): ctime, status;
      birth time: birth, creation;
      with -l, WORD determines which time to show;
      with --sort=time, sort by WORD (newest first)
     --time-style=TIME_STYLE time/date format with -l; see TIME_STYLE below
 -t     sort by time, newest first; see --time
 -T, --tabsize=COLS  assume tab stops at each COLS instead of 8
 -u     with -lt: sort by, and show, access time;
      with -l: show access time and sort by name;
      otherwise: sort by access time, newest first
 -U     do not sort; list entries in directory order
 -v     natural sort of (version) numbers within text
 -w, --width=COLS   set output width to COLS. 0 means no limit
 -x     list entries by lines instead of by columns
 -X     sort alphabetically by entry extension
 -Z, --context   print any security context of each file
 -1     list one file per line. Avoid '\n' with -q or -b
     --help  display this help and exit
     --version output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT.
FORMAT is interpreted like in date(1). If FORMAT is FORMAT1<newline>FORMAT2,
then FORMAT1 applies to non-recent files and FORMAT2 to recent files.
TIME_STYLE prefixed with 'posix-' takes effect only outside the POSIX locale.
Also the TIME_STYLE environment variable sets the default style to use.

Using color to distinguish file types is disabled both by default and
with --color=never. With --color=auto, ls emits color codes only when
standard output is connected to a terminal. The LS_COLORS environment
variable can change the settings. Use the dircolors command to set it.

Exit status:
 0 if OK,
 1 if minor problems (e.g., cannot access subdirectory),
 2 if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
root@ez:/etc/nginx# 

ls 명령의 옵션이 40여개 정도 되는데 이를 모두다 알아야 할 필요는 없으며 일반적으로 전술한 내용 정도만 알아두면 사용에 무리가 없는데요. 자주 사용하지 않는 명령을 더 알고 싶은 경우 ls --helpman ls를 실행하면 ls 명령으로 사용 가능한 모든 옵션에 대한 내용을 확인할 수도 있습니다.

데이터베이스 관리를 위한 오픈 소스 데이터베이스 도구 Adminer 사용법

관련 글

댓글로 남기기 어려운 내용은 Contact Form 에서 개별적으로 문의 할 수 있습니다. 해당 글과 연관 된 내용은 Copy를 이용해 현재 페이지의 주소를 복사 후 문의 폼에 입력시 보다 정확한 답을 얻을 수 있습니다.

Leave a Comment