bash 在复合命令中查找 + ls -lT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14591219/
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 04:23:09  来源:igfitidea点击:

Find + ls -lT in compound command

linuxbashshellunixfind

提问by David542

To get the full timestamp of a file, I can do:

要获取文件的完整时间戳,我可以执行以下操作:

$ ls -lT

However, when I try the following:

但是,当我尝试以下操作时:

find . -ls -lT

I get an find: -lt: unknown primary or operator(using find . -lsworks).

我得到一个find: -lt: unknown primary or operator(使用find . -ls作品)。

What would be the correct way to use the find+ ls -lTcommand?

使用find+ls -lT命令的正确方法是什么?

回答by Andy Ross

The find "-ls" option isn't running ls and doesn't accept all its arguments. That said, I don't know why you want the -T argument, which is an obscure thing involving tab stops that I had to look up. But broadly, you just want to run a command ("ls -lT" in this case) on a bunch of files found by find. So any of the following should work: find . -type f | xargs -n1 ls -lTor find . -type f -exec ls -lT {} ';'or for i in $(find . -type f); do ls -lT $i; done.

find "-ls" 选项不运行 ls 并且不接受其所有参数。也就是说,我不知道你为什么想要 -T 参数,这是一个涉及制表位的晦涩的东西,我不得不查找。但总的来说,您只想对 find 找到的一堆文件运行一个命令(在本例中为“ls -lT”)。因此,以下任何一项都应该有效:find . -type f | xargs -n1 ls -lTorfind . -type f -exec ls -lT {} ';'for i in $(find . -type f); do ls -lT $i; done

Or, for the special case of ls that takes more than one command line argument, just find . -type f | xargs ls -lT

或者,对于需要多个命令行参数的 ls 的特殊情况,只需 find . -type f | xargs ls -lT

回答by Andy Lester

If you're parsing the output of ls, you're doing it wrong. There's always a better way to interface with the filesystem than parsing human-readable output.

如果您正在解析 的输出ls,那么您就做错了。总有比解析人类可读输出更好的方式与文件系统交互。

I'm not sure what you mean by the "full timestamp of the file", but if you want, say, the last modification time, use stat.

我不确定你所说的“文件的完整时间戳”是什么意思,但如果你想要,比如上次修改时间,请使用stat.

stat --printf='%x' foo 
2013-01-29 13:33:32.000000000 -0600

Run man statto see all the other formatting options in the --printfargument.

运行man stat以查看参数中的所有其他格式选项--printf