Linux 迭代 ls -l 输出的每一行

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

Iterating over each line of ls -l output

linuxshell

提问by Ivan

I want to iterate over each line in the output of: ls -l /some/dir/*

我想遍历输出中的每一行: ls -l /some/dir/*

Right now I'm trying: for x in $(ls -l $1); do echo $x; done

现在我正在尝试: for x in $(ls -l $1); do echo $x; done

However, this iterates over each element in the line separately, so I get:

但是,这会分别迭代该行中的每个元素,所以我得到:

-r--r-----
1
ivanevf
eng
1074
Apr
22
13:07
File1

-r--r-----
1
ivanevf
eng
1074
Apr
22
13:17
File2

But I want to iterate over each line as a whole, though. How do I do that?

但是,我想将每一行作为一个整体进行迭代。我怎么做?

采纳答案by Randy Proctor

Set IFS to newline, like this:

将 IFS 设置为换行符,如下所示:

IFS='
'
for x in `ls -l `; do echo $x; done

Put a sub-shell around it if you don't want to set IFS permanently:

如果您不想永久设置 IFS,请在其周围放置一个子外壳:

(IFS='
'
for x in `ls -l `; do echo $x; done)

Or use while | read instead:

或使用 while | 改为:

ls -l  | while read x; do echo $x; done

One more option, which runs the while/read at the same shell level:

还有一个选项,它在同一 shell 级别运行 while/read:

while read x; do echo $x; done << EOF
$(ls -l )
EOF

回答by Adam Holmberg

It depends what you want to do with each line. awk is a useful utility for this type of processing. Example:

这取决于您想对每一行做什么。awk 是用于此类处理的有用实用程序。例子:

 ls -l | awk '{print , }'

.. on my system prints the name and size of each item in the directory.

.. 在我的系统上打印目录中每个项目的名称和大小。

回答by Steve Emmerson

The read(1) utility along with output redirection of the ls(1) command will do what you want.

read(1) 实用程序以及 ls(1) 命令的输出重定向将执行您想要的操作。

回答by Axel

As already mentioned, awk is the right tool for this. If you don't want to use awk, instead of parsing output of "ls -l" line by line, you could iterate over all files and do an "ls -l" for each individual file like this:

如前所述,awk 是解决此问题的正确工具。如果您不想使用 awk,而不是逐行解析“ls -l”的输出,您可以遍历所有文件并对每个单独的文件执行“ls -l”,如下所示:

for x in * ; do echo `ls -ld $x` ; done

回答by David Koski

You can also try the findcommand. If you only want files in the current directory:

你也可以试试这个find命令。如果您只想要当前目录中的文件:

find . -d 1 -prune -ls

find . -d 1 -prune -ls

Run a command on each of them?

对他们每个人运行一个命令?

find . -d 1 -prune -exec echo {} \;

find . -d 1 -prune -exec echo {} \;

Count lines, but only in files?

计算行数,但仅限于文件?

find . -d 1 -prune -type f -exec wc -l {} \;

find . -d 1 -prune -type f -exec wc -l {} \;

回答by BradChesney79

So, why didn't anybody suggest just using options that eliminate the parts he doesn't want to process.

那么,为什么没有人建议只使用消除他不想处理的部分的选项。

On modern Debian you just get your file with:

在现代 Debian 上,您只需通过以下方式获取文件:

ls --format=single-column 

Further more, you don't have to pay attention to what directory you are running it in if you use the full directory:

此外,如果您使用完整目录,则不必注意运行它的目录:

ls --format=single-column /root/dir/starting/point/to/target/dir/

This last command I am using the above and I get the following output:

我正在使用上面的最后一个命令,我得到以下输出:

bot@dev:~/downloaded/Daily# ls --format=single-column /home/bot/downloaded/Daily/*.gz
/home/bot/downloaded/Daily/Liq_DailyManifest_V3_US_20141119_IENT1.txt.gz
/home/bot/downloaded/Daily/Liq_DailyManifest_V3_US_20141120_IENT1.txt.gz
/home/bot/downloaded/Daily/Liq_DailyManifest_V3_US_20141121_IENT1.txt.gz