Linux 列出目录(和子目录)中的所有文件(带完整路径),按访问时间排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9620050/
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
List all files (with full paths) in a directory (and subdirectories), order by access time
提问by Andrew
I'd like to construct a Linux command to list all files (with their full paths) within a specific directory (and subdirectories) ordered by access time.
我想构建一个 Linux 命令来列出按访问时间排序的特定目录(和子目录)中的所有文件(及其完整路径)。
ls can order by access time, but doesn't give the full path. find gives the full path, but the only control you have over the access time is to specify a range with -atime N (accessed at least 24*N hours ago), which isn't what I want.
ls 可以按访问时间排序,但不提供完整路径。find 给出了完整路径,但您对访问时间的唯一控制是使用 -atime N 指定一个范围(至少在 24*N 小时前访问过),这不是我想要的。
Is there a way to order by access time and get the full path at once? I could just write a script, but it seems there should be a way to do this with the standard Linux programs.
有没有办法按访问时间排序并立即获取完整路径?我可以只写一个脚本,但似乎应该有一种方法可以用标准的 Linux 程序来做到这一点。
采纳答案by Alex
find . -type f -exec ls -l {} \; 2> /dev/null | sort -t' ' -k +6,6 -k +7,7
This will find all files, and sort them by date and then time. You can then use awk
or cut
to extract the dates and files name from the ls -l
output
这将找到所有文件,并按日期和时间对它们进行排序。然后您可以使用awk
或cut
从ls -l
输出中提取日期和文件名
回答by Kent
you could try:
你可以试试:
ls -l $(find /foo/bar -type f )
- you can add other options (e.g. -t for sorting) to
ls
command to achieve your goal. - also you could add your searching criteria to
find
cmd
- 您可以在
ls
命令中添加其他选项(例如 -t 用于排序)以实现您的目标。 - 您也可以将搜索条件添加到
find
cmd
回答by Daenyth
find . -type f | xargs ls -ldt
should do the trick as long as there's not so many files that you hit the command like argument limit and spawn 2 instances of ls.
find . -type f | xargs ls -ldt
只要没有太多文件,您就可以使用参数限制之类的命令并生成 ls 的 2 个实例,就应该可以解决问题。
回答by Ron
find . -type f -exec ls -l --full-time {} \; 2> /dev/null | sort -t' ' -k +6,6 -k +7,7
Alex's answer did not work for me since I had files older than one year and the sorting got messed up. The above adds the --full-time parameter which nuetralizes the date/time values and makes them sortable regardless of how old they are.
亚历克斯的回答对我不起作用,因为我的文件超过一年并且排序混乱。上面添加了 --full-time 参数,它中和了日期/时间值,并使它们无论多老都可以排序。
回答by ericj
pwd | xargs -I % find % -type f