4

if I run:

mkdir -p "$HOME"/old_foo && find "$HOME" -type d -name "*foo" -exec mv -vi {} "$HOME"/new_foo \;

I get:

/Users/medialab/old_foo -> /Users/medialab/new_foo

but also:

find: /Users/medialab/old_foo: No such file or directory

why is find searching for the directory old_foo after it already moved it?

Bleakley
  • 653
  • 2
  • 9
  • 19
  • the message is an annoyance, but the real problem for me is the non-zero return code of `1` which messes things up for me. – jxramos Aug 10 '22 at 08:25

2 Answers2

4

For your question, I think that find try to match the pattern also inside the directory. When find try to enter inside the directory, the directory is not here because he is already moved.

You can see it that if you use :

find "$HOME" -mindepth 1 -maxdepth 1 -type d -name "*foo" -exec mv -vi {} "$HOME"/new_foo \;

The command force the find to stay in your repertory, and don't explore inside all your repertory.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rilgar17
  • 181
  • 1
  • 7
2

This seems similar to the question here : Why does find -exec mv {} ./target/ + not work ? (on cygwin)

As pointed by the author, you could use gnu mv

I personally prefer using xargs as shown below :

mkdir old_foo && find . -type d -name "*foo" -print0 | xargs -0 -I {} mv {} new_foo
souser
  • 5,868
  • 5
  • 35
  • 50