Linux에서 수정 날짜 및 시간을 기준으로 파일을 찾고 정렬하는 방법
일반적으로 우리는 시스템에 많은 정보를 파일 형식으로 저장하는 습관이 있습니다. 일부는 숨겨진 파일이고 일부는 이해하기 쉽도록 별도의 폴더에 보관되어 있지만 일부는 그대로 유지됩니다. 그러나 이 모든 것들이 우리 디렉토리를 가득 채웁니다. 일반적으로 데스크탑을 사용하면 지저분해 보입니다. 그러나 이 거대한 컬렉션에서 특정 날짜와 시간에 수정된 특정 파일을 검색해야 할 때 문제가 발생합니다.
GUI에 익숙한 사람들은 긴 목록 형식으로 파일을 나열하는 파일 관리자를 사용하여 우리가 원하는 것을 쉽게 찾을 수 있지만, 검은 화면이 습관이 있는 사용자나 GUI가 없는 서버에서 작업하는 사람은 GUI를 사용하지 않을 것입니다. 검색을 쉽게 할 수 있는 간단한 명령이나 명령 세트를 원합니다.
Linux의 진정한 아름다움은 여기에서 보여줍니다. Linux에는 별도로 또는 함께 사용하면 파일을 검색하거나 이름, 수정 날짜, 생성 시간 또는 심지어는 다른 이름에 따라 파일 모음을 정렬하는 데 도움이 될 수 있는 명령 모음이 있기 때문입니다. 필터를 적용하면 결과를 얻을 수 있다고 생각할 수 있습니다.
여기에서는 날짜 및 시간을 기준으로 파일 또는 파일 목록을 정렬하는 데 도움이 되는 일련의 명령을 검토하여 Linux의 진정한 강점을 공개합니다.
Linux에서 파일을 정렬하는 Linux 유틸리티
날짜 및 시간을 기준으로 디렉터리를 정렬하는 데 충분한 몇 가지 기본 Linux 명령줄 유틸리티는 다음과 같습니다.
ls 명령
ls – 디렉토리 내용을 나열하는 이 유틸리티는 파일과 디렉토리를 나열할 수 있으며 수정 또는 액세스 날짜 및 시간, 권한, 크기, 소유자, 그룹 등을 포함한 모든 상태 정보도 나열할 수 있습니다. .
우리는 이미 Linux ls 명령 및 sort 명령에 관한 많은 기사를 다루었습니다. 아래에서 해당 기사를 찾을 수 있습니다.
- 15가지 기본 예제로 ls 명령 배우기
- 예제를 통해 7가지 고급 ls 명령 알아보기
- Linux의 ls 명령에 대한 15가지 유용한 인터뷰 질문
정렬 명령
정렬 – 이 명령을 사용하면 검색 결과를 필드 또는 필드의 특정 열별로 정렬할 수 있습니다.
우리는 이미 Linux 정렬 명령에 관한 두 가지 기사를 다루었습니다. 아래에서 해당 기사를 찾을 수 있습니다.
- 14 Linux 'sort' 명령 예 - 1부
- 7가지 유용한 Linux 'sort' 명령 예 - 2부
이 명령은 그 자체로 검은 화면에서 작업하고 원하는 파일을 얻기 위해 많은 파일을 처리해야 하는 경우 마스터해야 할 매우 강력한 명령입니다.
날짜와 시간을 사용하여 파일을 정렬하는 몇 가지 방법
다음은 날짜 및 시간을 기준으로 정렬하는 명령 목록입니다.
1. 수정 시간을 기준으로 파일 나열
아래 명령은 긴 목록 형식으로 파일을 나열하고 수정 시간을 기준으로 최신 항목부터 파일을 정렬합니다. 역순으로 정렬하려면 이 명령과 함께 '-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'
스위치는 출력을 역순으로 나열합니다. 주문하다.
find 명령의 출력에 대한 정렬과 같은 보다 심층적인 정렬을 위해 ls
도 사용할 수 있지만 'sort가 있습니다. '
는 출력에 파일 이름뿐만 아니라 사용자가 원하는 필드도 포함될 수 있으므로 더 유용하다는 것이 입증되었습니다.
아래 명령은 날짜 및 시간을 기준으로 파일 목록을 정렬하기 위해 find
명령과 함께 sort
를 사용하는 방법을 보여줍니다.
find 명령에 대해 자세히 알아보려면 다음 링크를 따르십시오. Linux에서 'find' 명령의 35가지 실제 사례
4. 월 기준으로 파일 정렬하기
여기서는 find
명령을 사용하여 루트 ('/')
디렉터리에 있는 모든 파일을 찾은 다음 결과를 다음과 같이 인쇄합니다. 월 어느 파일에 있는지 액세스한 다음 파일 이름을 지정했습니다. 전체 결과 중 상위 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'
로 지정된 키를 첫 번째 필드로 사용하여 출력을 정렬한 다음 그 앞에 있는 'M'
로 지정된 대로 월을 기준으로 정렬합니다.
# 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 명령에 대한 지식이 있으면 모든 필드와 심지어 원하는 열을 기준으로 거의 모든 목록을 정렬할 수 있습니다. 날짜 또는 시간을 기준으로 파일을 정렬하는 데 도움이 되는 몇 가지 요령은 다음과 같습니다. 이를 기반으로 자신만의 트릭을 구축할 수 있습니다. 하지만 다른 흥미로운 트릭이 있다면 언제든지 댓글에 언급해 주세요.