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

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

"find -ls" with proper time-style

linuxbashfinddatetime-formatls

提问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 -lsparameter. In fact, on my system, the output of -lsis not influenced by various environment variables that affect the output of the lscommand itself. I assume that the format of the -lsparameter is internal to findand does not involve the actual lscommand. 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 -execor -print0to pass the results to the actual lscommand. A bit of warning: If you pass the name of the directory, lswill print the contents of that directory, so you'll need to pass -dto the lscommand or add -type finto your findquery. 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 addtype -f` to my find query:

我能想到的唯一方法是使用-exec-print0将结果传递给实际ls命令。一点警告:如果您传递目录的名称,ls将打印该目录的内容,因此您需要传递-dls命令或添加-type f到您的find查询中。我在 Linux 上检查了 find 的联机帮助页,发现它应该与我的 find 查询的-dils , so I used that. Since-d is included, I didn't have to addtype -f`输出相同:

This is using the -execwhich will send each and every file or directory individually to the lscommand. If you have 10,000 files, lswill 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 lscommand. It will call the lscommand as many times as needed to complete all of the files. For example, if you have 10,000 files in your findcommand, the lscommand will be called maybe once or twice instead of 10,000 times.

这会将尽可能多的文件名分组到命令缓冲区中,并立即将它们传递给ls命令。它将ls根据需要多次调用该命令以完成所有文件。例如,如果您的find命令中有 10,000 个文件,则该ls命令可能会被调用一次或两次,而不是 10,000 次。

The problem is that xargshas 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 -print0parameter uses a NULcharacter to separate out file names instead of a NLand the -0parameter tells xargsto use the NULcharacter as a file name separator rather than whitespace(the characters in the $IFSenvironment variable).

-print0参数使用一个NUL字符来分隔文件名而不是 aNL并且该-0参数告诉xargs使用该NUL字符作为文件名分隔符而不是空格$IFS环境变量中的字符)。

This means using -print0 | xags -0works almost all of the time, but you may still decide that -exec lsis 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"