bash 具有适当时间样式的“find -ls”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20976065/
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
"find -ls" with proper time-style
提问by Denno
i use the following command to generate a filelist which i compare sometimes to see if something is changed:
我使用以下命令生成一个文件列表,我有时会比较它以查看是否有更改:
find /directory -xdev -ls
My Problem is that the time in the output is not always in the same format:
我的问题是输出中的时间并不总是相同的格式:
Sep 19 08:48 ./pool/f/f/0/ff046cc5b7188073cbd68207c52bddc5
Nov 2 06:24 ./pool/f/f/0/ff0e803c36d89315a6b3663ed1295f71
Jan 18 2012 ./pool/f/f/0/ff07f60465d8deb7a1aa38096d0b798d
Jan 18 2012 ./pool/f/f/0/ff07436f519bddf1d340afde5a240375
For the ls-command there is the option --time-format=long-iso to force the same time-format for all files. Is it possible to combinate this with the find-command?
对于 ls-command 有选项 --time-format=long-iso 强制所有文件使用相同的时间格式。是否可以将其与 find-command 结合使用?
Thanks
谢谢
采纳答案by David W.
There's nothing in my manpage about changing the format of the -ls
parameter. In fact, on my system, the output of -ls
is not influenced by various environment variables that affect the output of the ls
command itself. I assume that the format of the -ls
parameter is internal to find
and does not involve the actual ls
command. To me, this makes programming sense. Why run an external command? Just simulate the display.
我的联机帮助页中没有关于更改-ls
参数格式的内容 。事实上,在我的系统上, 的输出-ls
不受影响ls
命令本身输出的各种环境变量的影响。我假设-ls
参数的格式是内部的find
,不涉及实际ls
命令。对我来说,这使编程有意义。为什么要运行外部命令?只是模拟显示。
The only way I can think of to get around this is to use -exec
or -print0
to pass the results to the actual ls
command. A bit of warning: If you pass the name of the directory, ls
will print the contents of that directory, so you'll need to pass -d
to the ls
command or add -type f
into your find
query. I checked the manpage for find on Linux, and found that it's suppose to be the same output as -dils, so I used that. Since
-dis included, I didn't have to add
type -f` to my find query:
我能想到的唯一方法是使用-exec
或-print0
将结果传递给实际ls
命令。一点警告:如果您传递目录的名称,ls
将打印该目录的内容,因此您需要传递-d
给ls
命令或添加-type f
到您的find
查询中。我在 Linux 上检查了 find 的联机帮助页,发现它应该与我的 find 查询的-dils , so I used that. Since
-d is included, I didn't have to add
type -f`输出相同:
This is using the -exec
which will send each and every file or directory individually to the ls
command. If you have 10,000 files, ls
will be called 10,000 times.
这是使用-exec
将每个文件或目录单独发送到ls
命令的方法。如果有 10,000 个文件,ls
将被调用 10,000 次。
$ find /directory -xdev -exec ls -dils --time-style=long-iso {} \;
This maybe more efficient:
这可能更有效:
$ find /directory -xdev -print0 | xargs -0 ls -dils --time-style=long-iso
This will group as many file names as possible that will fit into the command buffer and pass them at once to the ls
command. It will call the ls
command as many times as needed to complete all of the files. For example, if you have 10,000 files in your find
command, the ls
command will be called maybe once or twice instead of 10,000 times.
这会将尽可能多的文件名分组到命令缓冲区中,并立即将它们传递给ls
命令。它将ls
根据需要多次调用该命令以完成所有文件。例如,如果您的find
命令中有 10,000 个文件,则该ls
命令可能会被调用一次或两次,而不是 10,000 次。
The problem is that xargs
has issues with funny file names, and there are some security issues as pointed out in the manpage:
问题在于xargs
有趣的文件名有问题,并且手册页中指出了一些安全问题:
It is not possible for xargs to be used securely, since there will always be a time gap between the production of the list of input files and their use in the commands that xargs issues. If other users have access to the system, they can manipulate the filesystem during this time window to force the action of the commands xargs runs to apply to files that you didn't intend. For a more detailed discussion of this and related problems, please refer to the ‘‘Security Considerations'' chapter in the findutils Texinfo documentation. The -execdir option of find can often be used as a more secure alternative.
xargs 不可能安全地使用,因为在输入文件列表的生成和它们在 xargs 发出的命令中的使用之间总是存在时间间隔。如果其他用户可以访问系统,他们可以在此时间段内操纵文件系统,以强制将 xargs 运行的命令的操作应用于您不想要的文件。有关此问题和相关问题的更详细讨论,请参阅 findutils Texinfo 文档中的“安全注意事项”一章。find 的 -execdir 选项通常可以用作更安全的替代方法。
The -print0
parameter uses a NUL
character to separate out file names instead of a NL
and the -0
parameter tells xargs
to use the NUL
character as a file name separator rather than whitespace(the characters in the $IFS
environment variable).
该-print0
参数使用一个NUL
字符来分隔文件名而不是 aNL
并且该-0
参数告诉xargs
使用该NUL
字符作为文件名分隔符而不是空格($IFS
环境变量中的字符)。
This means using -print0 | xags -0
works almost all of the time, but you may still decide that -exec ls
is a better way to go.
这意味着-print0 | xags -0
几乎所有时间都在使用作品,但您可能仍然认为这-exec ls
是更好的方法。
回答by Ken
You can use various options to -printf (man find)
您可以使用各种选项 -printf (man find)
find . -printf "%CY-%Cm-%Cd %CH:%CM\n"