bash 按上次编辑日期列出文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1404938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:30:21  来源:igfitidea点击:

List files by last edited date

bashshell

提问by Marty

I have a directory: /home/user/

我有一个目录: /home/user/

How can I list every file in this directory (including those in sub directories) and order them by the date that they were last modified?

如何列出此目录中的每个文件(包括子目录中的文件)并按上次修改日期对其进行排序?

回答by mipadi

You can use:

您可以使用:

ls -Rt

where -Rmeans recursive(include subdirectories) and -tmeans "sort by last modification date".

其中-R表示递归(包括子目录)并-t表示“按上次修改日期排序”。



To see a list of files sorted by date modified, use:

要查看按修改日期排序的文件列表,请使用:

ls -l -Rt

An alias can also be created to achieve this:

还可以创建别名来实现此目的:

alias lt='ls -lht'
lt

Where -hgives a more readable output.

哪里-h给出了更具可读性的输出。

回答by Paused until further notice.

If you'd like a master list in which all the files are sorted togetherby modification date, showing the directory they're in, but not grouped by directory, you can use this:

如果您想要一个主列表,其中所有文件修改日期排序在一起,显示它们所在的目录,但不按 directory 分组,您可以使用:

find . -type f -printf "%-.22T+ %M %n %-8u %-8g %8s %Tx %.8TX %p\n" | sort | cut -f 2- -d ' '

The result looks a lot like ls -l:

结果看起来很像ls -l

-rw-r--r-- 1 root     root         3892 08/11/2009 11:03:36 /usr/share/man/man1/xmllint.1.gz
-rw-r--r-- 1 root     root        22946 08/13/2009 11:59:20 /usr/share/man/man1/curl.1.gz
-rw-r--r-- 1 root     root          728 08/17/2009 12:06:33 /usr/share/man/man1/thunderbird.1.gz
-rw-r--r-- 1 root     root          873 08/18/2009 10:52:47 /usr/share/man/man1/libgnutls-config.1.gz
-rw-r--r-- 1 root     root         2552 08/19/2009 02:00:34 /usr/share/man/man3/Purple.3pm.gz
-rw-r--r-- 1 root     root         9546 08/19/2009 02:02:00 /usr/share/man/man1/pidgin.1.gz
-rw-r--r-- 1 root     root         2201 08/19/2009 02:02:46 /usr/share/man/man3/Pidgin.3pm.gz
-rw-r--r-- 1 root     root          926 08/19/2009 02:03:05 /usr/share/man/man1/purple-remote.1.gz
-rw-r--r-- 1 root     root        18052 08/19/2009 04:11:47 /usr/share/man/man1/mono.1.gz
-rw-r--r-- 1 root     root         1845 08/19/2009 04:11:47 /usr/share/man/man5/mono-config.5.gz


Mac OS X

Mac OS X

For those of you using Mac OS X, option -printfis not available on BSD find (you will get this error: find: -printf: unknown primary or operator). Fortunately you can Install GNU find through Homebrew(there should be an option to Finkand Macportsas well):

对于那些使用 Mac OS X 的人,选项-printf在 BSD find 上不可用(你会得到这个错误:)find: -printf: unknown primary or operator。幸运的是,您可以通过Homebrew安装 GNU find (FinkMacports也应该有一个选项):

brew install findutils

After install it the GNU find should be available to you as gfind. So, all you need to do is change the line above to:

安装后,GNU find 应该可以作为gfind. 因此,您需要做的就是将上面的行更改为:

gfind . -type f -printf "%-.22T+ %M %n %-8u %-8g %8s %Tx %.8TX %p\n" | sort | cut -f 2- -d ' '

回答by Mussa Charles

MAC OSX 2019

MAC OSX 2019

If you don't care about what time it was created but you want your list sorted then use this command

如果您不关心它的创建时间,但希望对列表进行排序,请使用此命令

==> ls -t

==> ls -t

If you want to order and see date and user info use this command

如果您想订购并查看日期和用户信息,请使用此命令

===> ls -lt

===> ls -lt

回答by Ulysse BN

For zshusers, you could also use glob qualifiers(also documented on man zshexpn):

对于zsh用户,您还可以使用glob 限定符(也记录在man zshexpn):

echo *(om)

Where ostands for sort order, upand mstands for last modification time.

其中o代表排序顺序,增长m代表最后修改时间

This can be useful when used in a for loop or another command:

这在 for 循环或其他命令中使用时很有用:

for file in *(^om); do
  [ -e "$file" ] || continue
  # do something with file orderer from least recently modified to last modified
done

Or chained with another glob qualifier:

或者与另一个 glob 限定符链接:

last_modified_file=(*(om[1]))