在 Linux 系统上,如果想在某个目录中查找最近被修改过的所有文件的列表,则可以使用 find 命令轻松实现。
在 Linux 中 find 命令可根据时间戳、文件类型和文件权限等各种条件来搜索或定位文件。在本文中,我们将介绍 13 个基于时间戳定位文件的示例。
在 Linux 中,一个文件包含三个时间戳,这些时间戳会在文件被访问、修改或替换时更新。
文件时间戳的类型:
- atime (Access time):显示最近访问文件的时间
- mtime (Modify time):显示最近修改文件内容的时间
- ctime (Change time):显示最近文件有所改变的状态,如文件修改,属性\属主改变,节点,链接变化等
查看文件的所有时间戳?
可以使用“stat”命令查看文件的时间戳,该命令将显示文件的如上所述的三个时间戳。
常用语法如下:
stat [File_Name]
# stat /home/daygeek/shell-script/remote-backup.sh
File: /home/daygeek/shell-script/remote-backup.sh
Size: 436 Blocks: 8 IO Block: 4096 regular file
Device: 10301h/66305d Inode: 2490772 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/ daygeek) Gid: ( 1000/ daygeek)
Access: 2019-09-08 01:00:20.963127924 +0530
Modify: 2019-09-08 01:04:56.274279514 +0530
Change: 2019-09-08 01:04:56.274279514 +0530
Birth: 2019-09-08 01:00:20.963127924 +0530
我们可以将数字参数与“mtime”一起使用。使用“-mtime n”命令返回最后一次修改的文件列表。
- +n:大于 n
- -n:小于 n
- n:正好 n
请参阅下面的格式以获得更好的理解。
- -mtime +10:查找 10 天前修改的所有文件。
- -mtime -10:查找最近 10 天内修改过的所有文件。
- -mtime 10:查找恰好在 10 天前修改的所有文件。
1) 根据时间对文件和文件夹进行排序
可以通过使用 ls 命令来对文件和文件夹进行排序,如下所示,它根据转换时间以相反的顺序对文件和文件夹进行排序。
# ls -ltrh /home/daygeek/shell-script
total 312K
-rw-r--r-- 1 daygeek daygeek 616 Jun 2 13:25 cpu-alert2.sh
-rw-r--r-- 1 daygeek daygeek 830 Jun 14 11:54 process.sh
-rw-r--r-- 1 daygeek daygeek 776 Jun 14 12:00 memory-monitor.sh
-rw-r--r-- 1 daygeek daygeek 157 Jul 29 18:29 service1.sh
-rw-r--r-- 1 daygeek daygeek 361 Jul 29 19:45 service3a.sh
-rw-r--r-- 1 daygeek daygeek 296 Aug 4 01:27 servicem.sh
drw-r--r-- 3 daygeek daygeek 4.0K Aug 19 12:33 backup
-rwxr-xr-x 1 daygeek daygeek 73 Aug 23 12:51 guna_server.sh
-rw-r--r-- 1 daygeek daygeek 436 Sep 8 01:04 remote-backup.sh
-rw-r--r-- 1 daygeek daygeek 367 Sep 8 01:19 remote-backup-2.sh
-rw-r--r-- 1 daygeek daygeek 418 Sep 8 03:54 remote-backup-1.sh
-rw-r--r-- 1 daygeek daygeek 459 Sep 8 04:21 list-remote-backup.sh
drwxr-xr-x 2 daygeek daygeek 4.0K Sep 20 12:53 test
drwxr-xr-x 2 daygeek daygeek 4.0K Sep 20 12:54 test1
drwxr-xr-x 2 daygeek daygeek 4.0K Sep 20 12:54 test3
2) 根据转换时间仅对文件夹进行排序
使用以下格式根据转换时间以相反的顺序对文件夹进行排序。
# ls -ltrh /home/daygeek/shell-script | grep ^d
drw-r--r-- 3 daygeek daygeek 4.0K Aug 19 12:33 backup
drwxr-xr-x 2 daygeek daygeek 4.0K Sep 20 12:53 test
drwxr-xr-x 2 daygeek daygeek 4.0K Sep 20 12:54 test1
drwxr-xr-x 2 daygeek daygeek 4.0K Sep 20 12:54 test3
3) 查找 120 天前修改过的文件
下面的 find 命令将显示 120 天前更改的文件列表。
# find /home/daygeek/shell-script -type f -mtime +120 -ls
2491042 4 -rw-r--r-- 1 daygeek daygeek 410 Apr 27 23:58 /home/daygeek/shell-script/disk-space-alert.sh
1705245 4 -rwxr-xr-x 1 daygeek daygeek 41 Jan 15 2019 /home/daygeek/shell-script/ovh.sh
1704244 4 -rw-r--r-- 1 daygeek daygeek 99 Jan 19 2019 /home/daygeek/shell-script/file-copy.sh
1744131 4 -rwxr-xr-x 1 daygeek daygeek 1361 Feb 9 2019 /home/daygeek/shell-script/cpu-mem-check.sh
1719630 4 -rwxr-xr-x 1 daygeek daygeek 82 Mar 1 2019 /home/daygeek/shell-script/pass-gen.sh
1719891 4 -rwxr-xr-x 1 daygeek daygeek 274 Mar 16 2019 /home/daygeek/shell-script/pass-multi-user.sh
2490973 4 -rwxr-xr-x 1 daygeek daygeek 237 Apr 6 10:40 /home/daygeek/shell-script/mysql_backup_1.sh
2490935 0 -rw-r--r-- 1 daygeek daygeek 0 Apr 29 10:25 /home/daygeek/shell-script/test.txt
1736513 4 -rw-r--r-- 1 daygeek daygeek 227 Mar 17 2019 /home/daygeek/shell-script/server-list.txt
2490942 8 -rw-r--r-- 1 daygeek daygeek 4780 Apr 11 14:57 /home/daygeek/shell-script/2g.out
1738172 4 -rwxr-xr-x 1 daygeek daygeek 214 Mar 10 2019 /home/daygeek/shell-script/user-add.sh
1719571 4 -rwxr-xr-x 1 daygeek daygeek 151 Mar 16 2019 /home/daygeek/shell-script/pass-up.sh
2491095 140 -rwxr-xr-x 1 daygeek daygeek 139406 May 12 20:26 /home/daygeek/shell-script/xsos
1720392 4 -rwxr-xr-x 1 daygeek daygeek 76 Mar 17 2019 /home/daygeek/shell-script/port_scan.sh
1730638 4 -rwxr-xr-x 1 daygeek daygeek 98 Jan 11 2019 /home/daygeek/shell-script/passwd-up1.sh
2490982 4 -rwxr-xr-x 1 daygeek daygeek 241 Apr 6 10:49 /home/daygeek/shell-script/mysql_backup_2.sh
2490955 4 -rwxr-xr-x 1 daygeek daygeek 761 Apr 6 21:36 /home/daygeek/shell-script/mysql_backup.sh
1717834 4 -rwxr-xr-x 1 daygeek daygeek 159 Jan 11 2019 /home/daygeek/shell-script/passwd-up.sh
2491025 4 -rw-r--r-- 1 daygeek daygeek 78 Apr 26 11:39 /home/daygeek/shell-script/bulk-package-install.sh
4) 查找最近 15 天内修改过的文件
下面的 find 命令将显示过去 15 天内更改的文件列表:
# find /home/daygeek/shell-script -type f -mtime -15 -ls
2490782 4 -rw-r--r-- 1 daygeek daygeek 418 Sep 8 03:54 /home/daygeek/shell-script/remote-backup-1.sh
2494508 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 20 12:54 /home/daygeek/shell-script/test3/remote-backup.sh
2490783 4 -rw-r--r-- 1 daygeek daygeek 538 Sep 20 12:53 /home/daygeek/shell-script/test/cpu-alert.sh
2490772 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 8 01:04 /home/daygeek/shell-script/remote-backup.sh
2494505 4 -rw-r--r-- 1 daygeek daygeek 776 Sep 20 12:54 /home/daygeek/shell-script/test1/memory-monitor.sh
2490786 4 -rw-r--r-- 1 daygeek daygeek 367 Sep 8 01:19 /home/daygeek/shell-script/remote-backup-2.sh
2490789 4 -rw-r--r-- 1 daygeek daygeek 459 Sep 8 04:21 /home/daygeek/shell-script/list-remote-backup.sh
5) 查找恰好 10 天前修改过的文件
下面的 find 命令将显示恰好 10 天前更改的文件列表:
# find /home/daygeek/shell-script -type f -mtime 10 -ls
2490782 4 -rw-r--r-- 1 daygeek daygeek 418 Sep 8 03:54 /home/daygeek/shell-script/remote-backup-1.sh
2490772 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 8 01:04 /home/daygeek/shell-script/remote-backup.sh
2490786 4 -rw-r--r-- 1 daygeek daygeek 367 Sep 8 01:19 /home/daygeek/shell-script/remote-backup-2.sh
2490789 4 -rw-r--r-- 1 daygeek daygeek 459 Sep 8 04:21 /home/daygeek/shell-script/list-remote-backup.sh
6) 查找最近 30 分钟内修改过的文件
下面的 find 命令将显示在过去 30 分钟内发生更改的文件列表。
# find /home/daygeek/ -type d -mmin -30 -ls
6553934 4 drwx------ 2 daygeek daygeek 4096 Sep 20 14:08 /home/daygeek/.cache/google-chrome/Default/Cache/index-dir
1974320 4 drwxr-xr-x 3 daygeek daygeek 4096 Sep 20 14:06 /home/daygeek/.config/Notepadqq/backupCache
1974321 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 14:06 /home/daygeek/.config/Notepadqq/backupCache/window_94904345125616
6553884 4 drwx------ 19 daygeek daygeek 4096 Sep 20 14:03 /home/daygeek/.config/google-chrome
6553899 4 drwx------ 26 daygeek daygeek 4096 Sep 20 14:07 /home/daygeek/.config/google-chrome/Default
6553923 4 drwx------ 2 daygeek daygeek 4096 Sep 20 13:50 /home/daygeek/.config/google-chrome/Default/Sync\ Data/LevelDB
6823938 4 drwx------ 2 daygeek daygeek 4096 Sep 20 14:03 /home/daygeek/.config/google-chrome/Default/databases/https_translate.google.co.in_0
6553915 4 drwx------ 2 daygeek daygeek 4096 Sep 20 13:54 /home/daygeek/.config/google-chrome/Safe\ Browsing
1704068 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 14:05 /home/daygeek/.local/share/tracker/data
1704089 4 drwx------ 2 daygeek daygeek 4096 Sep 20 14:04 /home/daygeek/.local/share/gvfs-metadata
1703995 4 drwx------ 3 daygeek daygeek 4096 Sep 20 14:05 /home/daygeek/.local/share/gnome-shell
7) 查找最近 5 天修改过的文件夹
此命令仅显示最近 5 天内修改的文件夹。
# find /home/daygeek/shell-script -type d -mtime -5 -ls
2494645 4 drwxr-xr-x 6 daygeek daygeek 4096 Sep 20 12:55 /home/daygeek/shell-script
2498566 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:54 /home/daygeek/shell-script/test3
2498564 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:53 /home/daygeek/shell-script/test
2498565 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:54 /home/daygeek/shell-script/test1
8) 查找最近 15 天内修改的文件和文件夹
此命令显示过去 15 天内修改的文件和文件夹的列表:
# find /home/daygeek/shell-script -mtime -15 -ls
2494645 4 drwxr-xr-x 6 daygeek daygeek 4096 Sep 20 12:55 /home/daygeek/shell-script
2490782 4 -rw-r--r-- 1 daygeek daygeek 418 Sep 8 03:54 /home/daygeek/shell-script/remote-backup-1.sh
2498566 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:54 /home/daygeek/shell-script/test3
2494508 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 20 12:54 /home/daygeek/shell-script/test3/remote-backup.sh
2498564 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:53 /home/daygeek/shell-script/test
2490783 4 -rw-r--r-- 1 daygeek daygeek 538 Sep 20 12:53 /home/daygeek/shell-script/test/cpu-alert.sh
2490772 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 8 01:04 /home/daygeek/shell-script/remote-backup.sh
2498565 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:54 /home/daygeek/shell-script/test1
2494505 4 -rw-r--r-- 1 daygeek daygeek 776 Sep 20 12:54 /home/daygeek/shell-script/test1/memory-monitor.sh
2490786 4 -rw-r--r-- 1 daygeek daygeek 367 Sep 8 01:19 /home/daygeek/shell-script/remote-backup-2.sh
2490789 4 -rw-r--r-- 1 daygeek daygeek 459 Sep 8 04:21 /home/daygeek/shell-script/list-remote-backup.sh
9) 查找从给定日期到最新日期修改过的文件和文件夹
此命令允许您查找从给定日期到最新日期已修改的文件和文件夹的列表:
# find /home/daygeek/shell-script -newermt "2019-09-08" -ls
2494645 4 drwxr-xr-x 6 daygeek daygeek 4096 Sep 20 12:55 /home/daygeek/shell-script
2490782 4 -rw-r--r-- 1 daygeek daygeek 418 Sep 8 03:54 /home/daygeek/shell-script/remote-backup-1.sh
2498566 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:54 /home/daygeek/shell-script/test3
2494508 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 20 12:54 /home/daygeek/shell-script/test3/remote-backup.sh
2498564 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:53 /home/daygeek/shell-script/test
2490783 4 -rw-r--r-- 1 daygeek daygeek 538 Sep 20 12:53 /home/daygeek/shell-script/test/cpu-alert.sh
2490772 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 8 01:04 /home/daygeek/shell-script/remote-backup.sh
2498565 4 drwxr-xr-x 2 daygeek daygeek 4096 Sep 20 12:54 /home/daygeek/shell-script/test1
2494505 4 -rw-r--r-- 1 daygeek daygeek 776 Sep 20 12:54 /home/daygeek/shell-script/test1/memory-monitor.sh
2490786 4 -rw-r--r-- 1 daygeek daygeek 367 Sep 8 01:19 /home/daygeek/shell-script/remote-backup-2.sh
2490789 4 -rw-r--r-- 1 daygeek daygeek 459 Sep 8 04:21 /home/daygeek/shell-script/list-remote-backup.sh
10) 查找最近 24 小时内修改的所有文件和文件夹
可以使用如下所示的格式来查找在过去 24 小时内发生更改的文件和文件夹。
# find /home/daygeek -newermt "1 day ago" -ls
or
# find /home/daygeek -newermt "-24 hours" -ls
or
# find /home/daygeek -newermt "yesterday" -ls
1704781 36 -rw------- 1 daygeek daygeek 35943 Sep 20 12:13 /home/daygeek/.local/share/keyrings/login.keyring
1705911 8 -rw-r--r-- 1 daygeek daygeek 4548 Sep 20 12:15 /home/daygeek/Remove-Symbolic-Links-in-Linux.txt
1703977 56 drwxr-xr-x 14 daygeek daygeek 53248 Sep 20 08:04 /home/daygeek/Downloads
1707507 36 -rw-r--r-- 1 daygeek daygeek 36092 Sep 20 08:04 /home/daygeek/Downloads/picture-networking-9.png
2490860 20 drwxr-xr-x 4 daygeek daygeek 20480 Sep 20 08:18 /home/daygeek/Desktop/Daily_Activity/2g-images/Designed_Image
2490796 72 -rw-r--r-- 1 daygeek daygeek 71000 Sep 20 08:18 /home/daygeek/Desktop/Daily_Activity/2g-images/Designed_Image/how-to-remove-delete-symbolic-links-in-linux-1.png
1966609 188 -rw-r--r-- 1 daygeek daygeek 192110 Sep 20 12:39 /home/daygeek/Desktop/Daily_Activity/magi.txt
1709893 36 -rw-r--r-- 1 daygeek daygeek 34631 Sep 20 07:36 /home/daygeek/old-Mysqladmin-command.txt
1710619 40 drwxr-xr-x 2 daygeek daygeek 36864 Sep 20 12:35 /home/daygeek/Pictures/BingWallpaper
1710658 340 -rw-r--r-- 1 daygeek daygeek 348014 Sep 20 12:35 /home/daygeek/Pictures/BingWallpaper/20190920-th?id=OHR.ThePando_EN-US1263045290_1920x1080.jpg
11) 查找最近 30 天内访问的“sh”扩展名文件列表
此命令可帮助查找在过去 30 天内访问的带有“sh”扩展名的文件列表。
# find /home/daygeek/shell-script -type f -iname "*.sh" -atime -30 -ls
2490782 4 -rw-r--r-- 1 daygeek daygeek 418 Sep 8 03:54 /home/daygeek/shell-script/remote-backup-1.sh
2494508 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 20 12:54 /home/daygeek/shell-script/test3/remote-backup.sh
2490783 4 -rw-r--r-- 1 daygeek daygeek 538 Sep 20 12:53 /home/daygeek/shell-script/test/cpu-alert.sh
2490772 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 8 01:04 /home/daygeek/shell-script/remote-backup.sh
2494505 4 -rw-r--r-- 1 daygeek daygeek 776 Sep 20 12:54 /home/daygeek/shell-script/test1/memory-monitor.sh
2490786 4 -rw-r--r-- 1 daygeek daygeek 367 Sep 8 01:19 /home/daygeek/shell-script/remote-backup-2.sh
2490789 4 -rw-r--r-- 1 daygeek daygeek 459 Sep 8 04:21 /home/daygeek/shell-script/list-remote-backup.sh
12) 查找一段时间内修改过的文件
下面的命令显示了过去 20 分钟内更改的文件列表。
# find /home/daygeek -cmin -20 -ls
6595804 11440 -rw-r--r-- 1 daygeek daygeek 11713819 Sep 20 13:24 /home/daygeek/.config/google-chrome/Safe\ Browsing/UrlSoceng.store
6586612 12 -rw-r--r-- 1 daygeek daygeek 11171 Sep 20 13:24 /home/daygeek/.config/google-chrome/Safe\ Browsing/UrlSuspiciousSite.store
6595504 216 -rw-r--r-- 1 daygeek daygeek 219195 Sep 20 13:24 /home/daygeek/.config/google-chrome/Safe\ Browsing/UrlUws.store
6553893 28 -rw------- 1 daygeek daygeek 270336 Sep 20 13:32 /home/daygeek/.config/google-chrome/ShaderCache/GPUCache/data_1
6595827 88 -rw------- 1 daygeek daygeek 87328 Sep 20 13:20 /home/daygeek/.config/google-chrome/Local\ State
2490785 104 -rw-r--r-- 1 daygeek daygeek 105595 Sep 20 13:30 /home/daygeek/.local/share/fish/fish_history
1703995 4 drwx------ 3 daygeek daygeek 4096 Sep 20 13:33 /home/daygeek/.local/share/gnome-shell
1704389 4 -rw-r--r-- 1 daygeek daygeek 1258 Sep 20 13:33 /home/daygeek/.local/share/gnome-shell/application_state
1707425 16 -rw------- 1 daygeek daygeek 14108 Sep 18 12:52 /home/daygeek/.bash_history
13) 查找今天创建的文件列表
此命令可查找今天创建的文件列表:
# find /home/daygeek/shell-script -type f -ctime -1 -ls
2494508 4 -rw-r--r-- 1 daygeek daygeek 436 Sep 20 12:54 /home/daygeek/shell-script/test3/remote-backup.sh
2490783 4 -rw-r--r-- 1 daygeek daygeek 538 Sep 20 12:53 /home/daygeek/shell-script/test/cpu-alert.sh
2494505 4 -rw-r--r-- 1 daygeek daygeek 776 Sep 20 12:54 /home/daygeek/shell-script/test1/memory-monitor.sh
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/how-to-find-recently-modified-files-in-linux/