Linux 对当前目录中的每个文件执行命令

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

Execute command for every file in the current dir

linuxbash

提问by Quamis

How can i execute a certain command for every file/folder in the current folder?

如何为当前文件夹中的每个文件/文件夹执行某个命令?

I've started with this as a base script, but this seems that its only working when using temporary files, and i dont really like the ideea. Is there any other way?

我已经开始将此作为基本脚本,但这似乎仅在使用临时文件时才有效,而且我真的不喜欢 ideea。有没有其他办法?

FOLDER=".";
DIRS=`ls -1 "$FOLDER">/tmp/DIRS`;

echo >"/tmp/DIRS1";
while read line ; do
    SIZE=`du "$FOLDER$line"`;
    echo $SIZE>>"/tmp/DIRS1";
done < "/tmp/DIRS";


For anyone interested, i wanted to make a list of folders, sorted by their size. Here is the final result

对于任何感兴趣的人,我想制作一个文件夹列表,按大小排序。这是最终结果

FOLDER="";
for f in $FOLDER/*; do 
  du -sb "$f";
done | sort -n | sed "s#^[0-9]*##" | sed "s#^[^\./]*##" | xargs -L 1 du -sh | sed "s|$FOLDER||";

which leads to du -sb $FOLDER/* | sort -n | sed "s#^[0-9]*##" | sed "s#^[^\./]*##" | xargs -L 1 du -sh | sed "s|$FOLDER||";

这导致 du -sb $FOLDER/* | sort -n | sed "s#^[0-9]*##" | sed "s#^[^\./]*##" | xargs -L 1 du -sh | sed "s|$FOLDER||";

采纳答案by Tony Delroy

Perhaps xargs, which reinvokes the command specified after it for each additional line of parameters received on stdin...

也许是 xargs,它为在 stdin 上接收到的每一行额外的参数重新调用在它之后指定的命令......

ls -1 $FOLDER | xargs du

But, in this case, why not...

但是,在这种情况下,为什么不...

du *

...? Or...

……?或者...

for X in *; do
    du $X
done

(Personally, I use zsh, where you can modify the glob pattern to only find say regular files, or only directories, only symlinks etc - I'm pretty sure there's something similar in bash - can dig for details if you need that).

(就我个人而言,我使用 zsh,您可以在其中修改 glob 模式以仅查找常规文件,或仅查找目录,仅查找符号链接等 - 我很确定 bash 中有类似的东西 - 如果您需要,可以挖掘详细信息)。

Am I missing part of your requirement?

我是否遗漏了您的部分要求?

回答by demas

This works for every file in the current directory:

这适用于当前目录中的每个文件:

do
   /usr/local/mp3unicode/bin/mp3unicode -s cp1251 --id3v2-encoding unicode "$file"
done

回答by Benoit

It is useless to parse output of lsto cycle over files. Bash can do it with wildcard expansion.

解析ls循环文件的输出是没用的。Bash 可以通过通配符扩展来实现。

Storing the result of duin a variable to output it to a file is also a useless use of a variable.

将 的结果存储du在变量中以将其输出到文件也是对变量的无用使用。

What I suggest:

我的建议:

for i in ./tmp/DIRS/*
do
    du "$i" >> "/tmp/DIRS1"
done

回答by Noufal Ibrahim

What's wrong with something like this?

这样的事情有什么问题?

function process() {
   echo "Processing "
}

for i in *
do
  process $i
done

You can put all the "work" you want done inside the function process. This will do it for your current directory.

您可以将您想要完成的所有“工作”放入 function 中process。这将为您的当前目录执行此操作。

回答by Daniel Schneller

The findcommand will let you execute a command for each item it finds, too. Without further arguments it will find all files and folders in the current directory, like this:

find命令还允许您为它找到的每个项目执行一个命令。没有进一步的参数,它将找到当前目录中的所有文件和文件夹,如下所示:

$ find -exec du -h {} \;

The {}part is the "variable" where the match is placed, here as the argument to du. \;ends the command.

{}部分是放置匹配项的“变量”,此处作为du. \;结束命令。

回答by oat

The invocation of action exec can be done by two ways:

action exec 的调用可以通过两种方式完成:

  • find . -type d -exec du -ch {} \;
  • find . -type d -exec du -ch {} +
  • find . -type d -exec du -ch {} \;
  • find . -type d -exec du -ch {} +

In the first command, the substitution {} occurs for each folder found. In the second one all the results of find are passed to exec at once, which matters, to obtain a final total.

在第一个命令中,对找到的每个文件夹进行替换 {}。在第二个中, find 的所有结果立即传递给 exec ,这很重要,以获得最终总数。

https://www.eovao.com/en/a/bash%20find%20exec%20linux/2/bash-execute-action-on-find-(-exec)-for-each-file

https://www.eovao.com/en/a/bash%20find%20exec%20linux/2/bash-execute-action-on-find-(-exec)-for-each-file