在使用计算机时,我们习惯在计算机中保存很多信息,例如文件,文件夹等。通常,桌面看起来很乱,但是当用户想在特定日期或时间搜索修改后的文件时,就会出现问题。Linux中有一些简单的命令可用来搜索修改后的文件。本文介绍“如何在Linux中基于修改日期和时间查找和排序文件”。
list命令显示文件,目录,有关修改或访问的日期和时间,权限,大小,所有者,组等的信息的列表。下面的命令显示文件列表以及格式,根据修改时间和最新性对文件进行排序首先归档。
$ ls -lt
样本输出应如下所示–
total 322428 drwxr-xr-x 3 linux linux 4096 Mar 8 13:59 Downloads drwxr-xr-x 6 linux linux 4096 Mar 3 14:34 Desktop lrwxrwxrwx 1 linux linux 37 Feb 27 13:25 PlayOnLinux's virtual drives -> /home/linux/.PlayOnLinux//wineprefix/ -rw-r--r-- 1 root root 70706 Feb 23 14:52 Selection_007.png -rw-r--r-- 1 root root 108159 Feb 23 14:49 root@linux: ~_005.png -rw-r--r-- 1 root root 145629 Feb 23 14:47 Workspace 1_004.png drwxr-xr-x 2 linux linux 4096 Feb 23 14:30 Pictures -rw-rw-r-- 1 linux linux 87631 Feb 19 14:08 account.png -rw-rw-r-- 1 linux linux 72172 Feb 19 14:07 network.png -rw-rw-r-- 1 linux linux 98362 Feb 19 14:05 sample1.png drwxr-xr-x 8 root root 4096 Feb 19 11:38 linux-dash drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Documents drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Music drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Public drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Templates drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Videos -rw-r--r-- 1 linux linux 8980 Feb 19 10:55 examples.desktop
要获取基于上次访问时间的所有文件的列表,请使用以下命令-
$ ls -ltu
样本输出应如下所示–
total 322428 drwxr-xr-x 3 linux linux 4096 Mar 8 14:00 Downloads drwxr-xr-x 8 root root 4096 Mar 8 11:18 linux-dash -rw-r--r-- 1 linux linux 8980 Mar 8 11:18 examples.desktop -rw-r--r-- 1 root root 70706 Mar 8 11:18 Selection_007.png -rw-r--r-- 1 root root 108159 Mar 8 11:18 root@linux: ~_005.png -rw-r--r-- 1 root root 145629 Mar 8 11:18 Workspace 1_004.png lrwxrwxrwx 1 linux linux 37 Mar 8 11:18 PlayOnLinux's virtual drives -> /home/linux/.PlayOnLinux//wineprefix/ drwxr-xr-x 2 linux linux 4096 Mar 8 11:18 Public drwxr-xr-x 2 linux linux 4096 Mar 8 11:18 Documents drwxr-xr-x 2 linux linux 4096 Mar 8 11:18 Music drwxr-xr-x 2 linux linux 4096 Mar 8 11:18 Pictures drwxr-xr-x 2 linux linux 4096 Mar 8 11:18 Videos drwxr-xr-x 2 linux linux 4096 Mar 8 11:15 Templates drwxr-xr-x 6 linux linux 4096 Mar 8 11:15 Desktop
要获取最后修改的文件,请使用以下命令–
$ ls -ltc
样本输出应如下所示–
total 322428 drwxr-xr-x 3 linux linux 4096 Mar 8 13:59 Downloads drwxr-xr-x 6 linux linux 4096 Mar 3 14:34 Desktop lrwxrwxrwx 1 linux linux 37 Feb 27 13:25 PlayOnLinux's virtual drives -> /home/linux/.PlayOnLinux//wineprefix/ -rw-r--r-- 1 root root 70706 Feb 23 14:52 Selection_007.png -rw-r--r-- 1 root root 108159 Feb 23 14:49 root@linux: ~_005.png -rw-r--r-- 1 root root 145629 Feb 23 14:47 Workspace 1_004.png drwxr-xr-x 2 linux linux 4096 Feb 23 14:30 Pictures -rw-rw-r-- 1 linux linux 87631 Feb 19 14:08 account.png -rw-rw-r-- 1 linux linux 72172 Feb 19 14:07 network.png -rw-rw-r-- 1 linux linux 98362 Feb 19 14:05 sample1.png drwxr-xr-x 8 root root 4096 Feb 19 11:38 linux-dash drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Documents drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Music drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Public drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Templates drwxr-xr-x 2 linux linux 4096 Feb 19 11:08 Videos -rw-r--r-- 1 linux linux 8980 Feb 19 10:55 examples.desktop
该命令根据月份的第1字段对“ ls -l”命令的输出进行排序。
使用以下命令–
ls -l | sort -k1M
输出样本应如下所示–
drwxr-xr-x 9 nhooo 2000 4096 Feb 23 10:37 psensor-1.1.3 -rw-r--r-- 1 root root 108159 Feb 23 14:49 root@linux: ~_005.png -rw-r--r-- 1 root root 124850 Feb 23 14:52 root@linux: ~_006.png -rw-r--r-- 1 root root 145629 Feb 23 14:46 Workspace 1_004.png -rw-r--r-- 1 root root 145658 Feb 23 14:43 Workspace 1_003.png -rw-r--r-- 1 root root 146010 Feb 23 14:39 Workspace 1_002.png -rw-r--r-- 1 root root 178005 Feb 23 14:39 Workspace 1_001.png -rw-r--r-- 1 root root 200505 Aug 29 2015 Nhooo ad-DADCpj8sFCE.mp3 -rw-r--r-- 1 root root 215 Mar 3 10:28 dead.letter ................
要进行更深入的排序,请使用以下命令-
查找命令用于根据用户指定的条件搜索和查找文件和目录的列表。
要查找基于月份的已排序根文件,请使用以下命令–
# find / -type f -printf "\n%Ab %p" | head -n 11
上面的命令给出了按月访问的前11条目的完整列表。
May /etc/newt/palette.ubuntu May /etc/newt/palette.original May /etc/ltrace.conf Mar /etc/pulse/daemon.conf Mar /etc/pulse/default.pa Jan /etc/pulse/system.pa Mar /etc/pulse/client.conf Mar /etc/gtk-3.0/settings.ini Jul /etc/gtk-3.0/im-multipress.conf Feb /etc/subgid-
要使用带有月的第一个键来查找排序的根文件,请使用-k1M选项,如下所示–
# find / -type f -printf "\n%Ab %p" | head -n 11 | sort -k1M
样本输出应如下所示–
Jan /etc/pulse/system.pa Feb /etc/subgid- Mar /etc/gtk-3.0/settings.ini Mar /etc/pulse/client.conf Mar /etc/pulse/daemon.conf Mar /etc/pulse/default.pa May /etc/ltrace.conf May /etc/newt/palette.original May /etc/newt/palette.ubuntu Jul /etc/gtk-3.0/im-multipress.conf
上面的命令已按月份排序。
要基于日期查找排序的根文件,请使用以下命令–
# find / -type f -printf "\n%AD %AT %p" | head -n 11
上面的命令根据日期给出结果,如下所示–
05/14/13 22:26:41.0000000000 /etc/newt/palette.ubuntu 05/14/13 22:26:41.0000000000 /etc/newt/palette.original 05/10/14 05:20:35.0000000000 /etc/ltrace.conf 03/08/16 11:14:01.9113136790 /etc/pulse/daemon.conf 03/08/16 11:14:01.9193136790 /etc/pulse/default.pa 01/29/15 04:17:39.0000000000 /etc/pulse/system.pa 03/08/16 11:14:01.3433136590 /etc/pulse/client.conf 03/08/16 11:14:00.0873136140 /etc/gtk-3.0/settings.ini 07/01/15 08:44:19.0000000000 /etc/gtk-3.0/im-multipress.conf 02/22/16 10:49:09.0000000000 /etc/subgid-
要根据时间查找排序的根文件,请使用以下命令–
# find / -type f -printf "\n%AT %p" | head -n 11
样本输出应如下所示–
22:26:41.0000000000 /etc/newt/palette.ubuntu 22:26:41.0000000000 /etc/newt/palette.original 05:20:35.0000000000 /etc/ltrace.conf 11:14:01.9113136790 /etc/pulse/daemon.conf 11:14:01.9193136790 /etc/pulse/default.pa 04:17:39.0000000000 /etc/pulse/system.pa 11:14:01.3433136590 /etc/pulse/client.conf 11:14:00.0873136140 /etc/gtk-3.0/settings.ini 08:44:19.0000000000 /etc/gtk-3.0/im-multipress.conf 10:49:09.0000000000 /etc/subgid-
恭喜你!现在,您知道“如何在Linux中根据修改日期和时间查找和排序文件”。在我们的下一篇Linux文章中,我们将详细了解这些类型的命令。继续阅读!