简体   繁体   English

迭代 ls -l 输出的每一行

[英]Iterating over each line of ls -l output

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 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?我怎么做?

Set IFS to newline, like this:将 IFS 设置为换行符,如下所示:

IFS='
'
for x in `ls -l $1`; 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 $1`; do echo $x; done)

Or use while |或使用 while | read instead:改为:

ls -l $1 | 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 $1)
EOF

It depends what you want to do with each line.这取决于您想对每一行做什么。 awk is a useful utility for this type of processing. awk 是用于此类处理的有用实用程序。 Example:例子:

 ls -l | awk '{print $9, $5}'

.. on my system prints the name and size of each item in the directory. .. 在我的系统上打印目录中每个项目的名称和大小。

As already mentioned, awk is the right tool for this.如前所述,awk 是解决此问题的正确工具。 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,而不是逐行解析“ls -l”的输出,您可以遍历所有文件并对每个单独的文件执行“ls -l”,如下所示:

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

You can also try the find command.您也可以尝试使用find命令。 If you only want files in the current directory:如果您只想要当前目录中的文件:

find . -d 1 -prune -ls

Run a command on each of them?对他们每个人运行一个命令?

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

Count lines, but only in files?计算行数,但仅限于文件?

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM