A.A
A.A

Reputation: 4101

Loop through directory and run a command

I want to run the following command

./mc cp --recursive local//first/second remote//first/second

I want to loop through local//first and find all directories and run ./mc cp for each directories

How can I do this?

Upvotes: 0

Views: 97

Answers (2)

Sedfer
Sedfer

Reputation: 61

If local//first only contains directories, this should work for you:

./mc cp -r local//first/* remote//first

For a general case you can also use find to list all directories:

find local//first -type d

Then do whatever with this list using -exec or pipe it into xargs:

find local//first -type d -path "local//first/*" -prune -exec ./mc cp --recursive {} remote//first \;

Read more in documentation (find, xargs).

Upvotes: 1

Gurhan Polat
Gurhan Polat

Reputation: 706

You can use find tool to do such thing

find . -type d -exec echo {} \;

your command would something be like

find . -type d -exec ./mc cp --recursive "local/{}" "remote/{}" \;

Upvotes: 2

Related Questions