awk'{print $ 9}'最后的ls -l列,包括文件名中的任何空格 [英] awk '{print $9}' the last ls -l column including any spaces in the file name
问题描述
如果某些文件中有空格,我将如何在ls -l
中用awk输出整个文件名.通常,我可以运行以下命令:
How would I get awk to output the whole file name in ls -l
if some of the files have spaces in them. Usually, I can run this command:
ls -l | awk '{print $9}'
如果文件中有空格,那将不起作用.可以打印$ 9,$ 10,$ 11等吗?
That doesn't work if the files have spaces in them. Is it possible to print $9, $10, $11 etc as well somehow?
推荐答案
一个更好的解决方案:不要一开始就尝试解析ls输出.
A better solution: Don't attempt to parse ls output in the first place.
irc.freenode.org #bash频道的官方Wiki解释了为什么这是一个坏主意,以及可以采用的替代方法: http://mywiki.wooledge.org/ParsingLs
The official wiki of the irc.freenode.org #bash channel has an explanation of why this is a Bad Idea, and what alternate approaches you can take instead: http://mywiki.wooledge.org/ParsingLs
使用查找,统计和类似工具将提供您正在寻找的功能,而无需陷阱(并非所有陷阱都是显而易见的-有些陷阱仅在移至具有不同ls实现的平台时才会发生).
Use of find, stat and similar tools will provide the functionality you're looking for without the pitfalls (not all of which are obvious -- some occur only when moving to platforms with different ls implementations).
对于您的特定示例,我猜您正在尝试仅在当前目录中查找文件(而不是目录);您当前使用ls -l
的实现存在错误,因为它排除了具有+ t或setuid权限的文件.实现此目标的正确方法如下:
For your specific example, I'm guessing that you're trying to find only files (and not directories) in your current directory; your current implementation using ls -l
is buggy, as it excludes files which have +t or setuid permissions. The Right Way to implement this would be the following:
find . -maxdepth 1 -type f -printf '%f\n'
这篇关于awk'{print $ 9}'最后的ls -l列,包括文件名中的任何空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!