Linux에서 수정 날짜 및 시간을 기준으로 파일을 찾고 정렬하는 방법


일반적으로 우리는 시스템에 파일 형태로 많은 정보를 저장하는 습관이 있습니다. 일부 숨겨진 파일, 일부는 이해하기 쉽도록 별도의 폴더에 보관하고 일부는있는 그대로입니다. "그러나이 모든 것들이 우리의 디렉토리를 채 웁니다. "일반적으로 바탕 화면을 엉망으로 만듭니다. 그러나이 거대한 컬렉션에서 특정 날짜와 시간에 수정 된 특정 파일을 검색해야 할 때 문제가 발생합니다.

GUI에 익숙한 사람들은 긴 목록 형식으로 파일을 나열하는 파일 관리자를 사용하여 찾을 수 있으므로 원하는 것을 쉽게 파악할 수 있지만 검은 색 화면을 자주 사용하는 사용자 또는 GUI가없는 서버에서 작업하는 사람은 검색을 쉽게 할 수있는 간단한 명령 또는 명령 집합을 원합니다.

Linux에는 개별적으로 또는 함께 사용하면 파일을 검색하거나 이름, 수정 날짜, 생성 시간 또는 심지어는 파일 모음을 정렬하는 데 도움이 될 수있는 명령 모음이 있습니다. 결과를 얻기 위해 적용 할 수있는 필터입니다.

여기서는 파일을 정렬하거나 날짜 및 시간별로 파일 목록을 정렬하는 데 도움이되는 일련의 명령을 검사하여 Linux의 진정한 강점을 보여줄 것입니다.

Linux에서 파일을 정렬하는 Linux 유틸리티

날짜 및 시간을 기준으로 디렉토리를 정렬하는 데 충분한 일부 기본 Linux 명령 줄 유틸리티는 다음과 같습니다.

ls – 디렉토리 내용을 나열하는이 유틸리티는 파일과 디렉토리를 나열 할 수 있으며 수정 날짜 및 시간 또는 액세스, 권한, 크기, 소유자, 그룹 등을 포함하여 모든 상태 정보를 나열 할 수도 있습니다.

Linux ls 명령 및 정렬 명령에 대한 많은 기사를 이미 다루었습니다. 아래에서 찾을 수 있습니다.

  1. Learn ls Command with 15 Basic Examples
  2. Learn 7 Advance ls Commands with Examples
  3. 15 Useful Interview Questions on ls Command in Linux

sort –이 명령은 필드 또는 필드의 특정 열을 기준으로 검색 결과를 정렬하는 데 사용할 수 있습니다.

Linux 정렬 명령에 대한 두 개의 기사를 이미 다루었습니다. 아래에서 찾을 수 있습니다.

  1. 14 Linux ‘sort’ Command Examples – Part 1
  2. 7 Useful Linux ‘sort’ Command Examples – Part 2

이러한 명령은 검은 화면에서 작업하고 원하는 파일을 얻기 위해 많은 파일을 처리해야하는 경우 마스터 할 수있는 매우 강력한 명령입니다.

날짜 및 시간을 사용하여 파일을 정렬하는 몇 가지 방법

다음은 날짜 및 시간을 기준으로 정렬 할 명령 목록입니다.

아래 명령은 긴 목록 형식으로 파일을 나열하고 수정 시간을 기준으로 파일을 최신순으로 정렬합니다. 역순으로 정렬하려면이 명령과 함께 '-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

마지막 액세스 시간을 기준으로, 즉 파일이 수정되지 않고 마지막으로 액세스 된 시간을 기준으로 디렉토리에있는 파일 목록.

# 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

파일 상태 정보의 마지막 수정 시간 또는 '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' 스위치는 출력을 반대로 나열합니다. "주문.

찾기 명령의 출력 정렬과 같은보다 심층적 인 정렬을 위해 ls 도 사용할 수 있지만 출력이 없을 수 있으므로 'sort'가 더 도움이됩니다. 파일 이름 만 있지만 사용자가 원하는 필드.

아래 명령은 find 명령과 함께 sort 를 사용하여 날짜 및 시간을 기준으로 파일 목록을 정렬하는 방법을 보여줍니다.

find 명령에 대한 자세한 내용은 다음 링크를 참조하십시오. Linux에서 'find'명령의 실제 예 35

여기서는 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

여기서 다시 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

여기서 다시 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

이 명령은 월 6 번째 필드를 기준으로 'ls -l' 명령의 출력을 정렬 한 다음 숫자로 된 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

결론

마찬가지로 정렬 명령에 대한 지식이 있으면 모든 필드와 원하는 열을 기준으로 거의 모든 목록을 정렬 할 수 있습니다. 날짜 또는 시간을 기준으로 파일을 정렬하는 데 도움이되는 몇 가지 트릭입니다. "이를 바탕으로 자신 만의 트릭을 만들 수 있습니다. "그러나 다른 흥미로운 트릭이 있으면 언제든지 댓글에 언급 할 수 있습니다.