Any Simple Way to Do for Loop Pipe, Such as ` Ls | Grep ". *.7Z" | 7Z -E {}`

I am curious that is there an function/command to cope for loop pipe ?

It is easy to occur below demand

  1. you run a command return multiple line
  2. you need process each line
  3. pass each line to another function/command

For example:

you need find some path match a parttern , move each to another place :

# 1.
find path_A -name '*AAA*' -exec mv -t path_B {} +

# 2.
find path_A -name "*AAA*" -print0 | xargs -0 -I {} mv {} path_B 

People would be confused,

  1. why need mv -t (I mean, through a [for loop pipe] you can use mv {} some_path ,just like what you wrote without pipe )
  2. why exec or | xargs is write in that way , xargs need much more options, but they are actually no need in many computer language 。
  3. why I can't use ls ... mv -t path_B {} + , or ls | xargs -0 -I {} mv {} path_B , in other words , why it doesn't work after I change to another command ?
  4. Why I failed when change mv to 7z : find path_A -name '*AAA*' -exec 7z -t {} + ?

NOTE: Above are not question, just examples!!


I asked this question for a common and simple way to cope for loop pipe . Is there a simple and common way to do:

# 0
command with option (usually you use a command like this)

# 1
command with option [for loop pipe] command with option | command with option
# 2
command with option [for loop pipe] command with option | command with option [for loop pipe] command with option
# 3 
....

So you don't need change command with option when you use [for loop pipe] . This would be much convenient for daily use .

Update

I put all the example just want to say normal way is confusing when different comand has it own style to cope with multiline input, it would need exec or xargs or some else options . if we have a for pipe command which can connect two command with option . It would be much easy to use

I mean, if you are using an advanced language. For example python,

def pipe(x, func, *func_options):
    return func(x, *func_options)

def forloop_pipe(x, func, *func_options):   
    return [func(i, *func_options) for i in x.split('\n')]


def ls(*options)
    pass

def mv(*options)
    pass

a = ls('*.txt') 
b = forloop_pipe(a, mv, '/home')

This is a very simple example with a lot of flaws, but it explains what I'm trying to get in bash command .

7

3 Answers

I'm not sure this is a straight answer to the question (there are much more than one questions), but I think you are asking for something like:

  • for loop with command substitution or
  • while loop with process substitution.

for loop with $(command substitution)

Command substitution allows the output of a command to replace the command itself. We can combine this functionality with the for loop in this way:

for item in "$(find . -type f)"
do
    echo "$item" | tee -a ./"file-list.txt"
done

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.