how to list files and execute an action?

I'm not sure how to word what I'm trying to do.

I would like to:

  1. Generate a list of files (easy to do ls -l > list.txt)
  2. Carry out an action again each file in the list (not so easy to do)

Like:

List all files in /dir and then execute a move of each file individually. something like:

mv file1 /dir2
mv file2 /dir2
mv file3 /dir2

I know I can carry out a move with

"find . -type f -name "file" -exec mv {}" 

but I'm trying to find a work around to something else and I think this may be it.

Hi.

From it's man page:

xargs - build and execute command lines from standard input

i.e.

ls -1 | xargs -I{} mv {} /dir2

A simple for loop is what you need. This will take each value ls outputs, (Do not use -l)assigns it to the variable of x, and executes the commands between do and done.

# for x in `ls`
> do
> some_command $x
> another_command $x
> done

I tried this already and it didn't work either. Let me try it again.

ls -1 | while read line
do
echo $line
done

Note : ls -1(One) and Not l

You can redirect the output of ls -1 to a file and just do the same thing using a cat
Hope this helps

what's the `ls`?

Let's say I'm seaching for *.txt, normally I would type:

for x in *.txt
do
something $x
something $x
done

I don't understand the for x in `ls`

---------- Post updated at 02:36 PM ---------- Previous update was at 01:56 PM ----------

Thanks for the suggestions but this didn't solve the problem either. Thanks.

Then perhaps you should be more specific about what "the problem" is.

None of the solutions given don't solve problem you showed us.

Also, which OS and shell are you using?