우리는 일반적으로 파일 형태로 많은 정보를 시스템에 저장하는 습관이 있습니다. 일부는 숨겨진 파일, 일부는 우리의 이해를 돕기 위해 별도로 생성된 폴더에 보관되며, 일부는 그대로 유지됩니다. 하지만 이 모든 것들이 우리의 디렉토리를 채워; 보통은 바탕 화면을 어지럽게 만듭니다. 문제는 이 거대한 컬렉션에서 특정 날짜와 시간에 수정된 특정 파일을 검색해야 할 때 발생합니다.

GUI에 익숙한 사람들은 파일 관리자를 사용하여 파일을 긴 목록 형식으로 나열하여 원하는 것을 쉽게 찾을 수 있지만, 검은 화면의 습관을 가진 사용자나 GUI가 없는 서버에서 작업하는 사람이라면 검색을 간소화할 수 있는 간단한 명령이나 명령 세트를 원할 것입니다.
리눅스의 진정한 아름다움이 여기에서 드러납니다. 리눅스에는 명령어 모음이 있으며, 이를 따로 또는 함께 사용하면 파일을 검색하거나 파일 모음을 이름, 수정 날짜, 생성 시간 또는 결과를 얻기 위해 적용할 수 있는 필터에 따라 정렬하는 데 도움이 될 수 있습니다.
여기에서 우리는 파일을 정렬하거나 파일 목록을 날짜 및 시간별로 정렬할 수 있는 명령 세트를 검토함으로써 리눅스의 진정한 강점을 밝혀낼 것입니다.
리눅스에서 파일을 정렬하는 리눅스 유틸리티
날짜 및 시간에 따라 디렉토리를 정렬하기에 충분한 몇 가지 기본 리눅스 명령줄 유틸리티는 다음과 같습니다:
ls 명령
ls – 디렉터리 내 내용을 列挙하는 도구이며, 이 유틸리티는 파일과 디렉터리의 정보를 列挙할 수 있으며, 수정 또는 접근 시간, 권한, 크기, 소유자, 그룹 등의 모든 상태 정보를 포함할 수 있습니다.
우리는 Linux ls 명령어와 sort 명령어에 대한 많은 記事을 이미 다루었습니다. 이 記事들은 다음과 같은 곳에 있습니다:
sort 명령어
sort – 이 명령어는 search의 결과를 어떤 필드나 필드의 특정 열에 따라 정렬할 수 있습니다.
Linux sort 명령어에 대한 두 가지 記事을 이미 다루었습니다. 이 记事들은 다음과 같은 곳에 있습니다:
이러한 명령은 검은 화면에서 작업하고 많은 파일을 처리해야 하는 경우에 사용되는 매우 강력한 명령입니다.
날짜와 시간을 사용하여 파일을 정렬하는 방법
아래는 날짜와 시간 기반으로 정렬할 수 있는 명령어 목록입니다.
1. 수정 시간을 기반으로 파일 목록
아래 명령어는 Long Listing 형식으로 파일을 목록화하고, 가장 최근에 수정된 파일이 먼저 정렬되는 것처럼 보여줍니다. 倒了 정렬 순서를 얻고자 하면, 이 명령어와 함께 '-r'
스위치를 사용하면 됩니다.
# ls -lt total 673768 -rwxr----- 1 tecmint tecmint 3312130 Jan 19 15:24 When You Are Gone.MP3 -rwxr----- 1 tecmint tecmint 4177212 Jan 19 15:24 When I Dream At Night - Marc Anthony-1.mp3 -rwxr----- 1 tecmint tecmint 4177212 Jan 19 15:24 When I Dream At Night - Marc Anthony.mp3 -rwxr----- 1 tecmint tecmint 6629090 Jan 19 15:24 Westlife_Tonight.MP3 -rwxr----- 1 tecmint tecmint 3448832 Jan 19 15:24 We Are The World by USA For Africa (Michael Jackson).mp3 -rwxr----- 1 tecmint tecmint 8580934 Jan 19 15:24 This Love.mp3 -rwxr----- 1 tecmint tecmint 2194832 Jan 19 15:24 The Cross Of Changes.mp3 -rwxr----- 1 tecmint tecmint 5087527 Jan 19 15:24 T.N.T. For The Brain 5.18.mp3 -rwxr----- 1 tecmint tecmint 3437100 Jan 19 15:24 Summer Of '69.MP3 -rwxr----- 1 tecmint tecmint 4360278 Jan 19 15:24 Smell Of Desire.4.32.mp3 -rwxr----- 1 tecmint tecmint 4582632 Jan 19 15:24 Silence Must Be Heard 4.46.mp3 -rwxr----- 1 tecmint tecmint 4147119 Jan 19 15:24 Shadows In Silence 4.19.mp3 -rwxr----- 1 tecmint tecmint 4189654 Jan 19 15:24 Sarah Brightman & Enigma - Eden (remix).mp3 -rwxr----- 1 tecmint tecmint 4124421 Jan 19 15:24 Sade - Smooth Operator.mp3 -rwxr----- 1 tecmint tecmint 4771840 Jan 19 15:24 Sade - And I Miss You.mp3 -rwxr----- 1 tecmint tecmint 3749477 Jan 19 15:24 Run To You.MP3 -rwxr----- 1 tecmint tecmint 7573679 Jan 19 15:24 Roger Sanchez_Another Chance_Full_Mix.mp3 -rwxr----- 1 tecmint tecmint 3018211 Jan 19 15:24 Principal Of Lust.3.08.mp3 -rwxr----- 1 tecmint tecmint 5688390 Jan 19 15:24 Please Forgive Me.MP3 -rwxr----- 1 tecmint tecmint 3381827 Jan 19 15:24 Obvious.mp3 -rwxr----- 1 tecmint tecmint 5499073 Jan 19 15:24 Namstey-London-Viraaniya.mp3 -rwxr----- 1 tecmint tecmint 3129210 Jan 19 15:24 MOS-Enya - Only Time (Pop Radio mix).m
2. 마지막 접근 시간을 기반으로 파일 목록
이 명령어는 마지막 접근 시간을 기반으로 디렉터리 내 파일을 목록化합니다. つまり 마지막으로 접근된 시간, 파일의 수정 시간이 아닌 것입니다.
# ls -ltu total 3084272 drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:24 Music drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 Linux-ISO drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 Music-Player drwx------ 3 tecmint tecmint 4096 Jan 19 15:22 tor-browser_en-US drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 bin drwxr-xr-x 11 tecmint tecmint 4096 Jan 19 15:22 Android Games drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 Songs drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 renamefiles drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 katoolin-master drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 Tricks drwxr-xr-x 3 tecmint tecmint 4096 Jan 19 15:22 Linux-Tricks drwxr-xr-x 6 tecmint tecmint 4096 Jan 19 15:22 tuptime drwxr-xr-x 4 tecmint tecmint 4096 Jan 19 15:22 xdm drwxr-xr-x 2 tecmint tecmint 20480 Jan 19 15:22 ffmpeg usage drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:22 xdm-helper
3. 마지막 수정 시간을 기반으로 파일 목록
이 명령어는 파일의 마지막 수정 시간을 기반으로 디렉터리를 목록化합니다. つまり 파일의 상태 정보의 마지막 수정 시간('ctime'
)을 기반으로 목록化합니다. 이 명령어는 最近 변경된 어떠한 상태 정보도 사용할 수 있습니다. 예를 들어 소유자, 그룹, 권한, 사이즈 등이 최근에 변경되었다면 그 파일을 먼저 목록화합니다.
# ls -ltc total 3084272 drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 15:24 Music drwxr-xr-x 2 tecmint tecmint 4096 Jan 19 13:05 img -rw------- 1 tecmint tecmint 262191 Jan 19 12:15 tecmint.jpeg drwxr-xr-x 5 tecmint tecmint 4096 Jan 19 10:57 Desktop drwxr-xr-x 7 tecmint tecmint 12288 Jan 18 16:00 Downloads drwxr-xr-x 13 tecmint tecmint 4096 Jan 18 15:36 VirtualBox VMs -rwxr-xr-x 1 tecmint tecmint 691 Jan 13 14:57 special.sh -rw-r--r-- 1 tecmint tecmint 654325 Jan 4 16:55 powertop-2.7.tar.gz.save -rw-r--r-- 1 tecmint tecmint 654329 Jan 4 11:17 filename.tar.gz drwxr-xr-x 3 tecmint tecmint 4096 Jan 4 11:04 powertop-2.7 -rw-r--r-- 1 tecmint tecmint 447795 Dec 31 14:22 Happy-New-Year-2016.jpg -rw-r--r-- 1 tecmint tecmint 12 Dec 18 18:46 ravi -rw-r--r-- 1 tecmint tecmint 1823 Dec 16 12:45 setuid.txt ...
위의 명령어에 '-a'
스위치를 사용하면 현재 디렉터리의 숨김 파일도 목록化和 정렬할 수 있습니다. 그리고 '-r'
스위치는 정렬 결과를 거꾸로 보여줍니다.
더 깊은 정렬을 해야 하는 경우, Output find 명령어의 기반으로 정렬하는 것도 가능합니다. 그러나 ls
를 사용할 수 있지만, 사용자가 원하는 다양한 필드가 출력되는 것을 위해 'sort'
가 더 유용합니다.
아래 명령어는 find
명령어와 함께 sort
를 사용하여 파일 목록을 날짜와 시간에 따라 정렬하는 방법을 보여줍니다.
find 명령어에 대해 더 알아보려면 다음 링크를 참고하세요: 리눅스에서 ‘find’ 명령어의 35가지 실용적인 예제
4. 월별로 파일 정렬
여기서는 루트 디렉토리 (‘/’)
에서 모든 파일을 찾은 다음 결과를 파일에 액세스한 월과 파일 이름으로 출력합니다. 이 전체 결과 중에서 상위 11개 항목을 나열합니다.
# find / -type f -printf "\n%Ab %p" | head -n 11 Dec /usr/lib/nvidia/pre-install Dec /usr/lib/libcpufreq.so.0.0.0 Apr /usr/lib/libchromeXvMCPro.so.1.0.0 Apr /usr/lib/libt1.so.5.1.2 Apr /usr/lib/libchromeXvMC.so.1.0.0 Apr /usr/lib/libcdr-0.0.so.0.0.15 Dec /usr/lib/msttcorefonts/update-ms-fonts Nov /usr/lib/ldscripts/elf32_x86_64.xr Nov /usr/lib/ldscripts/elf_i386.xbn Nov /usr/lib/ldscripts/i386linux.xn
아래 명령어는 출력을 첫 번째 필드로 지정된 키를 사용하여 정렬하고, 앞에 '-k1'
로 지정된대로 월을 기준으로 정렬합니다.
# find / -type f -printf "\n%Ab %p" | head -n 11 | sort -k1M Apr /usr/lib/libcdr-0.0.so.0.0.15 Apr /usr/lib/libchromeXvMCPro.so.1.0.0 Apr /usr/lib/libchromeXvMC.so.1.0.0 Apr /usr/lib/libt1.so.5.1.2 Nov /usr/lib/ldscripts/elf32_x86_64.xr Nov /usr/lib/ldscripts/elf_i386.xbn Nov /usr/lib/ldscripts/i386linux.xn Dec /usr/lib/libcpufreq.so.0.0.0 Dec /usr/lib/msttcorefonts/update-ms-fonts Dec /usr/lib/nvidia/pre-install
5. 날짜를 기준으로 파일 정렬
여기서도 루트 디렉토리에서 모든 파일을 찾기 위해 find
명령어를 사용하지만, 이제는 결과를 파일에 액세스한 마지막 날짜, 마지막 시간 및 파일 이름으로 출력합니다. 여기서 상위 11개 항목을 가져옵니다.
# find / -type f -printf "\n%AD %AT %p" | head -n 11 12/08/15 11:30:38.0000000000 /usr/lib/nvidia/pre-install 12/07/15 10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0 04/11/15 06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0 04/11/15 06:08:34.9939910430 /usr/lib/libt1.so.5.1.2 04/11/15 06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0 04/11/15 06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15 12/18/15 11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts 11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr 11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn 11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn
아래 정렬 명령어는 먼저 년도의 마지막 자릿수를 기준으로 정렬하고, 그 다음에는 월의 마지막 자릿수를 역순으로 정렬하고, 마지막으로 첫 번째 필드를 기준으로 정렬합니다. 여기서 ‘1.8’는 첫 번째 필드의 8번째 열을 의미하며, ‘n’은 숫자로 정렬을 의미하고, ‘r’은 역순으로 정렬함을 나타냅니다.
# find / -type f -printf "\n%AD %AT %p" | head -n 11 | sort -k1.8n -k1.1nr -k1 12/07/15 10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0 12/08/15 11:30:38.0000000000 /usr/lib/nvidia/pre-install 12/18/15 11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts 11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr 11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn 11/12/15 12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn 04/11/15 06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0 04/11/15 06:08:34.9939910430 /usr/lib/libt1.so.5.1.2 04/11/15 06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15 04/11/15 06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0
6. 시간을 기준으로 파일 정렬
다시 한번, find
명령어를 사용하여 루트 디렉토리에서 상위 11개 파일을 나열하고 결과를 “파일에 마지막으로 액세스한 시간”과 “파일 이름”의 형식으로 출력합니다.
# find / -type f -printf "\n%AT %p" | head -n 11 11:30:38.0000000000 /usr/lib/nvidia/pre-install 10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0 06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0 06:08:34.9939910430 /usr/lib/libt1.so.5.1.2 06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0 06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15 11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts 12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr 12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn 12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn
아래 명령은 출력을 첫 번째 열의 첫 번째 필드인 시간의 첫 번째 숫자에 따라 정렬합니다.
# find / -type f -printf "\n%AT %p" | head -n 11 | sort -k1.1n 06:08:34.9819910430 /usr/lib/libchromeXvMCPro.so.1.0.0 06:08:34.9939910430 /usr/lib/libt1.so.5.1.2 06:08:35.0099910420 /usr/lib/libcdr-0.0.so.0.0.15 06:08:35.0099910420 /usr/lib/libchromeXvMC.so.1.0.0 10:34:45.2694776230 /usr/lib/libcpufreq.so.0.0.0 11:19:25.2656728990 /usr/lib/msttcorefonts/update-ms-fonts 11:30:38.0000000000 /usr/lib/nvidia/pre-install 12:56:34.0000000000 /usr/lib/ldscripts/elf32_x86_64.xr 12:56:34.0000000000 /usr/lib/ldscripts/elf_i386.xbn 12:56:34.0000000000 /usr/lib/ldscripts/i386linux.xn
7. 날짜별로 ls -l의 출력을 정렬
이 명령은 'ls -l'
명령의 출력을 6번째 필드인 월과 7번째 필드인 날짜에 따라 수치적으로 정렬합니다.
# ls -l | sort -k6M -k7n total 116 -rw-r--r-- 1 root root 0 Oct 1 19:51 backup.tgz drwxr-xr-x 2 root root 4096 Oct 7 15:27 Desktop -rw-r--r-- 1 root root 15853 Oct 7 15:19 powertop_report.csv -rw-r--r-- 1 root root 79112 Oct 7 15:25 powertop.html -rw-r--r-- 1 root root 0 Oct 16 15:26 file3 -rw-r--r-- 1 root root 13 Oct 16 15:17 B -rw-r--r-- 1 root root 21 Oct 16 15:16 A -rw-r--r-- 1 root root 64 Oct 16 15:38 C
결론
마찬가지로, sort 명령어에 대한 일부 지식을 가지고 있다면, 거의 모든 필드와 원하는 열 기준으로 목록을 정렬할 수 있습니다. 이것은 날짜나 시간에 따라 파일을 정렬하는 데 도움이 되는 몇 가지 요령이었습니다. 이를 기반으로 자신만의 요령을 만들 수도 있습니다. 그러나 다른 흥미로운 요령이 있다면 언제든지 댓글에 언급할 수 있습니다.
Source:
https://www.tecmint.com/find-and-sort-files-modification-date-and-time-in-linux/