学习如何在Linux中查找和列出最近修改的文件


您是否正在处理成千上万的文件?您是否忘记了Linux中最近修改的文档?但知道您将这些文件保存在哪里了吗?如果是这样,本文将帮助您识别和列出Linux中最近修改的记录。

使用find命令

以下命令有助于按更新时间的反向顺序识别排序的文件:

$ find /etc -type f -printf '%TY-%Tm-%Td %TT %p
' | sort -r

示例输出应如下所示:

2016-12-22 11:10:18.2928833250 /etc/cups/subscriptions.conf
2016-12-22 11:09:18.1987314780 /etc/cups/subscriptions.conf.O
2016-12-20 10:38:47.1524556320 /etc/init.d/.depend.stop
2016-12-20 10:38:47.1524556320 /etc/init.d/.depend.start
2016-12-20 10:38:47.1524556320 /etc/init.d/.depend.boot
2016-12-19 13:28:14.3963910250 /etc/ld.so.cache
2016-12-19 13:28:05.8242856890 /etc/mailcap
2016-12-19 13:24:12.4493955340 /etc/apt/sources.list.d/rvm-ubuntu-smplayer-xenial.list
2016-12-19 13:24:12.4493955340 /etc/apt/sources.list.d/google-chrome.list
2016-12-19 13:24:12.4493955340 /etc/apt/sources.list
2016-12-19 13:24:12.4413954340 /etc/apt/trusted.gpg.d/rvm_ubuntu_smplayer.gpg
.....................................................................................

如果您想查找特定时间段内更改的文件,请使用以下命令:

$ find -cmin -5

以上命令将显示最后5分钟内更改的文件状态结果。示例输出应如下所示:

./.config/google-chrome
./.config/google-chrome/Local State
./.config/google-chrome/Default
./.config/google-chrome/Default/Cookies
./.config/google-chrome/Default/Service Worker/ScriptCache/index-dir
./.config/google-chrome/Default/Service Worker/ScriptCache/index-dir/the-real-index
./.config/google-chrome/Default/Current Session
./.config/google-chrome/Default/Cookies-journal
./.config/google-chrome/Default/Extension State/000006.log
./.config/google-chrome/Default/History-journal
./.config/google-chrome/Default/Local Storage/chrome-extension_gighmmpiobklfepjocnamgkkbiglidom_0.localstorage
./.config/google-chrome/Default/Local Storage/https_plus.google.com_0.localstorage-journal
./.config/google-chrome/Default/Local Storage/chrome-extension_gighmmpiobklfepjocnamgkkbiglidom_0.localstorage-journal
./.config/google-chrome/Default/Local Storage/https_plus.google.com_0.localstorage
./.config/google-chrome/Default/Preferences
./.config/google-chrome/Default/Local Extension Settings/gighmmpiobklfepjocnamgkkbiglidom/000085.log
./.config/google-chrome/Default/Session Storage
./.config/google-chrome/Default/Session Storage/LOG
./.config/google-chrome/Default/Session Storage/MANIFEST-000001
./.config/google-chrome/Default/Session Storage/000471.log
./.config/google-chrome/Default/Session Storage/000472.ldb
./.config/google-chrome/Default/Extension Activity
.........................................................................

使用ls命令

使用此命令,我们可以找到如下所示的最后更改的文件:

$ stat --printf="%y %n
" $(ls -tr $(find * -type f))

示例输出应如下所示:

2010-03-03 01:16:33.000000000 +0530 lohit_te.ttf
2016-12-16 17:50:29.791969567 +0530 examples.desktop
2016-12-19 10:43:35.372620496 +0530 Pictures/flash.png
2016-12-19 10:59:25.275474607 +0530 Pictures/flash1.png
2016-12-19 11:27:20.234186900 +0530 httpstat.py
2016-12-19 12:32:39.819805672 +0530 Downloads/ttf-telugu-fonts_0.5.11ubuntu1_all.deb
2016-12-19 13:29:33.365359204 +0530 Pictures/smplayer.png
2016-12-19 13:53:39.907705833 +0530 Downloads/girl.jpg
2016-12-20 11:32:39.271477881 +0530 Downloads/200x200.jpg
2016-12-20 12:25:55.015482441 +0530 Downloads/hockey.jpg
2016-12-20 13:56:19.596432236 +0530 Downloads/flash-player-ubuntu.jpg
2016-12-20 14:40:41.191776867 +0530 Downloads/jabrdasth.jpg
2016-12-22 10:22:42.529763492 +0530 Videos/httpstat.py
2016-12-22 10:32:30.249527866 +0530 abc
2016-12-22 10:34:46.671286096 +0530 Music/httpstat.py
2016-12-22 10:34:46.675286130 +0530 Desktop/httpstat.py
2016-12-22 12:04:14.713113283 +0530 Downloads/SMPlayer.jpg
2016-12-22 12:05:19.310933827 +0530 Downloads/rsz_smplayer.jpg

要获取已更改目录的列表,请使用以下命令:

$ ls -ltc

示例输出应如下所示:

drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 22 12:05 Downloads
drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 22 10:34 Desktop
drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 22 10:34 Music
-rw-rw-r-- 1 tutorialspoint tutorialspoint 0 Dec 22 10:32 abc
drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 22 10:22 Videos
-rw-rw-r-- 1 tutorialspoint tutorialspoint 155368 Dec 19 14:55 lohit_te.ttf
drwxr-xr-x 3 tutorialspoint tutorialspoint 4096 Dec 19 13:29 Pictures
-rw-rw-r-- 1 tutorialspoint tutorialspoint 10577 Dec 19 11:27 httpstat.py
drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 16 19:03 Documents
drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 16 19:03 Public
drwxr-xr-x 2 tutorialspoint tutorialspoint 4096 Dec 16 19:03 Templates
-rw-r--r-- 1 tutorialspoint tutorialspoint 8980 Dec 16 17:50 examples.desktop

使用Python命令

要使用Python获取Linux中最近修改的文件列表,请使用以下命令:

$ find . -type f -print |
> python -c 'import os, sys; times = {}
> for f in sys.stdin.readlines(): f = f[0:-1]; times[f] = os.stat(f).st_mtime
> for f in sorted(times.iterkeys(), key=lambda f:times[f]): print f'

示例输出应如下所示:

./.cache/google-chrome/Default/Cache/28b4d5c73382c76e_0
./.cache/google-chrome/Default/Cache/47603dda53f4eff9_0
./.cache/google-chrome/Default/Cache/628cf2914597e226_0
./.cache/google-chrome/Default/Cache/6abab0fdc6e1b757_0
./.cache/google-chrome/Default/Cache/edc5a146f72fc62a_1
./.cache/google-chrome/Default/Cache/e21f7099ce14a183_0
./.cache/google-chrome/Default/Cache/954cd78918e0ef94_1
./.cache/google-chrome/Default/Cache/66966ca01a40cbea_1
./.cache/google-chrome/Default/Cache/e53fd0655371a5dd_0
./.cache/google-chrome/Default/Cache/e81e30e428358e80_0
./.cache/google-chrome/Default/Cache/787db5079efa9083_0
./.cache/google-chrome/Default/Cache/17340f80011e77a5_1
./.cache/google-chrome/Default/Cache/a82652595e2c24a1_1
./.cache/google-chrome/Default/Cache/3061c4bd90e582ca_1
./.cache/google-chrome/Default/Cache/5dc7329ed9c947e5_1
./.cache/google-chrome/Default/Cache/54346d971accf6ee_1
..............................................................................................

阅读本文后,您将能够学习如何在Linux中查找和列出最近修改的文件。在我们的下一篇文章中,我们将提供更多基于Linux的技巧和提示。继续关注!

更新于:2019年10月22日

270 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告