강좌
클라우드/리눅스에 관한 강좌입니다.
리눅스 분류

파일내용정렬 마법사 sort 실무7편: 디렉토리내의 파일들의 파일명을 기준으로 정렬하기

작성자 정보

  • 관리자 작성
  • 작성일

컨텐츠 정보

본문

파일내용정렬 마법사 sort 실무7: 디렉토리내의 파일들의 파일명을 기준으로 정렬하기

 

 

 

 

이번에는 현재 디렉토리의 파일이름을 대상으로 오름차순 정렬한 것이다.

 

 

 

 ls -l의 결과 중 각 행의 8번째 필드가 파일명을 나타내므로 이 이름을 기준으로 정렬한 결과이다.

 

 

 

 

 

[root@sulinux ~]# ls -l /var/log | sort -k 8

합계 1672

drwxr-xr-x 2 root root   4096 2022-12-01 22:23 ConsoleKit

-rw-r--r-- 1 root root  22149 2022-12-04 23:33 Xorg.0.log

-rw-r--r-- 1 root root  22149 2022-12-04 03:39 Xorg.0.log.old

-rw------- 1 root root  27728 2022-12-01 22:16 anaconda.log

-rw------- 1 root root  25749 2022-12-01 22:16 anaconda.syslog

-rw------- 1 root root  21424 2022-12-01 22:16 anaconda.xlog

drwxr-x--- 2 root root   4096 2022-12-01 22:22 audit

-rw-r--r-- 1 root root   2285 2022-12-24 20:41 boot.log

-rw-r--r-- 2 root root   2285 2022-12-18 23:00 boot.log-20081219

-rw-r--r-- 1 root root   2285 2022-12-23 02:09 boot.log-20081223

-rw------- 1 root utmp   1920 2022-12-03 23:40 btmp

-rw------- 1 root root   4321 2022-12-25 01:01 cron

-rw------- 1 root root  21521 2022-12-19 04:02 cron-20081219

-rw------- 1 root root   3758 2022-12-23 04:02 cron-20081223

drwxr-xr-x 2 lp   sys    4096 2022-12-23 04:02 cups

drwxr-xr-x 2 root root   4096 2022-08-02 13:22 dirmngr

-rw-r--r-- 1 root root  20944 2022-12-24 20:41 dmesg

-rw-r--r-- 1 root root  20944 2022-12-23 17:08 dmesg.old

-rw------- 1 root root  12096 2022-12-21 22:48 faillog

drwxrwx--T 2 root gdm    4096 2022-12-04 20:26 gdm

drwx------ 2 root root   4096 2022-12-23 04:02 httpd

-rw-r--r-- 1 root root 147168 2022-12-24 20:42 lastlog

drwxr-xr-x 2 root root   4096 2022-12-01 21:59 mail

-rw------- 1 root root   1574 2022-12-24 20:41 maillog

-rw------- 1 root root  16240 2022-12-19 04:02 maillog-20081219

-rw------- 1 root root   2280 2022-12-23 04:02 maillog-20081223

-rw------- 1 root root  76664 2022-12-24 23:24 messages

-rw------- 1 root root 768661 2022-12-19 04:02 messages-20081219

-rw------- 1 root root 151722 2022-12-23 04:02 messages-20081223

drwxr-xr-x 2 ntp  ntp    4096 2022-10-08 20:23 ntpstats

drwx------ 2 root root   4096 2022-08-29 06:48 ppp

drwxr-xr-x 2 root root   4096 2022-12-01 23:41 prelink

-rw-r--r-- 1 root root  45798 2022-12-24 04:02 rpmpkgs

-rw-r--r-- 1 root root  45798 2022-12-06 04:02 rpmpkgs-20081219

-rw-r--r-- 1 root root  45798 2022-12-19 04:04 rpmpkgs-20081223

drwx------ 3 root root   4096 2022-12-01 22:01 samba

-rw------- 1 root root   2533 2022-12-24 20:42 secure

-rw------- 1 root root  28346 2022-12-19 02:20 secure-20081219

-rw------- 1 root root   7144 2022-12-23 02:24 secure-20081223

drwxr-xr-x 2 root root   4096 2022-12-23 04:02 setroubleshoot

-rw-r--r-- 1 root root     91 2022-12-22 23:03 spacefile

-rw------- 1 root root      0 2022-12-23 04:02 spooler

-rw------- 1 root root      0 2022-12-01 21:55 spooler-20081219

-rw------- 1 root root      0 2022-12-19 04:02 spooler-20081223

-rw------- 1 root root      0 2022-12-01 21:54 tallylog

drwxr-xr-x 2 root root   4096 2022-10-02 23:32 vbox

-rw-r--r-- 1 root root      0 2022-12-01 22:23 wpa_supplicant.log

-rw-rw-r-- 1 root utmp 141696 2022-12-24 20:42 wtmp

-rw-r--r-- 1 root root      0 2022-12-01 22:26 yum.log

[root@sulinux ~]#



 

오름차순 정렬은 정확하게 표현하자면 ASCII코드의 순서값을 기준으로 오름차순 정렬이 되므로 대문자가 소문자보다 앞에 온다.

 

 

 

 따라서 위의 결과에서 보면 X라는 대문자가 소문자보다 앞에 오기 때문에 맨 위에 정렬된 것이다.

 

 

 

 

 

이번 예에서 주목해야하는 것은 특정 디렉토리의 파일명을 기준으로 정렬을 하였다는 것이다.

 

 

 

 특정 디렉토리내의 파일들을 알파벳순서대로 정렬할 때에 유용하게 사용할 수 있다.

 

 

 

 

 

다음은 앞의 예와 반대의 경우로서 현재디렉토리에 존재하는 파일명을 기준으로 내림차순 정렬한 것이다.

 

 

 

 ls -l의 결과 중 각 행의 8번째 필드가 파일명을 나타내므로 "-k 8"라는 옵션과 -r옵션을 사용하여 표시한 것이다.

 

 

 

  

 

[root@sulinux ~]# ls -l /var/log | sort -rk 8

-rw-r--r-- 1 root root      0 2022-12-01 22:26 yum.log

-rw-rw-r-- 1 root utmp 141696 2022-12-24 20:42 wtmp

-rw-r--r-- 1 root root      0 2022-12-01 22:23 wpa_supplicant.log

drwxr-xr-x 2 root root   4096 2022-10-02 23:32 vbox

-rw------- 1 root root      0 2022-12-01 21:54 tallylog

-rw------- 1 root root      0 2022-12-19 04:02 spooler-20081223

-rw------- 1 root root      0 2022-12-01 21:55 spooler-20081219

-rw------- 1 root root      0 2022-12-23 04:02 spooler

-rw-r--r-- 1 root root     91 2022-12-22 23:03 spacefile

drwxr-xr-x 2 root root   4096 2022-12-23 04:02 setroubleshoot

-rw------- 1 root root   7144 2022-12-23 02:24 secure-20081223

-rw------- 1 root root  28346 2022-12-19 02:20 secure-20081219

-rw------- 1 root root   2533 2022-12-24 20:42 secure

drwx------ 3 root root   4096 2022-12-01 22:01 samba

-rw-r--r-- 1 root root  45798 2022-12-19 04:04 rpmpkgs-20081223

-rw-r--r-- 1 root root  45798 2022-12-06 04:02 rpmpkgs-20081219

-rw-r--r-- 1 root root  45798 2022-12-24 04:02 rpmpkgs

drwxr-xr-x 2 root root   4096 2022-12-01 23:41 prelink

drwx------ 2 root root   4096 2022-08-29 06:48 ppp

drwxr-xr-x 2 ntp  ntp    4096 2022-10-08 20:23 ntpstats

-rw------- 1 root root 151722 2022-12-23 04:02 messages-20081223

-rw------- 1 root root 768661 2022-12-19 04:02 messages-20081219

-rw------- 1 root root  76664 2022-12-24 23:24 messages

-rw------- 1 root root   2280 2022-12-23 04:02 maillog-20081223

-rw------- 1 root root  16240 2022-12-19 04:02 maillog-20081219

-rw------- 1 root root   1574 2022-12-24 20:41 maillog

drwxr-xr-x 2 root root   4096 2022-12-01 21:59 mail

-rw-r--r-- 1 root root 147168 2022-12-24 20:42 lastlog

drwx------ 2 root root   4096 2022-12-23 04:02 httpd

drwxrwx--T 2 root gdm    4096 2022-12-04 20:26 gdm

-rw------- 1 root root  12096 2022-12-21 22:48 faillog

-rw-r--r-- 1 root root  20944 2022-12-23 17:08 dmesg.old

-rw-r--r-- 1 root root  20944 2022-12-24 20:41 dmesg

drwxr-xr-x 2 root root   4096 2022-08-02 13:22 dirmngr

drwxr-xr-x 2 lp   sys    4096 2022-12-23 04:02 cups

-rw------- 1 root root   3758 2022-12-23 04:02 cron-20081223

-rw------- 1 root root  21521 2022-12-19 04:02 cron-20081219

-rw------- 1 root root   4321 2022-12-25 01:01 cron

-rw------- 1 root utmp   1920 2022-12-03 23:40 btmp

-rw-r--r-- 1 root root   2285 2022-12-23 02:09 boot.log-20081223

-rw-r--r-- 2 root root   2285 2022-12-18 23:00 boot.log-20081219

-rw-r--r-- 1 root root   2285 2022-12-24 20:41 boot.log

drwxr-x--- 2 root root   4096 2022-12-01 22:22 audit

-rw------- 1 root root  21424 2022-12-01 22:16 anaconda.xlog

-rw------- 1 root root  25749 2022-12-01 22:16 anaconda.syslog

-rw------- 1 root root  27728 2022-12-01 22:16 anaconda.log

-rw-r--r-- 1 root root  22149 2022-12-04 03:39 Xorg.0.log.old

-rw-r--r-- 1 root root  22149 2022-12-04 23:33 Xorg.0.log

drwxr-xr-x 2 root root   4096 2022-12-01 22:23 ConsoleKit

합계 1672

[root@sulinux ~]#

 

 

 

 

이번 예에서 주목해야하는 것은 특정 디렉토리의 파일명을 기준으로 내림차순(역순)정렬을 하였다는 것이다.

 

 

 

 특정 디렉토리내의 파일명을 기준으로 내림차순(역순) 정렬할 때에 유용하게 사용할 수 있다.

 

 

 

 

 

 

 

 

 

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,042 명
  • 현재 강좌수 :  35,846 개
  • 현재 접속자 :  131 명