Linux 如何递归列出具有大小和上次修改时间的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6964565/
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
How to recursive list files with size and last modified time?
提问by whatupdave
Given a directory i'm looking for a bash one-liner to get a recursive list of all files with their size and modified time tab separated for easy parsing. Something like:
给定一个目录,我正在寻找一个 bash one-liner 来获取所有文件的递归列表,它们的大小和修改的时间选项卡分开以便于解析。就像是:
cows/betsy 145700 2011-03-02 08:27
horses/silver 109895 2011-06-04 17:43
采纳答案by Adam Rosenfield
You can use stat(1)
to get the information you want, if you don't want the full ls -l
output, and you can use find(1)
to get a recursive directory listing. Combining them into one line, you could do this:
stat(1)
如果您不想要完整的ls -l
输出,您可以使用来获取所需的信息,并且可以使用find(1)
来获取递归目录列表。将它们组合成一行,你可以这样做:
# Find all regular files under the current directory and print out their
# filenames, sizes, and last modified times
find . -type f -exec stat -f '%N %z %Sm' '{}' +
If you want to make the output more parseable, you can use %m
instead of %Sm
to get the last modified time as a time_t
instead of as a human-readable date.
如果您想让输出更易于解析,您可以使用%m
而不是%Sm
将上次修改时间作为time_t
人类可读的日期而不是获取。
回答by John Kugelman
find is perfect for recursively searching through directories. The -ls
action tells it to output its results in ls -l
format:
find 非常适合递归搜索目录。该-ls
操作告诉它以以下ls -l
格式输出结果:
find /dir/ -ls
On Linux machines you can print customized output using the -printf
action:
在 Linux 机器上,您可以使用以下-printf
操作打印自定义输出:
find /dir/ -printf '%p\t%s\t%t\n'
See man find
for full details on the format specifiers available with -printf
. (This is not POSIX-compatible and may not be available on other UNIX flavors.)
有关man find
可用的格式说明符的完整详细信息,请参阅-printf
。(这与 POSIX 不兼容,并且在其他 UNIX 版本上可能不可用。)
回答by Keith Thompson
find * -type f -printf '%p\t%s\t%TY-%Tm-%Td %Tk:%TM\n'
find * -type f -printf '%p\t%s\t%TY-%Tm-%Td %Tk:%TM\n'
If you prefer fixed-width fields rather than tabs, you can do things like changing %s
to %10s
.
如果您更喜欢固定宽度的字段而不是选项卡,您可以执行诸如更改%s
为%10s
.
I used find * ...
to avoid the leading "./" on each file name. If you don't mind that, use .
rather than *
(which also shows files whose names start with .
). You can also pipe the output through sed 's/^\.\///'
.
我曾经find * ...
避免在每个文件名中使用前导“./”。如果您不介意,请使用.
而不是*
(它还显示名称以 开头的文件.
)。您还可以通过管道输出sed 's/^\.\///'
。
Note that the output order will be arbitrary. Pipe through sort
if you want an ordered listing.
请注意,输出顺序将是任意的。sort
如果您想要有序列表,请通过管道。
回答by bunk3m
You could try this for recursive listing from current folder called "/from_dir"
您可以尝试从名为“/from_dir”的当前文件夹中递归列表
find /from_dir/* -print0 | xargs -0 stat -c “%n|%A|%a|%U|%G” > permissions_list.txt?
?Lists files and directories passes through to stat command and puts all the info into a file called permissions_list.txt?
? 列出文件和目录传递给 stat 命令并将所有信息放入一个名为 permissions_list.txt 的文件中?
“%n|%A|%a|%U|%G” will give you the following result in the file:
“%n|%A|%a|%U|%G”将在文件中给出以下结果:
from_? dir|drwxr-sr-x|2755|root|root?
from_dir/filename|-rw-r–r–|644|root|root
?Cheers!?
?干杯!?